从第二个QDialog调用QMainWindow

2024-05-15 17:40:23 发布

您现在位置:Python中文网/ 问答频道 /正文

我的PyQt应用程序从登录屏幕开始。如果密码正常,将显示模块屏幕(带图标)。当用户单击某个按钮时,将出现一个QMainWindow。但我不能这样做,因为qmainwindow对象没有属性''u exec'错误。这是我的代码:

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

我做错什么了?非常感谢。在


Tags: selfnone屏幕initmaindefloginclass
2条回答

除了应用程序对象和QDrag,请假设{}不存在。这是一个完全令人困惑的方法,根本不必使用。尤其是那些不熟悉Qt的人。在

如果要显示任何小部件,只需show()它。如果您想在对话框被接受时得到通知,请将一些代码连接到其accepted()信号。这就是全部。在

最近我也做过类似的事工作:我有一个登录窗口和一个主窗口,我用一个类似FMS的东西在登录窗口和主窗口之间切换。 假设我们有3个状态:登录,main,退出。在

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

相关问题 更多 >