1

I have a list of QLabel and want to learn which QLabel clicked . When i look for the making QLabel clickable , this code has worked :

 labels[i].mousePressEvent = self.print_some
 def print_some(self, event):
    print("Clicked")

But I didn't figure out which object clicked . How can i do that ?

Cahit Yıldırım
  • 499
  • 1
  • 10
  • 26

1 Answers1

4

You can easily make custom receivers for events, which would contain the event source information:

import functools

labels[i].mousePressEvent = functools.partial(self.print_some, source_object=labels[i])

def print_some(self, event, source_object=None):
    print("Clicked, from", source_object)
mguijarr
  • 7,641
  • 6
  • 45
  • 72
  • This is a good solution but the `event` and `source_object` need to be swapped in the method signature. – Martin Valgur Dec 29 '15 at 22:00
  • I have tried and prints 'Clicked from ' this . When i want to print label text like ' functools.partial(self.print_some, labels[i].text()) ' also prints same above . How to receive label attributes ? – Cahit Yıldırım Dec 29 '15 at 22:08
  • I fixed the code - the second argument to the function has to be a keyword argument, check it now – mguijarr Dec 29 '15 at 22:10