You are currently browsing comments. If you would like to return to the full story, you can read the full entry here: “PyQt: Overloading/Ignoring events on widgets”.
You are currently browsing comments. If you would like to return to the full story, you can read the full entry here: “PyQt: Overloading/Ignoring events on widgets”.
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