PDA

View Full Version : Refresh QTableView after sorting



araglin
18th December 2008, 19:32
I have model-view application - a table with many rows in it.
There is sorting enabled at the header of this table. (To sort data in the table I use Proxy model because without it sorting didn't work). The app works well except one little thing:
1. I select row in the table
2. I click on a column in the header to sort data
3. There is no auto-scroll to the raw I selected before sorting.

i.e. if my table has 100 rows and on the screen I can see only 20 of them, and I choose 1st raw, then press "sort", chosed row goes to the bottom of table and it would be nice if the application automatically scroll QTableView to show my row. How can I do this? Thank you!

jpn
18th December 2008, 20:37
How about QAbstractItemView::scrollTo(index, EnsureVisible)?

araglin
18th December 2008, 20:59
Thanks!
Think that something like this must work out:


connect( header, SIGNAL(clicked( const QModelIndex&) ), tableView, SLOT(scrollTo( const QModelIndex& ) ) );


But I think it's necessary to replace QModelIndex& of SIGNAL(clicked) to the index of the element in the row I chosed before. That's the problem for me. Would you please tell me right way to do this operation?

jpn
18th December 2008, 21:28
Unfortunately scrollTo() is not a slot so you have to implement a custom slot that further calls that function. Also, QHeaderView::sortIndicatorChanged() might be more reliable than the clicked() signal.

araglin
18th December 2008, 22:13
Ok, thanks for advices! I'll try to implement something :)