带有 ExtendedSelection 的 Qml TreeView 中的 selectedRows() 落后了一步

Posted

技术标签:

【中文标题】带有 ExtendedSelection 的 Qml TreeView 中的 selectedRows() 落后了一步【英文标题】:selectedRows() in Qml TreeView with ExtendedSelection is one step behind 【发布时间】:2016-09-15 13:57:22 【问题描述】:

我在qml中有一个treeview,选择方式是ExtendedSelection。每当用户更改所选索引时,我都需要准确知道选择了哪些树行。这是树视图:

Controls.TreeView 
             id: myTreeView
             Controls.TableViewColumn 
                  title: "Name"
                  role: "name"
             

             headerVisible: false
             anchors.fill: parent
             selection: ItemSelectionModel 
                onCurrentIndexChanged: 
                  console.log(myTreeView.selection.selectedRows(0))
                  console.log(currentIndex)
                 
             

             selectionMode: Controls.SelectionMode.ExtendedSelection
             onDoubleClicked: 
              if (isExpanded(index))
                   collapse(index);
              else
                   expand(index);
              

    

在上面的示例中,我只是尝试将选定的行打印到控制台以确保我有正确的选择。 currentIndex 始终保存最后选择的索引(如预期的那样),但是应该保存当前选择的所有行的 selection.selectedRows() 总是落后一步。例如:

如果用户(按住 Ctrl 键)一一选择行 1, 2, 3,则 selection.selectedRows() 将分别为 null, "1", "1,2"currentIndex 将分别为 1, 2, 3。将这两者结合在一起,可以得到所有处于选中状态的索引的列表。 我的问题是,如果用户释放 Ctrl 并选择行4,那么selection.selectedRows() 将是"1,2,3",尽管它应该是null。 总而言之,无法区分用户选择(按住 Ctrl)行 1,2,3,4 的情况和用户首先选择行(按住 Ctrl)1,2,3 然后释放 Ctrl 并仅选择行 4 的情况.

我也用myTreeView.selection.selectedIndexes 进行了测试,但仍然是相同的行为。 对我来说,它看起来像 TreeView 中的一个错误,请建议如何解决。

谢谢。

【问题讨论】:

【参考方案1】:

您应该使用onSelectionChanged 而不是onCurrentIndexChanged。使用该信号,您可以获得所选索引的列表。

我不知道你的全部代码,但使用 Qt 提供的 File System Browser 示例你可以检查差异。

在那个例子中,替换

ItemSelectionModel 
  id: sel
  model: fileSystemModel

ItemSelectionModel 
    id: sel
    model: fileSystemModel
    onSelectionChanged: 
       console.log("onSelectionChanged - selectedRows: " + selectedIndexes)
    
    onCurrentIndexChanged: 
       console.log("onCurrentIndexChanged - selectedRows: " + selectedIndexes)
    

看看这两个信号是如何工作的。

【讨论】:

以上是关于带有 ExtendedSelection 的 Qml TreeView 中的 selectedRows() 落后了一步的主要内容,如果未能解决你的问题,请参考以下文章

需要在pyspark中将列表转换为数据框

(转)WebSphere MQ基础命令

Proxmox VE 7.2 qm 方式安装openwet

Proxmox VE 7.2 qm 方式安装openwet

SAP QM 如何将一个附件挂在一个Quality Notification单据上?

在 Windows 中呈现 Qm 问题