PyQt: Overloading/Ignoring events on widgets

You know when you have all these widgets laid out in your class, and you are hooking up all the connections, and you say “Aw dammit I have to subclass QLabel now just so make it ignore blahEvent”? You end up with all these little widget subclasses, where all they are doing is ignoring an event.

I noticed I was doing this a few times, in more than one of my classes, and finally got annoyed for the last time. I figured there had to be a simple way of just overloading the method on the normal object when I create an instance. Fortunately python considers everything objects and pretty much anything can be changed. So I did this:

myLabel = QLabel()
myLabel.mousePressEvent = lambda event: event.ignore()

Magic.

I have also had to make clickable widgets, such as QLabel:

myLabel = QLabel()
myLabel.mousePressEvent = lambda event: myLabel.emit(SIGNAL("clicked"))

Or if you had to do more than just a single statement:

myLabel = QLabel()
def clickedEvent(event):
myLabel.emit(SIGNAL("clicked"))
# do other stuff
# do stuff
event.accept()
myLabel.mousePressEvent = clickedEvent

I like this better than piling up subclasses that don’t do much.

w