Search code examples
pythonqtpyqtpyqt4

Calling QMainWindow From Second QDialog


My PyQt application starts with Login screen. If password OK, a module-screen (with icons) appears. When user click some button, a QMainWindow will appears. But I can't do this because of qmainwindow object has no attribute '_exec' error. This is my code:

from PyQt4.QtGui import *
from PyQt4.QtCore import *

class Main(QMainWindow):
    def __init__(self, parent=None):
        super(Main, self).__init__(parent)
        ...
        ...

class Login(QDialog):
    def __init__(self, parent=None):
        super(Login, self).__init__(parent)
        ...
        ...

uyg=QApplication(sys.argv)

class icons(QDialog):
    def __init__(self, parent=None):
        super(icons, self).__init__(parent)
        ...
        self.buton = QPushButton()
        self.buton.pressed.connect(self.open)
        ...
    def open(self):
        dialogmain = Main()
        dialogmain._exec() #or dialogmain.show() ???
        self.accept()
        self.close()
        uyg.exec_()    

if Login().exec_() == QDialog.Accepted:
    dialog = icons()
    dialog.exec_()
else:
    uyg.quit()

What am I doing wrong? Thank you.


Solution

  • Lately i have done the similar work:I have a loging window and a main window ,and I used something like a FSM to switch between the loging and main window. Let's say we have 3 state:loging,main,quit.

    STATE_LOGING = 0
    STATE_MAIN = 1
    STATE_QUIT = 2
    STATE_DESTROY = 3    #this is a flag
    
    class CState():
        sigSwitchState = pyqtSignal(int)
        def __init__(self):
            super(CState,self).__init__()
        
        def start(self):
            pass
    
        def sendQuit(self,nextstate):
            self.sigSwitch.emit(nextstate)
    
    class CLoginState(CState):
        def __init__(self):
            super(CLoginState,self).__init__()
    
        def start(self):
            w = Loging()
            w.show()
    
        def whenPasswdOk(self):
            self.sendQuit(STATE_MAIN)
    
    class CMainState(CState):
        def __init__(self):
            super(CMainState,self).__init__()
     
        def start(self):
            w = MainWindow()
            w.show()
    
        def whenMainWindowQuit(self):
            self.sendQuit(STATE_QUIT)
    
    class CQuitState(CState):
        def __init__(self):
            super(CQuitState,self).__init__()
    
        def start(self):
            #do some clean stuff...
            pass
        def whenCleanDone(self):
            self.sendQuit(STATE_DESTROY)
    
    class CMainApp():
        def __init__(self):
            self.app = QApplication(sys.argv)
            
        def __CreateState(state):
            if state == STATE_LOGING:
                s = CLoginState()
            if state == STATE_MAIN:
                s = CMainState()
            #... same as other state
            s.sigSwitchState.connect(self.procNextState)
    
       def procNextState(self,state):
           if state == STATE_DESTROY:
                QApplication().exit()
    
           s = self.__CreateState(state)
           s.start()
        
        def run(self):
            self.procNextState(STATE_LOGING)
            sys.exit(self.app.exec_())
    
    if __name__ == "__main__":
        app = CMainApp()
        app.run()