PyQt Login Dialog

I almost finished my application when a client asked if I could implement some kind of registration form when starting the application.

So far I have developed a user interface and worked hard on the actual implementation. Username and password are no longer relevant.

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_()) 

The login form is displayed. If the correct username and password are entered, the main window will appear and work. But the login form remains active, and if I close it, the main window will also close.

+8
python login qt dialog pyqt
source share
1 answer

A QDialog has its own event loop, so it can be run separately from the main application.

So you just need to check the return code in the dialog box to decide whether to run the main application or not.

Code example:

 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_()) 
+23
source share

All Articles