使用QSortFilterProxymodel过滤QTableView后保持选择
我创建了一个链接到另一个模型的QSortFilterProxyModel
的QTableView
。 在QTableView
(在GUI 中)下有一个QLineEdit
,用于“搜索”视图中的元素。
我的想法是在 QLineEdit 中写入我要查找的内容,并让视图仅显示匹配的元素。过滤后,我想选择相关项目,然后清理 QLineEdit
以在完整视图中返回。 一切正常,但将被过滤的所选项目也将因失效而失去选择。
我该如何解决这个问题?
I created a QTableView
linked to a QSortFilterProxyModel
linked to another model.
Under the QTableView
(in the GUI) there is a QLineEdit
used for "searching" an element in the view.
My idea is to write in the QLineEdit
what I'm looking for and let the view show only the matched elements. After filtering, I want to select the concerned item and then clean the QLineEdit
for returning at the complete view.
Everything works but the selected item that will be filtered will also lose the selection because of the invalidation.
How can I solve this problem?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
为什么不记住过滤之前选定的行,然后在完成过滤后恢复它。
我想你可以直接使用
QItemSelectionModel
。在过滤之前使用
QItemSelectionModel::selectedRows()
,并在过滤之后使用QItemSelectionModel::select()
选择行。我知道这个帖子已经很老了,但我想我应该为面临类似问题的其他人留下评论。
Why don't you remember the selected rows before the filtering and then just restore it when you're done with filtering.
You could use the
QItemSelectionModel
directly I'd imagine.Use
QItemSelectionModel::selectedRows()
before filtering and select rows after filtering usingQItemSelectionModel::select()
.I know this thread is very old, but I thought I'd leave the comment for anybody else facing a similar problem.
从您所写的内容来看,问题似乎出在清理
QLineEdit
内容时QTableView
失去选择。如果您在行编辑的editingFinished() 或 textChanged() 向您发出信号在更改QLineEdit
之前可以断开与它们的连接然后再次重新连接。或者使用布尔标志,并且在打开时不更改过滤。如果您发布代码的简化版本以及您遇到的问题,那么回答您的问题会容易得多。From what you wrote it looks like the problem is in the
QTableView
loosing selection when you're cleaning yourQLineEdit
content. If you're starting your 'search' routine in the line edit's editingFinished() or textChanged() signals you can disconnect from them before changing theQLineEdit
and then reconnect back again. Or use a boolean flag and don't change filtering when it's on. It would be much easier to answer your question if you would post up a simplified version of your code with the problem you're having.