在QTabWidget中添加已移除的标签(PyQT)

0 投票
1 回答
4916 浏览
提问于 2025-04-17 07:14

我在使用pyQT的时候遇到了一个问题。
我用设计工具创建了一个图形界面,里面有一个QTabWidget(就是一个可以切换的标签页)。
问题是我想在我的函数运行的时候隐藏和显示这些标签页。我找到了一种解决办法,就是先把所有的标签页都删掉,然后再加回来。
假设我只有两个标签页:

removedTab = self._application.getAlgorithmGUI().getWidget('tabWidget_Verification').widget(1)
self._application.getAlgorithmGUI().getWidget( 'tabWidget_Verification' ).removeTab( 1 )

但是当我后来尝试把删掉的标签页加回去的时候,我的程序就崩溃了。

self._application.getAlgorithmGUI().getWidget( 'tabWidget_Verification' ).addTab(removedTab,QString.fromUtf8("TabRemoved"))

这是我收到的错误信息:

QCoreApplication::sendPostedEvents: Cannot send posted events for objects in another thread
QCoreApplication::sendPostedEvents: Cannot send posted events for objects in another thread
QCoreApplication::sendPostedEvents: Cannot send posted events for objects in another thread
QCoreApplication::sendPostedEvents: Cannot send posted events for objects in another thread
<unknown>: Fatal IO error 11 (Ressource temporairement non disponible) on X server :0.0.

有没有什么建议?

1 个回答

2

你可以在你的主窗口对象或者其他任何小部件中声明你需要的所有标签页。例如:

self.tab = QtGui.QWidget()
self.tab.setObjectName(_fromUtf8("tab"))

即使你还没有调用 addTab() 方法,你也可以正常地将小部件分配给你的标签页。例如:

self.lineEdit = QtGui.QLineEdit(self.tab)

当需要的时候,你可以显示你的标签页。例如:

self.tabWidget.addTab(self.tab, "Label")

同样地,你也可以通过它的索引号将标签页移除。例如:

self.tabWidget.removeTab(3)

同一个标签页可以被多次调用,随便你想多少次。我觉得这样做既干净又简单。如果这不符合你的需求,请告诉我。

撰写回答