bear
bear

Reputation: 181

Keep the selection after filtering a QTableView with a QSortFilterProxyModel

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?

Upvotes: 2

Views: 2947

Answers (2)

kossmoboleat
kossmoboleat

Reputation: 1941

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.

Upvotes: 1

serge_gubenko
serge_gubenko

Reputation: 20482

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.

Upvotes: 0

Related Questions