Cheyn Shmuel
Cheyn Shmuel

Reputation: 448

PyQt: switch windows/layouts created by Qt Designer

I am creating an app using PyQt4. I have created two interfaces with Qt Designer. When a button is pushed I would like to switch between one layout and the other.

A sample of my code is:

from PyQt4 import QtGui, uic
form_class = uic.loadUiType("sample.ui")[0]
form_class2 = uic.loadUiType("sample2.ui")[0]

class SecondLayout(form_class2, QtGui.QMainWindow):
    def __init__(self, parent=None):
        QtGui.QMainWindow.__init__(self, parent)
        form_class2.setupUi(self)

class MainWindow(form_class, QtGui.QMainWindow):
    def __init__(self, parent=None):
        QtGui.QMainWindow.__init__(self, parent)
        self.setupUi(self)
        self.btn.clicked.connect(self.open_new_window)
    def open_new_window(self):
        self.Window = SecondLayout()
        # here I would like to switch the layout with a layout of self.Window

app = QtGui.QApplication(sys.argv)
myWindow = MainWindow(None) 
myWindow.show()
app.exec_()

I have done a lot of searching and reading about QStackedLayout, but haven't been able to get it to work with layouts created in Qt Designer.

My Question is how would I be able to have one Main Window and switch its central widget but i'm not sure if that would work for the seperate menus. I have defined all the menus and widgets and status bars, etc. in Qt Designer as two different projects(both main windows) so I would like to have the main program in one of the main windows, then at some point create an instance of the second main window and switch the layout and all the widgets, menus, text edits, etc. I tried using setCentralWidget but hasn't worked for me.

Could someone please explain to me how to do this.

Upvotes: 1

Views: 2061

Answers (1)

ekhumoro
ekhumoro

Reputation: 120608

It sounds like you have two completely separate main windows. There is really no point in switching all the widgets, menus, toolbars, etc, because they will have no shared code. You might just as well simply hide one window, and then show the other one.

Here is a simple demo that shows one way to do that:

PyQt5

from PyQt5 import QtWidgets

class Window1(QtWidgets.QMainWindow):
    def __init__(self, window2=None):
        super(Window1, self).__init__()
        self.setGeometry(500, 100, 100, 50)
        self.button = QtWidgets.QPushButton('Go To Window 2', self)
        self.button.clicked.connect(self.handleButton)
        self.setCentralWidget(self.button)
        self._window2 = window2

    def handleButton(self):
        self.hide()
        if self._window2 is None:
            self._window2 = Window2(self)
        self._window2.show()

class Window2(QtWidgets.QMainWindow):
    def __init__(self, window1=None):
        super(Window2, self).__init__()
        self.setGeometry(500, 100, 100, 50)
        self.button = QtWidgets.QPushButton('Go To Window 1', self)
        self.button.clicked.connect(self.handleButton)
        self.setCentralWidget(self.button)
        self._window1 = window1

    def handleButton(self):
        self.hide()
        if self._window1 is None:
            self._window1 = Window1(self)
        self._window1.show()

if __name__ == '__main__':

    import sys
    app = QtWidgets.QApplication(sys.argv)
    window = Window1()
    window.show()
    sys.exit(app.exec_())

PyQt4

from PyQt4 import QtCore, QtGui

class Window1(QtGui.QMainWindow):
    def __init__(self, window2=None):
        super(Window1, self).__init__()
        self.setGeometry(500, 100, 100, 50)
        self.button = QtGui.QPushButton('Go To Window 2', self)
        self.button.clicked.connect(self.handleButton)
        self.setCentralWidget(self.button)
        self._window2 = window2

    def handleButton(self):
        self.hide()
        if self._window2 is None:
            self._window2 = Window2(self)
        self._window2.show()

class Window2(QtGui.QMainWindow):
    def __init__(self, window1=None):
        super(Window2, self).__init__()
        self.setGeometry(500, 100, 100, 50)
        self.button = QtGui.QPushButton('Go To Window 1', self)
        self.button.clicked.connect(self.handleButton)
        self.setCentralWidget(self.button)
        self._window1 = window1

    def handleButton(self):
        self.hide()
        if self._window1 is None:
            self._window1 = Window1(self)
        self._window1.show()

if __name__ == '__main__':

    import sys
    app = QtGui.QApplication(sys.argv)
    window = Window1()
    window.show()
    sys.exit(app.exec_())

Upvotes: 2

Related Questions