PDA

View Full Version : qtableview extended selection mode problem



hvengel
1st November 2011, 22:28
I have a QTableView with

setSelectionBehavior(QAbstractItemView::SelectItem s);
setSelectionMode(QAbstractItemView::ExtendedSelect ion);

When I select individual items or I use the control key modifier I get exactly the results I expect when I call

QItemSelection selRow(model()->index(row, 0), model()->index(row, 0));
selectionModel()->select(selRow, QItemSelectionModel::Select | QItemSelectionModel::Rows);

But when I make my selection using the shift modifier I see some apparently incorrect sets of behavior.

1. The first row selected before using the shift modifier to select the whole set of rows will be deselected. This only happen when the focus was not on the tableview when the first row was selected and there were no current selections or when the row before the shift select was selected using the control key. When I watch what is happening in the debugger in selectionChanged() I see that the selected rows after the shift select include all row numbers except the initial selected row. This is what I would expect since I would expect the code snip-it above to add those rows to the existing list of selected rows just like it adds new selected rows when the control key modifier is used.

Has anyone seen this before? Any ideas about why this is happening?

This is with Qt 4.7 on a Windows 7 box.

Added after 4 minutes:

Just in case here is a more complete code snip-it.


void SelectedManagersTableWidget::selectionChanged(QIte mSelection const& selected, QItemSelection const& deselected)
{
if (selected.indexes().size()>0)
{
std::set<int> selRows = getSelectedRowsFast(selected);
BOOST_FOREACH(int row, selRows)
{
QItemSelection selRow(model()->index(row, 0), model()->index(row, 0));
selectionModel()->select(selRow, QItemSelectionModel::Select | QItemSelectionModel::Rows);
}
}

if (deselected.indexes().size()>0)
{
std::set<int> unselRows = getSelectedRowsFast(deselected);
BOOST_FOREACH(int row, unselRows)
{
QItemSelection unselRow(model()->index(row, 0), model()->index(row, 0));
selectionModel()->select(unselRow, QItemSelectionModel::Deselect | QItemSelectionModel::Rows);
}
}
}

Added after 13 minutes:

OK I found it. I was doing something dumb. So never mind.