PDA

View Full Version : QSqlTableModel and QTableView selection problems



innerhippy
16th December 2008, 15:37
Hello smart people!

I have a simple app that displays database rows in a QTableView, using a derived QSqlTableModel. One column of the dataset is editable - a simple checkbox - and to achive this I have redefined data and setData methods with Qt::CheckStateRole.

All works ok, but there's an annoying problem with the tableView. If I select a row and then call select(), the row selection is cleared model is reset.

So, if I update the checkbox on one of the rows - this will trigger a select() - my selection is gone and the view whizzes back to the first row!

Can anyone explain why it's doing this and how to prevent it?

Thanks!

Will

caduel
16th December 2008, 17:06
If you call select() that is basically a reset on the model.
Thus the selection gets lost.

You can try to work around that by storing the selection and re-setting it after the select().
See QAbstractItemView::selectionModel(), QItemSelectionModel::selection(), QItemSelectionModel::select()

(Always assuming that this works gracefully when the old itemselection contains indexes that no longer exist in the updated model...)

Please, tell us if that approach works ok.

HTH

innerhippy
16th December 2008, 17:54
Thanks for the info - I suspected as much!

Not only will I need to re-select the item, but also re-position it in the viewport. For that all I should need to know is the first visible row, and then scoll to this after the select statement, using the "QAbstractItemView::PositionAtTop" scroll hint.

So, the £2,000,000 (about 3 Euros) question is how to find the first visible row?

caduel
16th December 2008, 21:01
QModelIndex indexAt() gives you the index at a certain point. At QPoint(5,5) you should find the first row.

I have forgot about the following problem:
both this index *and* the indexes in the QItemSelection probably will (or at least might) get invalid if you call select.

innerhippy
18th December 2008, 17:25
Thanks caduel, your suggestions worked great!

Just for reference - this is the implementaton:


QModelIndex topVisibleRow = tableView->indexAt(QPoint(5,5));

m_model->setData(index, 99);

tableView->scrollTo(topVisibleRow, QAbstractItemView::PositionAtTop);



If the row was deleted instead of updated, would the topVisibleRow index still be valid or would it reference the previous row in the model?

This restores the table position after the triggered select(). I can save a single selected row and re-select that, but there's no way to restore mulitple rows (which may or may not be contiguous), but I can live with that.

Thanks for your help!

calmspeaker
19th December 2008, 06:48
If the row was deleted instead of updated, would the topVisibleRow index still be valid or would it reference the previous row in the model?

Before you use the index, you should call isValid().
I think the index reference the next row,:cool:

By the way, is QTableView has a function named select()? I cannot find it.

And I have a question:How can I get the index of one column of the tableview which is visible to user? Thanks a lot!