[PyQt] events / signals

fpp fpp.gsp at gmail.com
Thu Aug 26 18:38:32 BST 2010


On Wed, Aug 25, 2010 at 10:43 PM, Eric Frederich
<eric.frederich at gmail.com> wrote:
> If I find myself subclassing a widget just to get something to happen on an
> event am I doing something wrong?
> Is there another way to accomplish this?
> Is there a way to use an event like a signal in a self.connect?
> For example... If I want so do something when a label is clicked I have done
> the following and then just connect to the signal I emmit.
> class ClickableLabel(QLabel):
>     def __init__(self, *args, **kwargs):
>         super(ClickableLabel, self).__init__(*args, **kwargs)
>     def mousePressEvent(self, event):
>         self.emit(SIGNAL("clicked"))

I guess it depends... If you have only one such label in your app, you
don't need a subclass or even a signal.

You can just assign your own function to the method of a QLabel instance, as in:

self.myClickableQLabel.mousePressEvent = self.on_QLabel_clicked

then define what happens there:

def on_QLabel_clicked(self, event):
   print "Aha!"

Then again, if you need several such labels, creating a subclass
avoids code duplication.


More information about the PyQt mailing list