79130368

Date: 2024-10-27 10:40:48
Score: 1
Natty:
Report link

I eventually found some answer to my question at https://qtcentre.org/threads/21332-ItemIsUserCheckable-and-checking-all-selected-items. Here are some considerations at this topic:

Solution

  1. If you work in PySide6-designer (or Qt Creator), select "Promote to..." option for QListView and create transformed class, which inherits from QListView (otherwise, just create your custom class, which inherits from QListView),
  2. Reimplement keyPressEvent() method there. E. g., following way:
  def keyPressEvent(self, event: QKeyEvent):
    if event.key() == Qt.Key.Key_Space:
      self._checkAllSelected()
    else:
      super().keyPressEvent(event)

Here _checkAllSelected() is new function, which would handle checking of items. I implemented it following way:

  def _checkAllSelected(self):
    model = self.model()
    indexes = self.selectedIndexes()
    setCheckState = Qt.CheckState.Unchecked
    for index in indexes:
      if model.data(index, Qt.ItemDataRole.CheckStateRole) == Qt.CheckState.Unchecked:
        setCheckState = Qt.CheckState.Checked
        break
    for index in indexes:
      model.setData(index, setCheckState.value, Qt.ItemDataRole.CheckStateRole)

Now checking of items in case of multiple selection can be managed.

Reasons:
  • Long answer (-1):
  • Has code block (-0.5):
  • User mentioned (1): @musicamante
  • Self-answer (0.5):
  • Low reputation (1):
Posted by: IvanS