PDA

View Full Version : QT SqlQueryModel, TreeView and SortFilterProxyModel how to get selected row



LautaroMed
30th April 2013, 15:28
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

ChrisW67
1st May 2013, 01:58
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.

LautaroMed
1st May 2013, 06:49
Thank you so much! You just made my day :P