开发者

Keep the selection after filtering a QTableView with a QSortFilterProxyModel

开发者 https://www.devze.com 2023-02-22 18:27 出处:网络
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 t开发者_如何学Che view.

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 t开发者_如何学Che 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?


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 using QItemSelectionModel::select().

I know this thread is very old, but I thought I'd leave the comment for anybody else facing a similar problem.


From what you wrote it looks like the problem is in the QTableView loosing selection when you're cleaning your QLineEdit content. If you're starting your 'search' routine in the line edit's editingFinished() or textChanged() signals you can disconnect from them before changing the QLineEdit 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.

0

精彩评论

暂无评论...
验证码 换一张
取 消