I've a GUI (written in PyQt) with three tabs, each a QtGui.QMainWindow class. The whole GUI is defined by a .ui file. That seems to be the main difference between the similar questions on SO (links in next paragraph) where people are looking to hide a QMainWindow object. More specifically, that I'm trying to hide a QMainWindow object that's part of (not the whole) Gui element.
The main window and its tabs look like this:
Depending on a setting in my config file, I want to either leave TEST visible, or hide it from the user. I've checked both the PyQT Docs (ie removeDockWidget()
?) and there are two similar SO queries (Link One and link Two), and a SO Question suggesting layouts/setVisible..
But I can't get it right. If I use .hide()
or .setVisible(False)
then nothing seems to change. If I set .setVisible(True)
then an additional, miniature window appears on startup, with the name of my main class.
The structure of my code is:
My entry/main class:
class Gui:
def __init__(self):
# Create the main window GUI and show it
self.mainWindow= GuiMainWindow(self)
self.mainWindow.show()
<...>
def main():
app = QtGui.QApplication(sys.argv)
myGui = Gui(app)
sys.exit(app.exec_())
if __name__ == '__main__':
main()
GuiMainWindow contain the three tabs:
class GuiMainWindow(QtGui.QMainWindow):
def __init__(self, appMain):
QtGui.QMainWindow.__init__(self)
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
# Create helper objects to manage main window tabs
self.daqTab = GuiMainDaqTab(appMain, self)
self.pwrTab = GuiMainPowerTab(appMain, self)
self.testTab = GuiMainTestTab(appMain, self)
self.testTab.hideMe()
Finally, the TEST tab is what I want to hide/show:
class GuiMainTestTab(QtGui.QMainWindow):
def __init__(self, appMain, mainWindow):
super(GuiMainTestTab, self).__init__()
self.appMain = appMain
self.mainWindow = mainWindow
self.ui = mainWindow.ui
<...>
def hideMe(self):
self.close()
# self.hide() # Also tried hide()
# self.setVisible(False) # ..Or setVisible()
--- Edit to assist the answer / my match comment to answer below ---
So, the class GuiMainTestTab
accesses the contents of the ui file through its member self.ui
. Digging through the ui file I found that testTab
is its name of the Test tab in the ui file (as opposed to testTab
in GuiMainWindow
..!). It's attached to a QTabWidget called verticalTabWidget. So to programmatically find Test tab's index, and remove it:
testIdx = self.ui.verticalTabWidget.indexOf(self.ui.testTab)
self.ui.verticalTabWidget.removeTab(testIdx)