PyQt: How can I quit a QDialog?
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()
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 becomes exec_
's result).
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 of form.exec_()
. The latter is normally used to display the dialog modally over a parent window.
精彩评论