PyQT中的多对话框程序不会关闭

时间:2010-06-18 20:18:40

标签: user-interface qt pyqt pyqt4 multiple-forms

对于我的项目,我需要多个对话框相互链接。一个按钮会转到一个级别,另一个按钮会回到两个级别。为了基本了解我正在寻找的内容而不显示我的所有代码,这里有一个可编辑的例子:

'''
Created on 2010-06-18

@author: dhatt
'''

import sys
from PyQt4 import QtGui, QtCore

class WindowLV3(QtGui.QDialog):
    def __init__(self, parent=None):
        QtGui.QWidget.__init__(self, parent)

        self.setGeometry(300, 300, 120, 150)
        self.setWindowTitle('LV3')

        quit = QtGui.QPushButton('Close', self)
        quit.setGeometry(10, 10, 60, 35)

        self.connect(quit, QtCore.SIGNAL('clicked()'),
            QtGui.qApp, QtCore.SLOT('quit()'))  # this will close entire program


class WindowLV2(QtGui.QDialog):
    def __init__(self, parent=None):
        QtGui.QWidget.__init__(self, parent)
        self.Window3 = WindowLV3()

        self.setGeometry(300, 300, 120, 150)
        self.setWindowTitle('LV2')

        quit = QtGui.QPushButton('Close', self)
        quit.setGeometry(10, 10, 60, 35)

        next = QtGui.QPushButton('Lv3', self)
        next.setGeometry(10, 50, 60, 35)

        self.connect(quit, QtCore.SIGNAL('clicked()'),
            QtGui.qApp, QtCore.SLOT('reject()'))  # this doesn't work

        self.connect(next, QtCore.SIGNAL('clicked()'),
            self.nextWindow)

    def nextWindow(self):
        self.Window3.show()


class WindowLV1(QtGui.QDialog):
    def __init__(self, parent=None):
        QtGui.QWidget.__init__(self, parent)
        self.Window2 = WindowLV2()

        self.setGeometry(300, 300, 120, 150)
        self.setWindowTitle('LV1')

        next = QtGui.QPushButton('Lv2', self)
        next.setGeometry(10, 50, 60, 35)

        quit = QtGui.QPushButton('Close', self)
        quit.setGeometry(10, 10, 60, 35)

        self.connect(next, QtCore.SIGNAL('clicked()'),
            self.nextWindow)

    def nextWindow(self):
        self.Window2.show()

        self.connect(quit, QtCore.SIGNAL('clicked()'),
            QtGui.qApp, QtCore.SLOT('reject()'))  # this doesn't work        


if __name__ == '__main__':
    app = QtGui.QApplication(sys.argv)
    Window1 = WindowLV1()
    Window1.show()
    sys.exit(app.exec_())

问题是我无法从窗口关闭并显示上一个窗口。例如,如果我点击LV3窗口内的“CLOSE”按钮,它会将控制权转移回LV2窗口。我可以调用QtCore.SLOT('quit()')),但它会关闭整个程序,我不希望这样。

我在这里做错了什么?

1 个答案:

答案 0 :(得分:1)

这里有两件事需要解决。

  1. 您只需在connect方法中调用QDialog.close即可。

  2. def nextWindow(self):中,您正在尝试连接本地变量quit。所以它不会起作用。您需要将quit定义为实例变量(self.quit)

    self.connect(self.quit,QtCore.SIGNAL('clicked()'),                 self.close)#the this应该起作用

  3. 以下是修改后的代码:

    import sys
    from PyQt4 import QtGui, QtCore
    
    class WindowLV3(QtGui.QDialog):
        def __init__(self, parent=None):
            QtGui.QWidget.__init__(self, parent)
    
            self.setGeometry(300, 300, 120, 150)
            self.setWindowTitle('LV3')
    
            self.quit = QtGui.QPushButton('Close', self)
            self.quit.setGeometry(10, 10, 60, 35)
    
            self.connect(self.quit, QtCore.SIGNAL('clicked()'),
                self.close)  # this will close entire program
    
    
    class WindowLV2(QtGui.QDialog):
        def __init__(self, parent=None):
            QtGui.QWidget.__init__(self, parent)
            self.Window3 = WindowLV3()
    
            self.setGeometry(300, 300, 120, 150)
            self.setWindowTitle('LV2')
    
            self.quit = QtGui.QPushButton('Close', self)
            self.quit.setGeometry(10, 10, 60, 35)
    
            next = QtGui.QPushButton('Lv3', self)
            next.setGeometry(10, 50, 60, 35)
    
            self.connect(self.quit, QtCore.SIGNAL('clicked()'),
                self.close)  # this should work
    
            self.connect(next, QtCore.SIGNAL('clicked()'),
                self.nextWindow)
    
        def nextWindow(self):
            self.Window3.show()
    
    
    class WindowLV1(QtGui.QDialog):
        def __init__(self, parent=None):
            QtGui.QWidget.__init__(self, parent)
            self.Window2 = WindowLV2()
    
            self.setGeometry(300, 300, 120, 150)
            self.setWindowTitle('LV1')
    
            next = QtGui.QPushButton('Lv2', self)
            next.setGeometry(10, 50, 60, 35)
    
            self.quit = QtGui.QPushButton('Close', self)
            self.quit.setGeometry(10, 10, 60, 35)
    
            self.connect(next, QtCore.SIGNAL('clicked()'),
                self.nextWindow)
    
        def nextWindow(self):
            self.Window2.show()
    
            self.connect(self.quit, QtCore.SIGNAL('clicked()'),
                self.close)  # this should work        
    
    
    if __name__ == '__main__':
        app = QtGui.QApplication(sys.argv)
        Window1 = WindowLV1()
        Window1.show()
        sys.exit(app.exec_())