qteventspyqtsignals-slotsqdialog

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


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?


Solution

  • 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)