Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 820
Juan Carlos Coto Avatar asked Oct 18 '25 11:10

Juan Carlos Coto


2 Answers

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)
like image 71
ekhumoro Avatar answered Oct 21 '25 10:10

ekhumoro


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()
like image 20
DRC Avatar answered Oct 21 '25 10:10

DRC



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!