2

I have two QListWidget items:

The bad list, we can only select one item.

self.badList = QtGui.QListWidget()
self.badList.setEditTriggers(QtGui.QAbstractItemView.DoubleClicked)
self.badList.itemSelectionChanged.connect(self.functionToCall1)
self.badList.itemChanged.connect(self.functionToCall2)

The other, i can select many items on it

self.badList.setSelectionMode(QtGui.QAbstractItemView.ExtendedSelection)
self.goodList.itemSelectionChanged.connect(self.functionToCall3)

FunctionToCall2 is called when doubleclicking an item and renaming it, then hitting enter.

Obviously the selectionMode is the problem here. Extended selection allows me to unselect all items when clicking outside all of them in empty space. But if selection is SingleSelection by default, like in the badList, i CAN'T unselect all items clicking outside.

How can I make this possible?

Darkgaze
  • 2,280
  • 6
  • 36
  • 59

1 Answers1

0

I think you can do it by overriding mousePressEvent() function.

In your class which inherited by QListWidget class,

class YourQListWidgetClass(QListWidget):

    def __init__(self,parent):
        super().__init__(parent)
        #add code lines

    def mousePressEvent(self, event):
        super(YourQListWidgetClass, self).mousePressEvent(event)

        if not self.indexAt(event.pos()).isValid():
            self.clearSelection()

Since this forces to clear all selection when clicked position is not valid, I think it will work.