Skip to content
Advertisement

How to get the text of the clicked item of Listwidget in PyQt5?

I was looking for a method to get a text/ name of the clicked element on a listWidget.

This was my approach looks like:

        # call lamp clicked event
    self.listWidget_lamps.itemClicked.connect(self.lamp_clicked)

    # call group clicked event


def lamp_clicked(self):
    self.lamp_on = True
    self.group_on = False
    lamp = Lamp(self.item.text())
    print("lamp" + self.item.text() + "got clicked")

but it always crashes and gives me this error:

lamp = Lamp(self.item.text())
AttributeError: 'MainWindow' object has no attribute 'item'

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.

def lamp_clicked(self, clickedItem):
    self.lamp_on = True
    self.group_on = False
    lamp = Lamp(clickedItem.text())
    print("lamp" + clickedItem.text() + "got clicked")

should do the trick.

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement