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 Python 2.6, latest PyQt4.
add the following method after the __init__ def button_clicked(self, s): print("click", s) dlg = CustomDialog() if dlg. exec(): print("Success!") else: print("Cancel!") Run it! Click to launch the dialog and you will see a dialog box with buttons.
To close a window in PyQt5 we use the . close() method on that window.
A dialog window is a top-level window mostly used for short-term tasks and brief communications with the user. QDialogs may be modal or modeless. QDialogs can provide a return value , and they can have default buttons . QDialogs can also have a QSizeGrip in their lower-right corner, using setSizeGripEnabled() .
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With