سؤال

I was wondering how to connect two widgets together. I have two widgets i created on QtDesigner, one being a Login page, and another being a Main Menu. I want to connect them so that when the Login is successful, the user will be redirected to the Main Window, and the Login widget will close automatically. Does anyone know how to do this?

PS. I have the code for the main menu and login in separate .py files

هل كانت مفيدة؟

المحلول

You could do the following:

In your QApp create first a dialogue containing the login widget and execute the dialogue. Depending on the result, either (if login failed) quit the application (or reprompt the user for login), or (if login succeeded) instantiate the main window and show it.

Or: Instantiate and show the main window. Immediately show an application-modal dialogue with the login widget. Depending on the result, either resume operation or quit the application.

Here a snippet from some code, that prompts the user for login and checks it against a DB. The login dialogue is defined as DlgLogin in another file.

#many imports
from theFileContainingTheLoginDialogue import DlgLogin

class MainWindow (QtGui.QMainWindow):
    def __init__ (self, parent = None):
        super (MainWindow, self).__init__ ()
        self.ui = Ui_MainWindow ()
        self.ui.setupUi (self)
        dlg = DlgLogin (self)
        if (dlg.exec_ () == DlgLogin.Accepted):
            #check here whether you accept or reject the credentials
            self.database = Database (*dlg.values)
            if not self.database.check (): sys.exit (0)
        else:
            sys.exit (0)
        self.mode = None

The dialogue class is the following (having two line-edit-widgets for the credentials):

from PyQt4 import QtGui
from dlgLoginUi import Ui_dlgLogin

class DlgLogin (QtGui.QDialog):
    def __init__ (self, parent = None):
        super (DlgLogin, self).__init__ ()
        self.ui = Ui_dlgLogin ()
        self.ui.setupUi (self)

    @property
    def values (self):
        return [self.ui.edtUser.text (), self.ui.edtPassword.text () ]

The application itself reads:

#! /usr/bin/python3.3

import sys
from PyQt4 import QtGui
from mainWindow import MainWindow

def main():
    app = QtGui.QApplication (sys.argv)
    m = MainWindow ()
    m.show ()
    sys.exit (app.exec_ () )

if __name__ == '__main__':
    main ()
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top