【问题标题】:Keep selection on QTreeView when updating the model更新模型时在 QTreeView 上保持选择
【发布时间】:2015-05-17 20:19:09
【问题描述】:

当我在 QTreeView 中选择一些行并触发重新加载模型的事件时,选择会丢失。

tree = QTreeView()
tree.setSelectionMode(QAbstractItemView.ExtendedSelection)

// Every time an event is triggered, update the model
model = QStandardItemModel(rowCount, columnCount)
tree.setModel( model )

我知道行可能已经改变,但这没关系,我只想选择相同的行(按索引)。

【问题讨论】:

    标签: qt qtreeview


    【解决方案1】:
    1. 在重置模型之前,您需要从 selectionModel 中获取列表选定的索引。
    2. 重新加载模型后,您需要重新设置此列表。

    只有当您的模型相同时,它才会起作用。但是在它失效后保持原始QModelIndex 是危险的。在你的情况下,如果你的模型是普通的(没有),你可以不保留QModelIndex,而是保留QPair< int, int >( row, column )。在任何情况下,您都需要创建自己的机制来识别需要选择哪些项目。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-04-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-12-14
      • 2018-10-29
      • 1970-01-01
      相关资源
      最近更新 更多