qtpyqtpysideqlistwidget

qt listwidget clear() unexpectedly trigger itemSelectionChanged()


in PyQt(Pyside6), I created a QListWidget:

the 'show_abc' button simple code is as bellow, ('show_123' is similar)

listWidget1.clear()
listWidget1.addItems(['a','b','c'])

and pseudo code for updating Label is

listWidget1.itemSelectionChanged.connect(update_label)

BUT, I found the "listWidget1.clear()" will trigger "listWidget1.itemSelectionChanged", so to call update_label. This is unexpected, I just want to 'clear&addItems' to refresh. how could I solve this problem


Solution

  • I added "setCurrentItem(None)" before clear() to solve it, not found some more elegant way.

    listWidget1.setCurrentItem(None)
    listWidget1.clear()
    listWidget1.addItems(...)