PyQt:如何退出 QDialog?
我已经构建了一个 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()
I've build a QDialog Widget. My problem is, I can't quit the QDialog.
If I press one of the buttons, then the QDialog is only set to "hide".
Here is a little part of the code. It is executable.
I don't know what I'm doing wrong. Maybe one of you can tell me.
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()
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
要终止对话框, accept 应该可以(至少如果您已将对话框设置为模式,我相信
exec_
总是这样做)。正常的替代方案是 reject;或者,您可以调用 done,而不是其中之一或两者都调用 带有
int
参数(成为exec_
的结果)。To terminate a dialog, accept should work (at least if you've made your dialog modal, which I believe
exec_
always does).The normal alternative is reject; or, instead of either or both, you could call done with an
int
parameter (which becomesexec_
's result).我根本不了解 python,但看起来该对话框是您的应用程序的唯一窗口。您可能想尝试使用
form.show_()
而不是form.exec_()
调用对话框。后者通常用于在父窗口上以模式方式显示对话框。I don't know python at all but it looks like the dialog is the only window for your app. You may want to try invoking the dialog with
form.show_()
instead ofform.exec_()
. The latter is normally used to display the dialog modally over a parent window.