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.mousePressEvent = lambda event: event.ignore()
Magic.
I have also had to make clickable widgets, such as QLabel:
myLabel.mousePressEvent = lambda event: myLabel.emit(SIGNAL("clicked"))
Or if you had to do more than just a single statement:
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.






AMAZING! Thanks a lot!
Hi,
I have tried your usage of Lambda function, which would be perfect for me, but I get into problems.
here is the part of the code:
##############################################
for i in range(0,12):
str = ‘imageLabel_%s’ % i
print self.__dict__[str]
if self.position == len(self.listing)-1:
self.position = 0
else:
label = self.__dict__[str]
label.setPixmap(QtGui.QPixmap(self.directory + self.listing[self.position]))
label.mousePressEvent = lambda: label.emit(SIGNAL(“clicked”))
self.position += 1
def buttonClicked(self):
print ‘Button Clicked’
##############################################
when I click on the label, I get :
TypeError: () takes no arguments (1 given)
any clue what it could be?
thanks a lot
aatu
Thanks for pointing this out. Actually you found a slight typo in my lambda example! I just corrected that.
Your line should be changed to:
label.mousePressEvent = lambda event: label.emit(SIGNAL(“clicked”))
The reason for this is, all mouse[blah]Event methods expect a QMouseEvent object to be passed in as an argument. It is used to determine the nature of the event by checking things like which mouse button was pressed, any modifier keys held down, mouse cursor position, etc. All of which would be set in the Event object. When you overload an event method you need to make sure you use the same signature as the original method: mousePressEvent(event)
You don’t have to do anything with the event object if you don’t want to, which defaults to calling event.ignore() I think. And it would be possible that the click could propagate up to the next parent to see if it wants to handle the click instead. If you need to call event.accept() to make sure the click doesn’t go any further than the current widget, then you would need to use the other example of defining a function that also calls event.accept()
Sorry for that typo!
TypeError: ” () takes no arguments (1 given)
ok, into the first argument is written lambda, it is removed while submitting comment
sorry for the spamming