Kevin
Kevin

Reputation: 39

reset MainWindow layout Pyqt5

I am creating a simple PyQt GUI that initially shows a login window. Upon successful login, the MainWindow is supposed to refresh and change the MainWindow layout. The approach I used is creating two layouts (loginLayout and mainLayout). The initial layout is set to loginLayout with

self.view.setLayout(loginLayout)
self.setCentralWidget(self.view)

then upon successful login, the handle_login function would change the MainWindow layout to mainLayout:

self.view.setLayout(mainLayout)
self.setCentralWidget(self.view)

Essentially, changing the centralWidget of MainWindow.

For days I could not get this to work. The MainWindow layout would not refresh after a successful login.

Below are my MainWindow class and main.py

MainWindow Class:

import glowButton as custom
from Login_class import *
from binnerWidget import *

WINDOW_SIZE = 1000, 700

class MainWindow(QMainWindow):
    LOGIN_CODE = 0

    # constructor for MainWindow class
    def __init__(self):
        # calls the constructor of the parent class 'QMainWindow' using 'super'
        # ensures the initialization of the parent class before customization
        super(MainWindow, self).__init__()
        # create dragView object which is a child widget of "MainWindow"
        # self here is the current MainWindow object, and is passed in dragView constructor
        self.view = custom.dragView(self)
        # set up (calls/initializes) MainWindows's user interface attributes
        self.initUI()

    # set up the initial window UI
    def initUI(self):
        # set position to 300, 300, and size of window, * to unpack tuple
        self.setGeometry(300, 300, *WINDOW_SIZE)
        # set window title
        self.setWindowTitle('PSE Piezometer Limits')
        # set the minimum height and width of the window (when resizing)
        self.setMinimumHeight(550)
        self.setMinimumWidth(950)
        # set window flags (need understand more)
        self.setWindowFlags(Qt.Window | Qt.FramelessWindowHint)  # removes window decorations (ex: border, buttons...)
        self.setAttribute(Qt.WA_TranslucentBackground)  # sets window background semi-translucent

        # loginLayout
        loginLayout = QVBoxLayout()
        self.loginWidget = Login(self)
        self.BannerWidget = Banner(self)
        loginLayout.addWidget(self.BannerWidget)
        # loginLayout.setStretchFactor(bannerLayout, 0)  # so bannerLayout would stay on top of the MainWindow
        loginLayout.addWidget(self.loginWidget)
        
        # mainLayout
        self.mainLayout = QVBoxLayout()
        self.mainLayout.addWidget(Banner(self)) #, alignment=Qt.AlignTop)
        # self.mainLayout.setStretchFactor(bannerLayout, 0)  # so bannerLayout would stay on top of the MainWindow
        self.mainLayout.addStretch(1)

        self.status = QLabel()

        # set MainWindow loginLayout and Initially, show the login layout
        self.view.setLayout(loginLayout)
        self.setCentralWidget(self.view)

        self.loginWidget.login_button.clicked.connect(self.handle_login)


    def handle_login(self):
        # Implement your login logic here
        username = self.loginWidget.username_edit.text()
        password = self.loginWidget.password_edit.text()

        # For simplicity, let's assume login is successful if username and password are not empty
        login_successful = bool(username and password)

        if login_successful:
            self.LOGIN_CODE = 1
            self.update_status("Login successful!", "green")
            self.show_main_content()
        else:
            self.LOGIN_CODE = -1
            QMessageBox.warning(self, 'Login Failed', 'Invalid username or password. Please try again.')

    def show_main_content(self):
        # Switch to the main content layout
        self.view.setLayout(self.mainLayout)
        self.setCentralWidget(self.view)


    def update_status(self, text, color):
        self.status.setStyleSheet("color: {}".format(color))
        self.status.setText(text)

    def min_window(self):
        self.setWindowState(Qt.WindowMinimized)


    def closeEvent(self, event):
        try:
            if hasattr(self, "confWindow"):
                self.confWindow.close()
            super(QMainWindow, self).closeEvent(event)
        except Exception as e:
            print(e)

main.py:

# Press the green button in the gutter to run the script.
if __name__ == '__main__':
    # initialize QApplication object, app is the application itself
    app = QApplication([])
    # process any pending events while splashScreen is displayed
    app.processEvents()
    # create main window
    window = MainWindow()
    window.show()
    sys.exit(app.exec_())

Upon googling, I have tried two approaches:

  1. used self.refresh() and self.update() inside handle_login function to delete the layout of the widget first, then add a new layout. This approach did nothing.

  2. tried using QStackedLayout, but it only takes QWidget object rather than layout objects. I would like to stack my two layouts (loginLayout and mainLayout) so I could toggle between them later.

Upvotes: 0

Views: 30

Answers (0)

Related Questions