PDA

View Full Version : Problem getting selected item from QTreeView + QSortFilterProxyModel



RenatoFerreira
24th December 2014, 22:36
Hi,

I'm not being able to read the selected item from a QTreeView when it's being sorted by a QSortFilterProxyModel. All the references from currentIndex(), currentIndex().row(), selectionModel()->currentIndex() or whatever are always returning the row number in QTreeview, but I cannot find the correct record in the original model using the row number as the sorting process seems to be not affecting the sequence of data in the model. I'm using the QSqlQueryModel.

Am I doing something wrong here? I would appreciate any suggestion....

Thank-you,
Renato A. Ferreira

anda_skoa
24th December 2014, 23:51
The indexes returned by the view's selection model are obviously from the model that the view works on.
In your case the proxy model.

They don't make any sense in the context of the proxy's source model.

If you really need an index of the source model, use the proxy's mapToSource() function.

If you just need data from the index, use its data() function.

Cheers,
_

RenatoFerreira
25th December 2014, 14:27
Perfect! It worked with mapToSource()...


mainModel.record(proxyModel->mapToSource(ui->treeView->currentIndex()).row())

And now I can access the primary key to search the rest of data from database.

Thank-you!