pyqt4: how to show a modeless dialog?
for the life of me I can't figure this out... on a button press I have the code:
@QtCore.pyqtSlot():
def buttonPressed(self):
d = QtGui.QDialog()
d.show()
all that happens is a window briefly pops up without any contents and then disappears. Hitting the button repeatedly doesn't help.
Using开发者_JS百科 Python 2.6, latest PyQt4.
If I am not mistaken, it seems that someone else had a similar issue. What seems to be happening is that you define a local variable d
and initialize it as a QDialog
, then show it. The problem is that once the buttonPressed
handler is finished executing, the reference to d
goes out of scope, and so it is destroyed by the garbage collector. Try doing something like self.d = QtGui.QDialog()
to keep it in scope.
You should pass a parent to the dialog when you created it like this:
@QtCore.pyqtSlot():
def buttonPressed(self):
d = QtGui.QDialog(self)
d.show()
This will keep the reference to the QDialog object, keeping it in scope. It also allows proper behavior of the Dialog if you are passing the appropriate QMainWindow, etc. as the parent.
精彩评论