为什么QML ListView委托不立即对选择更改做出反应?

问题描述 投票:0回答:1

我想知道为什么这不起作用?

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;
    }
}

即,委托对选择对象的更改没有反应。仅当重新创建索引的委托时才能看到选择(例如,当滚动得足够远和后退时)。

root.selection[index]改为ListView.view.selection[index]也无济于事。

我需要在ListView级别上进行选择以管理多选项。一直在敲打我的脑袋。

谢谢!

qt listview qml selection
1个回答
1
投票

问题是通过改变selection属性的子属性,selection属性的改变信号本身将不会被发出。

QML绑定机制仅在属性本身的值发生变化时才有效。但在你的情况下,selection指向的对象永远不会改变,因此如果selection的某些子属性发生变化,则无法通知您。

作为一种解决方法,一旦其任何子属性发生更改,您就可以重新分配/刷新整个选择对象。

© www.soinside.com 2019 - 2024. All rights reserved.