Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 Python 2.6, latest PyQt4.

like image 691
Claudiu Avatar asked May 03 '11 18:05

Claudiu


People also ask

How to add a dialog box in PyQt5?

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.

How to close a dialog in PyQt5?

To close a window in PyQt5 we use the . close() method on that window.

What is Qdialog in Python?

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() .


2 Answers

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.

like image 164
voithos Avatar answered Sep 22 '22 00:09

voithos


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.

like image 40
Andrew Ring Avatar answered Sep 23 '22 00:09

Andrew Ring