PDA

View Full Version : QSortFilterProxyModel search and view scroll help



waynew
8th November 2011, 19:19
I have a QSqlTableModel and a QTableView. Trying to sort through the model, find a record, then scroll to it in the view.
Here is the code, where strID is a string to search for, like "89".



QSortFilterProxyModel proxy;
proxy.setSourceModel(model);
QModelIndex vidx;

proxy.setFilterKeyColumn(0);
proxy.setFilterFixedString(strID);
vidx = proxy.mapToSource(proxy.index(0, 0));

if (vidx.isValid())
{
view->selectionModel()->select(vidx, QItemSelectionModel::Select | QItemSelectionModel::Rows);
view->scrollTo(vidx, QAbstractItemView::EnsureVisible);
}


I am having two problems.

1. If the header sort is set to column 0 ascending, and the record found has a sequential value in column 0 of more than 256, the scrollTo does not work, 256 or less and it works fine.

2. if the header sort is set to column 0 descending, and the record found has a sequential value in column 0 of 89 for example, the view scrolls to the record with 289 in column 0.

Any advice as to how to overcome these problems greatly appreciated.

waynew
10th November 2011, 02:58
Didn't seem to be able to edit this post.

Here is the update: problem number 2 has been solved with the following code:


proxy.setFilterKeyColumn(0);
QRegExp exp("^"+lastID+"$");
proxy.setFilterRegExp(exp);

It now matches only the whole string. Some success.

No luck with problem number 1. QSortFilterProxyModel does not seem to work with a record more than 256 rows down from the top, no matter how the view is sorted. vidx comes back invalid in those cases. No idea why.

Come on experts, I need some help here! Anyone else having this problem?
Thanks for any advice.