1

If I create my UI in QT Designer (and import UI to the script), how can I hide and show tabs in my script?

class Tool(QMainWindow, uiTool.Ui_Tool):
    def __init__(self):
        super(Tool, self).__init__()
        # SETUP UI
        self.setupUi(self)

        # self.tabWidget.removeTab() ???
ekhumoro
  • 115,249
  • 20
  • 229
  • 336
kiryha
  • 183
  • 1
  • 2
  • 14
  • When you remove a tab, the page widget is not deleted. So you can put it back with something like `self.tabWidget.insertTab(index, self.myPage, 'Title')`. You should probably make sure you set a sensible name for each page in Qt Designer. You can get the index of the page with `self.tabWidget.indexOf(self.myPage)`. – ekhumoro Oct 05 '17 at 18:03
  • Thanks, @ekhumoro! But the code `self.tabWidget.removeTab()` is not working. That's the original issue. Then I will need to figure out how to restore removed tabs. And what is a page in this context, is it the same as tab? – kiryha Oct 05 '17 at 19:23
  • You need pass an index to `removeTab`. In Qt Designer, whenever you create a tab-widget, it will automatically create some page-widgets. These will be shown below the tab-widget in the Object Inspector. If you click on these page-widgets, you can set the **objectName**. If you set one to, say, "myPage", you can then use `self.indexOf(self.myPage)` to get its index, then do `self.removeTab(index)` to remove it. – ekhumoro Oct 05 '17 at 19:42
  • @ekhumoro Cant find any page widgets nither in QT designer, nor in compiled python code! See the [QT screen](https://drive.google.com/open?id=0B08-uC9HedKCRlNUNWhQeEpPT1E) – kiryha Oct 05 '17 at 20:17
  • See my answer below for a simple demo. The pages are show as `tab_A`, `tab_B` and `tab_C` (Object Inspectror pane, top-right) in your screenshot. So they can be accessed as `self.tab_A`, `self.tab_B`, etc. – ekhumoro Oct 05 '17 at 20:20
  • @ekhumoro Ah! I was wrong with my terminology: I need to hide/show pages! Will take a look at your example. Thanks again. – kiryha Oct 05 '17 at 20:33
  • No problem. (PS: if you find the answer useful, please mark it as accepted. This will earn you 2 rep points, which should be enough to give you some other privileges). – ekhumoro Oct 05 '17 at 20:40

1 Answers1

1

There is no way to hide/show the tabs in a tab-widget, so you will need to remove and replace them instead.

Below is a demo script that shows how to do this. I have not attempted to keep track of the original indexes in this example - it just shows the basic usage of the methods involved:

import sys
from PyQt5 import QtCore, QtWidgets

class Ui_Form(object):
    def setupUi(self, Form):
        Form.setObjectName("Form")
        Form.resize(354, 268)
        self.gridLayout = QtWidgets.QGridLayout(Form)
        self.gridLayout.setObjectName("gridLayout")
        self.tabWidget = QtWidgets.QTabWidget(Form)
        self.tabWidget.setObjectName("tabWidget")
        self.tabRed = QtWidgets.QWidget()
        self.tabRed.setObjectName("tabRed")
        self.tabWidget.addTab(self.tabRed, "")
        self.tabBlue = QtWidgets.QWidget()
        self.tabBlue.setObjectName("tabBlue")
        self.tabWidget.addTab(self.tabBlue, "")
        self.tabGreen = QtWidgets.QWidget()
        self.tabGreen.setObjectName("tabGreen")
        self.tabWidget.addTab(self.tabGreen, "")
        self.gridLayout.addWidget(self.tabWidget, 0, 0, 1, 2)
        self.buttonRemove = QtWidgets.QPushButton(Form)
        self.buttonRemove.setObjectName("buttonRemove")
        self.gridLayout.addWidget(self.buttonRemove, 1, 0, 1, 1)
        self.buttonRestore = QtWidgets.QPushButton(Form)
        self.buttonRestore.setObjectName("buttonRestore")
        self.gridLayout.addWidget(self.buttonRestore, 1, 1, 1, 1)

        self.retranslateUi(Form)
        self.tabWidget.setCurrentIndex(2)
        QtCore.QMetaObject.connectSlotsByName(Form)

    def retranslateUi(self, Form):
        _translate = QtCore.QCoreApplication.translate
        Form.setWindowTitle(_translate("Form", "Form"))
        self.tabWidget.setTabText(self.tabWidget.indexOf(self.tabRed), _translate("Form", "Red"))
        self.tabWidget.setTabText(self.tabWidget.indexOf(self.tabBlue), _translate("Form", "Blue"))
        self.tabWidget.setTabText(self.tabWidget.indexOf(self.tabGreen), _translate("Form", "Green"))
        self.buttonRemove.setText(_translate("Form", "Remove"))
        self.buttonRestore.setText(_translate("Form", "Restore"))

class Window(QtWidgets.QWidget, Ui_Form):
    def __init__(self):
        super(Window, self).__init__()
        self.setupUi(self)
        self.buttonRemove.clicked.connect(self.handleButtonRemove)
        self.buttonRestore.clicked.connect(self.handleButtonRestore)
        self.tab_pages = []
        for index in range(self.tabWidget.count()):
            self.tab_pages.append((
                self.tabWidget.widget(index),
                self.tabWidget.tabText(index),
                ))

    def handleButtonRemove(self):
        index = self.tabWidget.currentIndex()
        if index >= 0:
            self.tabWidget.removeTab(index)

    def handleButtonRestore(self):
        for page, title in self.tab_pages:
            if self.tabWidget.indexOf(page) < 0:
                self.tabWidget.addTab(page, title)

if __name__ == '__main__':

    app = QtWidgets.QApplication(sys.argv)
    window = Window()
    window.setGeometry(600, 100, 300, 200)
    window.show()
    sys.exit(app.exec_())
ekhumoro
  • 115,249
  • 20
  • 229
  • 336