PyQT Qtabwidget 隐藏和关闭特定选项卡

4

有没有可能隐藏和关闭Qtabwidget中的某个选项卡?我有5个选项卡,其中两个是在使用我的软件时生成的图表。首先,我想在开始时隐藏这两个图表,其次,在生成后使它们可关闭。这种情况是否可能?使用self.setTabsClosable(True)将使所有选项卡都可关闭。

谢谢

import sys
from PyQt4 import QtGui

class QCustomTabWidget (QtGui.QTabWidget):
    def __init__ (self, parent = None):
        super(QCustomTabWidget, self).__init__(parent)
        self.setTabsClosable(True)
        self.tabCloseRequested.connect(self.closeTab)
        for i in range(1, 10):
            self.addTab(QtGui.QWidget(), 'Tab %d' % i)

    def closeTab (self, currentIndex):
        currentQWidget = self.widget(currentIndex)
        currentQWidget.deleteLater()
        self.removeTab(currentIndex)

myQApplication = QtGui.QApplication([])
myQCustomTabWidget = QCustomTabWidget()
myQCustomTabWidget.show()
sys.exit(myQApplication.exec_())
1个回答

7
您可以通过使用 QTabBar 的函数 setTabButton 来移除不应关闭的选项卡的关闭按钮,例如:
QtGui.QTabWidget.tabBar().setTabButton(0, QtGui.QTabBar.RightSide,None)

这里,我们将第一个选项卡的按钮设置为

通过相同的函数,您也可以在选项卡上创建自己的关闭按钮(并移除self.setTabsClosable(True))。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接