[SOLVED]Login dialog ?



  • I nearly finished my application, when the customer asked if I could implement some kind of login form on application startup.

    So far I have designed the UI, and tinkered about the actual execution. Username and password are irrelevant for now.

    My dillema is: which window should be the parent? Login form (QDialog) or MainWindow (QMainWindow). This is the code which shows the Login form. I want it to show MainWindow when I click "Login button"

    @class Login(QtGui.QDialog):
    def init(self,parent=None):
    QtGui.QWidget.init(self,parent)
    self.ui2=Ui_dlgLogovanje()
    self.ui2.setupUi(self)

        QtCore.QObject.connect(self.ui2.buttonLogin, QtCore.SIGNAL("clicked()"), self.doLogin)
    
    def doLogin(self):
        main = Window()
        main.show()
    

    class Window(QtGui.QMainWindow):
    def init(self,parent=None):
    QtGui.QWidget.init(self,parent)
    self.ui=Ui_MainWindow()
    self.ui.setupUi(self)
    ........# irrelevant code removed

    if name=="main":
    program = QtGui.QApplication(sys.argv)
    myprogram = Login()
    myprogram.show()
    sys.exit(program.exec_())@

    This gets me the Login dialog TWICE :) Is this even the right way to do this? What should I do?

    Thank you all in advance.



  • I think you should make three classes - Login, MainWindow and Execution.
    In Execution, you should track the login state(is someone logged in; if yes, then who, and so on). It should have one instance of MainWindow and one of login. After login is done, MainWindow is shown and Login is hidden and vice-versa.

    OR

    Login should be initialized in __ init __ of MainWindow before everything else. Everything else should be initialized only on a successful login. The state can be tracked by variables inside MainWindow and Login called again when required.



  • Thank you very much for your reply. I will try it and update this thread accordingly.

    :edit:

    I'm really not sure how to implement your second solution, which seems less complicated :/ One guy from stackoverflow suggested this:

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

    So you just need to check the dialog's return code to decide whether the main application should be run or not:

    program = QtGui.QApplication(sys.argv)
    if Login().exec_() == QtGui.QDialog.Accepted:
    myprogram = Window()
    myprogram.show()
    sys.exit(program.exec_())@

    which works by itself, but... I need to check if USER and PASSWD variables match to something, and then emit that signal. Which again, I'm not sure how.



  • Login form is shown. If correct username and password are entered, then main window is shown and working. But, the login form stays active, and if I close it, the main window will also close.

    Indentation might be a little off, just a pasting issue.

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



  • You can show/hide QDialog(or any other QWidget) by using setVisible(bool) method... so you can call it on successful login to hide the login form and redisplay it on logout...

    Also, in my previous post's second option, what I was trying to say was :
    @
    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)
    

    create a login form with MainWindow as parent

    so that closing it doesn't close ManiWindow

          self.loginForm = Login(self)
          self.loginForm.show()
          self.loginForm.raise_() #mind the '_'
    

    connect the accepted and rejected signals

    of loginForm and on successful login

    setup the mainwindow

    self.ui=Ui_MainWindow()

    self.ui.setupUi(self)

    if __name__=="__main__":
    

    start-up code here

    @
    This will need some changes... i copy pasted your code and edited it... so some more additions/subtractions/edits will be needed... this is just an idea, not the proper implementation

    P.S. - Take a look at the "QWidget Documentation":www.pyside.org/docs/pyside/PySide/QtGui/QWidget.html for some useful methods which can be used with any derived widget(the docs contain a list of all such widgets)



  • I am very thankful for your posts! Will try it a little later for sure.



  • This is the solution I was looking for, only simplified, so I could understand parent-child relations, and subclassing

    @from PyQt4 import QtGui

    class Login(QtGui.QDialog):
    def init(self):
    QtGui.QDialog.init(self)
    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):
    QtGui.QMainWindow.init(self)

    if name == 'main':

    import sys
    app = QtGui.QApplication(sys.argv)
    
    if Login().exec_() == QtGui.QDialog.Accepted:
        window = Window()
        window.show()
        sys.exit(app.exec_())@
    


  • Dear Ivica,

    Would you mind to show your solution in Qt C++? Because I am not familiar with phyton at all.

    Thank you very much in advance.



  • you search a solution in c + +?



  • c++ using qt.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.