I want to create a list of items using QListWidget and set it up so each time I click (select) an item a different method is executed. The way I thought about going about solving this is by connecting the list to a method that will check for the index and depending on the item index it will generate a signal connected to a different method. Thing is, I don't know how to generate this signal if I don't have a button or anything. The method is just checking for item index.
Any suggestions will be much appreciated!
The straightforward idea would be to connect the itemClicked
signal to a slot in which a method is called depending on the clicked index. In this case you don't need a signal for every item.
But if you really want to create a "clicked" signal for each item and connect it to its respective slot(s), you could do it like this:
Subclass the QListWidgetItem
and add a clicked
signal to it. How to add custom signals
While filling the QListWidget, connect each item with its slots depending on its index.
Connect the itemClicked
signal of the QListWidget to a slot, that emits the clicked item's signal like this:
def itemClickedSlot(item):
item.clicked.emit()