PDA

View Full Version : How mantain a number of rows selected in a QTableView after the model is reset.



ferrabras
3rd March 2011, 14:44
Hi,
I have a QAbstracTableModel that provide data for a QTableView. From time to time my model is reset, so when the internal data is about to be changed I call beginResetModel() then I change the internal data and finally calls reset() and endResetModel().

The form holding the respective view has the slotholdActiveFlightSelectio() connected to modelAboutToReset() and the slot resetActiveFlightSelection() connected to modelReset().
When the slot triggered by modelAboutToReset() is called I basically store the callsign of a item.
When the slot triggered by modelReset() is called I search for the callsign and then I try to select the correponding item at the new view.

this is the code:

void FlightTables::holdActiveFlightSelection()
{
int k;
selectedActiveFlights.clear();
QModelIndexList selectedRows = activeSelectionModel->selectedRows();
if(selectedRows.size() == 0) return;
for(k=0;k<selectedRows.size();k++)
{
selectedActiveFlights.append(activeModel->data(selectedRows.at(k),Qt::DisplayRole).toString( ));
}
}

void FlightTables::resetActiveFlightSelection()
{
int k;
QString callsign;
if(selectedActiveFlights.size() == 0) return;
activeSelectionModel = ui->activeFlightTableView->selectionModel();
for(k=0;k < activeModel->rowCount();k++)
{
QModelIndex tmpIndex = activeModel->index(k,0);
QItemSelection selection;
callsign = activeModel->data(tmpIndex,Qt::DisplayRole).toString();
if(selectedActiveFlights.contains(callsign))
{
selection.select(tmpIndex,tmpIndex);
activeSelectionModel->select(selection,QItemSelectionModel::Select|QItem SelectionModel::Rows);
//activeSelectionModel->select(tmpIndex,QItemSelectionModel::Select|QItemS electionmodel::Rows);

}
}
}
I am holding the callsigns before the model reset and searching for them in the new model, but when i try to paint the selecion at the QTableView by activeSelectionModel nothing happens?!
I am not sure about the diference of calling:
void QItemSelectionModel::select ( const QModelIndex & index, QItemSelectionModel::SelectionFlags command )
or
void QItemSelectionModel::select ( const QItemSelection & selection, QItemSelectionModel::SelectionFlags command )

wysota
6th March 2011, 10:01
The easiest thing would be not to reset your model but instead add/remove/move rows.

How exactly do you "try to paint the selection"?

ferrabras
9th March 2011, 14:35
The easiest thing would be not to reset your model but instead add/remove/move rows.
All data from the model changes every 1 minute so I beleive reseting it and only keeping track of the selected item would be better.


How exactly do you "try to paint the selection"?
I thought
activeSelectionModel->select(selection,QItemSelectionModel::Select|QItem SelectionModel::Rows); would do the trick.
How can I select items in a QTableView programatically!

wysota
9th March 2011, 17:06
All data from the model changes every 1 minute so I beleive reseting it and only keeping track of the selected item would be better.
This is contradictory. If all items are removed from the model, there is no selection to track as there are no items that used to be in the model and could have been selected before the update. If some of the items stay in the model then you shouldn't be resetting the model but rather updating (adding/removing/updating rows) it.

And yes, using select() should have worked assuming your stored selection list is valid and consistent with contents of the model.

ferrabras
10th March 2011, 16:52
The data is not removed but most of columns change, I would have to emit datachanged for each one of them, wouldn't I?
Could you give an exemple or direct me to one!? Please!

wysota
10th March 2011, 21:06
The data is not removed but most of columns change, I would have to emit datachanged for each one of them, wouldn't I?
You can emit one signal for the whole model.

emit dataChanged(index(0,0), index(rowCount()-1, columnCount()-1);
or something similar.