I was looking for a method to get a text/ name of the clicked element on a listWidget.
This was my approach looks like:
JavaScript
x
12
12
1
# call lamp clicked event
2
self.listWidget_lamps.itemClicked.connect(self.lamp_clicked)
3
4
# call group clicked event
5
6
7
def lamp_clicked(self):
8
self.lamp_on = True
9
self.group_on = False
10
lamp = Lamp(self.item.text())
11
print("lamp" + self.item.text() + "got clicked")
12
but it always crashes and gives me this error:
JavaScript
1
3
1
lamp = Lamp(self.item.text())
2
AttributeError: 'MainWindow' object has no attribute 'item'
3
Could someone please tell me what Im doing wrong?
Advertisement
Answer
The signature of your slot lamp_clicked
is wrong. Take a look at QListWidget::itemClicked and note that the signal has one parameter but your slot takes no parameter.
JavaScript
1
6
1
def lamp_clicked(self, clickedItem):
2
self.lamp_on = True
3
self.group_on = False
4
lamp = Lamp(clickedItem.text())
5
print("lamp" + clickedItem.text() + "got clicked")
6
should do the trick.