PDA

View Full Version : qtableview selectrow(s)



JeanC
8th May 2008, 14:51
Hello,

I have a tableview / sqlmodel.
A selection in the table can be made to rename those items with a dialog.

Renaming the items in the sql database is done by QSqlquery and afterwards the model gets a select() so the data is up to date, this works fine.

But I want to retain the selection after the select().
I am trying to do a loop with the (new because the tabel is sorted and positions can change) rownumbers and selectRow().


for (int i = 0; i < newnames.count(); i++)
table->selectRow(findSong(newnames[i]));
//findSong is code of my own to get the correct rownumber

But the result is that only the last row is selected allthough the table has:

setSelectionBehavior(QAbstractItemView::SelectRows );
setSelectionMode(QAbstractItemView::ExtendedSelect ion);

Why does that loop with selectRow() only select the last row and what can I do about it? I looked at QAbstractItemView::setSelection() but that one wants a rect.

Thanks

wysota
9th May 2008, 10:01
If you call select() the whole model is reset. I suggest you do the update manually using the model interface (QAbstractItemModel::setData()). Then your selection will be kept. And it will be much simpler to do, because you'll be operating on model indexes directly and not mapping them to database rows back and forth.


QStringList newnames;
newnames << ...
int curr = 0;
foreach(QModelIndex index, view->selectionModel()->selectedIndexes()){
view->model()->setData(index, newnames[curr++], Qt::DisplayRole);
}