为什么 QML 列表视图委托不会立即对选择更改做出反应?



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

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级别上进行选择来管理多选的内容。我已经撞了一段时间了。

谢谢!

问题是,通过更改selection Propery的子专业,selection属性的更改信号本身不会发出。

QML绑定机制仅在属性本身的价值变化时起作用。但是在您的情况下,selection点永远不会更改的对象,因此如果selection的某些子专业更改。

作为解决方法,您可以重新分配/刷新整个选择对象,一旦其任何子专业都会改变。

最新更新