PDA

View Full Version : QTableView not scrolling



waynew
15th February 2010, 22:46
I have a model/view where the code searches for the value in a field.

The correct row is always selected ok, but the view does not scroll so the selected row is visible.



QSortFilterProxyModel proxy;
proxy.setSourceModel(model);
proxy.setFilterKeyColumn(1);
proxy.setFilterFixedString(ui->mwCall->text());
QModelIndex vidx;
vidx = proxy.mapToSource(proxy.index(0,0));
view->selectionModel()->select(vidx, QItemSelectionModel::Select | QItemSelectionModel::Rows);
view->scrollTo(vidx, QAbstractItemView::EnsureVisible);


Any ideas as to how to fix this to make it scrollTo correctly?

Coises
16th February 2010, 00:32
Any ideas as to how to fix this to make it scrollTo correctly?

I don’t know what’s wrong, but I can suggest the way I would proceed: run it under the debugger with a breakpoint on the scrollTo line. Step into the QTableView::scrollTo implementation and see if you can spot where it’s going awry. That might give you a hint as to why it is failing.

waynew
16th February 2010, 01:16
Well Coises, I can't see where it is going wrong in the debugger.
I am wondering if it makes a difference that the search is executed from the main window and when it completes, the main window still has focus, not the view window. However, the row still gets selected and highlighted correctly in the view window. If the selected row is not visible and you manually scroll the view window you can find it and it is selected ok. So I guess I'm at a loss here - will just keep researching and see if I can come up with something.

Another cat lover? I have 3 white ones.

waynew
16th February 2010, 01:54
Ok, solved. I was hiding the primary key column from the database/model in the view and this was the field the search was based on.
Now if I setColumnHidden(0,FALSE) when starting the search, then setColumnHidden(0,TRUE) after the scrollTo, it works fine.

For the benefit of anyone else needing this, here is the code that works:



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

Coises
19th February 2010, 17:30
I’m glad you found the answer, and thanks for posting it so others can find it.

Yes, I’m a cat lover. Though my namesake, Coises! (http://www.coises.com/photos3.htm#coises2), died in 2005, I’ve yet to get another. I’m holding out until my own life feels stable. It’s looking like a long wait...

drescherjm
10th July 2017, 23:58
I just had the same issue. I wasted abut 1 hour trying to figure out why I could not scroll to the selected item. It ended up being the same cause the quite a few of my columns are hidden (from a database query) and the column I was trying to scroll to was one of the hidden columns.

Thanks,
John