PyQT Qtabwidget hide and close certain tab

7.1k Views Asked by At

Is it possible to hide and close a certain tab in Qtabwidget? I have 5 tabs, two of them are plots and generated while using my software. First I want to hide the two plots in the beginning and second I want to make them closable after there were generated. Is this possible? With self.setTabsClosable(True) all tabs will be closable.

Thanks

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

There are 1 best solutions below

0
On BEST ANSWER

You can remove the close button of the tabs that should not be closed. This is done by with the function setTabButton of QTabBar, like this:

QtGui.QTabWidget.tabBar().setTabButton(0, QtGui.QTabBar.RightSide,None)

Here, we set the button of the first tab to be None.

With the same function, you could also create your own close button on a tab (and remove self.setTabsClosable(True))