Reputation: 113
I have a TreeView that is feeded by a SortFilterProxyModel that is feeded by a SqlQueryModel.
Now I want to add double click event so that an edit dialog is loaded with data from the selected row and can be edited.
But all I get is a "random" row to be loaded, it's like the TreeView current index is wrong. My guess is that the SortFilterProxyModel is messing it up, but I have no clue on how to get the right index.
This is how I set my models:
proxyModel = new SortFilterProxyModel();
treeView = new QTreeView();
treeView->setModel(proxyModel);
sqlModel = new QSqlQueryModel(this);
proxyModel->setSourceModel(sqlModel);
And this is my code that gets the wrong row:
QSqlRecord product = sqlModel->record(treeView->currentIndex().row());
I'm a newbie on QT but I've looked everywhere on the net and couldn't find an answer, so I'm hoping someone here can help me! :D
Upvotes: 0
Views: 878
Reputation: 113
I'll leave the answer they gave me in another web:
You need to take the view's currentIndex(), which is a model index for the sorted/filter side of the proxy model, and use the proxy model's QSortFilterProxyModel::mapToSource() function to obtain a corresponding model index for the source model. Then you can use that index (if valid) to address the SQL model directly.
Upvotes: 1