16

当客户问我是否可以在应用程序启动时实现某种登录表单时,我几乎完成了我的应用程序。

到目前为止,我已经设计了 UI,并对实际执行进行了修改。用户名和密码暂时无关紧要。

class Login(QtGui.QDialog):
    def __init__(self,parent=None):
        QtGui.QWidget.__init__(self,parent)
        self.ui=Ui_dlgLogovanje()
        self.ui.setupUi(self)

        QtCore.QObject.connect(self.ui.buttonLogin, QtCore.SIGNAL("clicked()"), self.doLogin)

    def doLogin(self):
        name = str(self.ui.lineKorisnik.text())
        passwd = str(self.ui.lineSifra.text())
        if name == "john" and passwd =="doe":
            self.runIt()
        else:
            QtGui.QMessageBox.warning(self, 'Greška',
        "Bad user or password", QtGui.QMessageBox.Ok)           

    def runIt(self):
        myprogram = Window()        
        myprogram.showMaximized() #myprogram is

class Window(QtGui.QMainWindow):
    def __init__(self,parent=None):
        QtGui.QWidget.__init__(self,parent)
        self.ui=Ui_MainWindow()
        self.ui.setupUi(self)


if __name__=="__main__":
    program = QtGui.QApplication(sys.argv)
    myprogram = Window()
    if Login().exec_() == QtGui.QDialog.Accepted:       
        sys.exit(program.exec_())

显示登录表单。如果输入了正确的用户名和密码,则显示主窗口并正常工作。但是,登录表单保持活动状态,如果我关闭它,主窗口也会关闭。

4

1 回答 1

43

AQDialog有自己的事件循环,因此它可以与主应用程序分开运行。

您只需要检查对话框的返回代码来决定是否应该运行主应用程序。

示例代码(PyQt5):

from PyQt5 import QtWidgets
# from mainwindow import Ui_MainWindow

class Login(QtWidgets.QDialog):
    def __init__(self, parent=None):
        super(Login, self).__init__(parent)
        self.textName = QtWidgets.QLineEdit(self)
        self.textPass = QtWidgets.QLineEdit(self)
        self.buttonLogin = QtWidgets.QPushButton('Login', self)
        self.buttonLogin.clicked.connect(self.handleLogin)
        layout = QtWidgets.QVBoxLayout(self)
        layout.addWidget(self.textName)
        layout.addWidget(self.textPass)
        layout.addWidget(self.buttonLogin)

    def handleLogin(self):
        if (self.textName.text() == 'foo' and
            self.textPass.text() == 'bar'):
            self.accept()
        else:
            QtWidgets.QMessageBox.warning(
                self, 'Error', 'Bad user or password')

class Window(QtWidgets.QMainWindow):
    def __init__(self, parent=None):
        super(Window, self).__init__(parent)
        # self.ui = Ui_MainWindow()
        # self.ui.setupUi(self)

if __name__ == '__main__':

    import sys
    app = QtWidgets.QApplication(sys.argv)
    login = Login()

    if login.exec_() == QtWidgets.QDialog.Accepted:
        window = Window()
        window.show()
        sys.exit(app.exec_())

示例代码(PyQt4):

from PyQt4 import QtGui
# from mainwindow import Ui_MainWindow

class Login(QtGui.QDialog):
    def __init__(self, parent=None):
        super(Login, self).__init__(parent)
        self.textName = QtGui.QLineEdit(self)
        self.textPass = QtGui.QLineEdit(self)
        self.buttonLogin = QtGui.QPushButton('Login', self)
        self.buttonLogin.clicked.connect(self.handleLogin)
        layout = QtGui.QVBoxLayout(self)
        layout.addWidget(self.textName)
        layout.addWidget(self.textPass)
        layout.addWidget(self.buttonLogin)

    def handleLogin(self):
        if (self.textName.text() == 'foo' and
            self.textPass.text() == 'bar'):
            self.accept()
        else:
            QtGui.QMessageBox.warning(
                self, 'Error', 'Bad user or password')

class Window(QtGui.QMainWindow):
    def __init__(self, parent=None):
        super(Window, self).__init__(parent)
        # self.ui = Ui_MainWindow()
        # self.ui.setupUi(self)    

if __name__ == '__main__':

    import sys
    app = QtGui.QApplication(sys.argv)
    login = Login()

    if login.exec_() == QtGui.QDialog.Accepted:
        window = Window()
        window.show()
        sys.exit(app.exec_())
于 2012-08-04T22:53:42.067 回答