was successfully added to your cart.
Uncategorized

Programatically Select Item in QListWidget (PySide)

By May 20, 2017 No Comments

Today I was working on an updated version of my tpNamingManager tool (https://github.com/tpoveda/tpNamingManager) and I have noticed a weird behaviour when trying to select QListWidget items programatically.

For example, let’s say that we have a button in our UI and when is pressed we add a new item to a list (in my case, rulesList). We want that after pressing the button, select automatically that item in our list.

item = QListWidgetItem ("Test Item")
item.setFlags (item.flags () | Qt.ItemIsEditable)
self.rulesList.addItem (item)

# OPTION A) THIS OPTION WORKS VISUALLY BUT IT DOES NOT SET THE CURRENT ITEM
# self.rulesList.item(self.rulesList.count()-1).setSelected(True)
# currItem = self.rulesList.currentItem()       # Returns None

# OPTION B) SIMPLER AND IT WORKS PERFECTLY!
self.rulesList.setCurrentItem(item)
currItem = self.rulesList.currentItem()         # Returns correct object!

Leave a Reply