我已经构建了一个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()
有帮助吗?

解决方案

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

其他提示

要终止对话框, 接受 应该起作用(至少如果您制作了对话模式,我相信 exec_ 总是这样)。

正常的选择是 拒绝;或者,您可以打电话给 完毕 带着 int 参数(变成 exec_的结果)。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top