How to execute a callback when a QDialog is shown in PyQt4?

2.7k views Asked by At

I'd like to be able to execute a callback when a QDialog is shown in PyQt4, preferably via the signal/slot mechanism. Looking at the PyQt documentation on QDialog, I can't find the correct signal to which to attach the slot that I want run.

What is a good way to do this?

2

There are 2 answers

0
ekhumoro On BEST ANSWER

If you want a signal to be emitted every time the dialog is shown, you could create a class like this:

class Dialog(QtGui.QDialog):
    dialogShown = QtCore.pyqtSignal()

    def showEvent(self, event):
        super(Dialog, self).showEvent(event)
        self.dialogShown.emit()

and then use it like this:

    self.dialog = Dialog()
    self.dialog.dialogShown.connect(self.handleDialogShown)
5
DRC On

Would be fine in my opinion to override the show or showEvent method and put your callbacks there.

class MyDialog(QtGui.QDialog):
    def show(self):
        super(MyDialog, self).show()
        callbacks()