Alex Jenter
Alex Jenter

Reputation: 4432

Why QML ListView delegate doesn't react on selection changes immediately?

I wonder why this doesn't work?

ListView {
   id: root
   property var selection: QtObject {}
   ...
   delegate: MyView {
       focused: ListView.isCurrentItem
       selected: root.selection[index] === true

       Rectangle {
          id: selectionOverlay
          anchors.fill: parent
          color: "red"
          opacity: 0.3
          visible: selected
      }

   }

   Keys.onPressed: if (event.key === Qt.Key_Space) {
                        if(!root.selection[root.currentIndex])
                           root.selection[root.currentIndex] = true;
                       else 
                           root.selection[root.currentIndex] = false;
    }
}

Namely, the delegate doesn't react on the changes in the selection object. Only when delegate for the index is recreated can the selection be seen (e.g. when scrolling far enough and back).

Changing root.selection[index] to ListView.view.selection[index] doesn't help either.

I need to have selection on the ListView level to manage multi-select stuff. Have been banging my head for some time.

Thanks!

Upvotes: 0

Views: 825

Answers (1)

frogatto
frogatto

Reputation: 29285

The problem is that by changing a subproperty of selection propery, the changed signal for the selection property, itself won't be emitted.

QML binding mechanism only works if the value of property itself changes. But in your case, the object to which selection points never change, so you can't be notified if some subproperty of selection changes.

As a workaround you can re-assign/refresh the whole selection object, once any of its subproperties changes.

Upvotes: 1

Related Questions