0

我已经构建了一个 QDialog 小部件。我的问题是,我无法退出 QDialog。如果我按下其中一个按钮,则 QDialog 仅设置为“隐藏”。这是代码的一小部分。它是可执行的。我不知道我做错了什么。也许你们中的一个可以告诉我。

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

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

        # init
        # ------------------------------------------------
        self.setMinimumWidth(600)
        self.setWindowTitle("Select Dingsda")
        self.layout = QVBoxLayout()
        self.setLayout(self.layout)
        self.layoutWidget = QWidget(self)
        self.liste = []
        # widgets and layouts
        # ------------------------------------------------

        tempLayout = QHBoxLayout()
        self.cancelButton = QPushButton("Cancel")
        self.connect(self.cancelButton, SIGNAL('clicked()'), self.cancel)
        self.addSelectedButton = QPushButton("Add Selected")
        self.connect(self.addSelectedButton, SIGNAL('clicked()'), self.addSelected)
        tempLayout.addStretch()
        tempLayout.addWidget(self.cancelButton)
        tempLayout.addWidget(self.addSelectedButton)
        self.layout.addLayout(tempLayout)

        # test-data
        # ------------------------------------------------
    # methods
    # ------------------------------------------------

    def cancel(self):
        self.close()

    def addSelected(self):
        self.liste = ["1", "2", "3", "4", "5"]
        self.accept()


    def exec_(self):
        if QDialog.exec_(self) == QDialog.Accepted:
            return  self.liste
        else:
            return []

def test():    
    app = QApplication([""])
    form = MyClass()
    i = form.exec_()
    print i
    sys.exit(app.exec_())
#-------------------------------------------------------------------------------
# main
#-------------------------------------------------------------------------------
if __name__ == "__main__":
    test()
4

2 回答 2

3

要终止对话,accept应该可以工作(至少如果您已将对话设为模态,我相信exec_总是如此)。

正常的选择是拒绝;或者,您可以使用参数调用doneint(它成为exec_'s 结果),而不是其中一个或两个。

于 2010-08-23T04:23:16.353 回答
2

我根本不知道 python,但看起来对话框是您的应用程序的唯一窗口。您可能想尝试使用form.show_()而不是调用对话框form.exec_()。后者通常用于在父窗口上模态显示对话框。

于 2010-08-23T04:21:08.733 回答