pythonpyqtqstackedwidget

Using QStackedWidget in PyQt5


I have QStackedWidget in ApplicationWindow class and buttons which are going to point to different QWidgets in MenuWindow. I need a help with writing a function which would change the CurrentWidget according to button clicked - e.g. login_button would change the CurrentWidget to LoginWindow.
When trying to do it myself I ran into recursion problems as I have just started with learning Python.

from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
import sys

class ApplicationWindow(QWidget):

def __init__(self):
    super(ApplicationWindow, self).__init__()
    # stack = Controller()
    self.menu = MenuWindow()
    self.login = LoginWindow()

    self.setGeometry(0, 0, 800, 600)
    self.setWindowTitle('Finance tracker')

    self.setAutoFillBackground(True)
    p = self.palette()
    p.setColor(self.backgroundRole(), Qt.green)
    self.setPalette(p)

    self.stack = QStackedWidget()
    self.stack.setSizePolicy(QSizePolicy.Maximum, QSizePolicy.Maximum)
    self.stack.addWidget(self.menu)
    self.stack.addWidget(self.login)
    self.stack.setCurrentWidget(self.menu)

    layout = QVBoxLayout()
    layout.addWidget(self.stack)
    layout.setAlignment(Qt.AlignCenter)
    self.setLayout(layout)

class MenuWindow(QWidget):

def __init__(self):
    super(MenuWindow, self).__init__()
    self.setGeometry(0, 0, 250, 200)
    box = QVBoxLayout()

    self.setAutoFillBackground(True)
    p = self.palette()
    p.setColor(self.backgroundRole(), Qt.red)
    self.setPalette(p)

    label = QLabel('Welcome to finance tracker')
    label.setStyleSheet('font: 24pt')
    box.addWidget(label, alignment=Qt.AlignCenter)

    login_button = QPushButton('Login')
    login_button.clicked.connect(qApp.exit)

    new_button = QPushButton('Create a new account')
    new_button.clicked.connect(qApp.exit)

    exit_button = QPushButton('Exit')
    exit_button.clicked.connect(qApp.exit)

    for button in [login_button, new_button, exit_button]:
        button.setStyleSheet('font: 14pt')
        button.setFixedSize(200, 50)
        box.addWidget(button, alignment=Qt.AlignCenter)

    self.setLayout(box)
    self.show()

class LoginWindow(QWidget):

def __init__(self):
    super(LoginWindow, self).__init__()
    self.setGeometry(0, 0, 10, 250)

    self.setAutoFillBackground(True)
    p = self.palette()
    p.setColor(self.backgroundRole(), Qt.blue)
    self.setPalette(p)

    label = QLabel('Welcome to finance tracker')
    box = QVBoxLayout()
    box.addWidget(label)
    self.setLayout(box)
    self.show()

if __name__ == '__main__':
    app = QApplication(sys.argv)
    window = ApplicationWindow()
    window.show()
    sys.exit(app.exec())

Solution

  • Since you are using QPushButtons to switch pages I would add them to a QButtonGroup. This way you can connect the QButtonGroup.buttonClicked[int] signal to QStackedWidget.setCurrentIndex. Keep a pointer to the QButtonGroup in your MenuWindow.

    class MenuWindow(QWidget):
    
        def __init__(self):
            ...
            login_button = QPushButton('Login')
            new_button = QPushButton('Create a new account')
    
            exit_button = QPushButton('Exit')
            exit_button.clicked.connect(qApp.exit)
    
            self.btn_group = QButtonGroup()
            for i, button in enumerate([login_button, new_button, exit_button]):
                button.setStyleSheet('font: 14pt')
                button.setFixedSize(200, 50)
                box.addWidget(button, alignment=Qt.AlignCenter)
                self.btn_group.addButton(button)
                self.btn_group.setId(button, i + 1)
            ...
    

    And now you can connect the signal and slot in your ApplicationWindow.

    class ApplicationWindow(QWidget):
    
        def __init__(self):
            ...
            self.menu.btn_group.buttonClicked[int].connect(self.stack.setCurrentIndex)