How to communicate or switch between two windows in PyQt?

后端 未结 5 1520
孤独总比滥情好
孤独总比滥情好 2020-12-17 06:38

I am developing an application using python and Qt.

I have designed 2 Main windows ie..QMainWindow (Not QWidget or QDialog) using Qt.

Let it be.

1.

5条回答
  •  感动是毒
    2020-12-17 07:10

    Regardless of your description, I think your LoginWindow should be a QDialog, and your StuffWIndow be the MainWindow, and function like this...

    1. Your StuffWindow MainWindow should be created (not shown)
    2. Call a login() method that creates and exec_() your login QDialog as a application MODAL dialog
    3. Start the app.exec_() event loop now, and wait for the user to interact with login
    4. User interacts with login dialog, and the result of the dialog closing will then allow your app to check its values and choose to show its main interface.

    Here is a quick outline:

    class MainWindow():
    
        def login():
            loginDialog = LoginDialog()
    
            # this is modal. wait for it to close
            if loginDialog.exec_():
                # dialog was accepted. check its values and maybe:
                self.show()
    
            else:
                # maybe reshow the login dialog if they rejected it?
                loginDialog.exec_()
    
    
    if __name__ == "__main__":
    
        app = QApp
        win = MainWindow()
        win.login()
        app.exec_()
    

提交回复
热议问题