我有一个显示项目的QTreeView和一个用于过滤的QSortFilterProxyModel。为SingleSelection和SelectRows配置了QTreeView。
如果使用筛选器删除选定的项目,则会选择下一个未筛选的项目。我想改变这种行为。我现在的想法是连接到rowsAboutToBeRemoved(),如果所选的项被删除,则清除选择。
connect(_ui.treeView->model(), &QAbstractItemModel::rowsAboutToBeRemoved,
[this] (const QModelIndex & parent, int start, int end) {//check and clear selection});但是,在触发rowsAboutToBeRemoved()信号之前,QT会将选择更改为下一项。所以我不能测试被移除的项目是否是选中的项目。
如果项目被过滤,有没有更好的方法来清除选择?我更喜欢不涉及从QTreeView派生的解决方案。
发布于 2015-09-08 21:35:27
rowsAboutToBeRemoved不是一个信号,它是一个虚拟方法。
请参阅:http://doc.qt.io/qt-4.8/qtreeview.html
它不是QTreeView,而是有信号的ItemModel:
http://doc.qt.io/qt-4.8/qitemselectionmodel.html#selectionChanged
QTreeWidget有它自己的信号:
http://doc.qt.io/qt-5/qtreewidget.html#itemSelectionChanged
但对于QTreeView,必须使用ItemModel和/或ItemDelegate。
https://stackoverflow.com/questions/32458687
复制相似问题