PDA

View Full Version : QItemSelectionModel::selectedColumns returns empty list



MarkoSan
20th August 2014, 22:41
I have subclassed QTableView and inside of it there are 8x8 (64) cells. Now, I am trying to reimplement selectionChanged() slot. Here is declaration:

protected slots:
void selectionChanged(const QItemSelection& selected,
const QItemSelection& deselected);
and here is implementation:

void CTableView::selectionChanged(const QItemSelection& selected,
const QItemSelection& deselected)
{
// qDebug() << "\nBEGIN\nSelected cell:" << selected.indexes() <<
// "\nDeselected cell:" << deselected.indexes() <<
// "\nCurrently selected cells (history):" << this->selectionModel()->selectedIndexes() <<
// "\nEND\n";

foreach(QModelIndex modelIndex,
selected.indexes())
{
// qDebug() << "\nmodelIndex.row():" <<
// modelIndex.row() <<
// "\n";
qDebug() << "\nthis->selectionModel()->selectedColumns(modelIndex.row()):" <<
this->selectionModel()->selectedColumns(modelIndex.row()) <<
"\n";
// if(this->selectionModel()->selectedColumns(modelIndex.row()).size()<=0)
// {
// qDebug() << "\nOnly one cell selected:\n" <<
// this->selectionModel()->selectedColumns(modelIndex.row()).size() <<
// "\n";
// }
// else
// {
// qDebug() << "\nMore than one cell selected\n";
// }
}
}
When I click on some cell, the slot is launched, but
this->selectionModel()->selectedColumns(modelIndex.row())returns empty list:
this->selectionModel()->selectedColumns(modelIndex.row()): ()Why??10570As you can see from screenshot, I've selected a cell, but selectedColumns() list is empty.

d_stranz
21st August 2014, 18:55
Nice job with the Magic Marker on the screenshot. Hope you were able to clean it off your monitor :)

Doe the modelIndex row and column match what you think you have selected? The QItemSelectionModel::selectionChanged() signal represents a delta - if you have single cell selection enabled, the selected argument contains the new selection, the deselected argument contains the previous selection. If you have multiple selection enabled, then the selected argument contains only the new items added to the previous selection, not the entire set of selected items. Likewise, the deselected argument will contain only the items that have been reomved from the selection since the previous change.

I find it more reliable to ignore these arguments. I retrieve the QItemSelectionModel pointer from the view and simply call QItemSelectionModel::selection() or QItemSelectionModel::selectedIndexes(). Either of these calls will return you the complete set of the currently-selected indexes.