Details
-
Bug
-
Resolution: Done
-
P2: Important
-
5.12.0
-
None
-
-
5edf34848a51c7678031aeb9576b8f3b7b5fceab (qt/qtbase/5.15)
Description
Can not deselect one row in QTableView if column 0 is not visible by clicking on QHeaderView with +Ctrl modifier.
if 0 column is visible the row gets deselected.
if not - it always selects the row, thus no changes.
The problem is in QTableView.cpp method QTableViewPrivate::selectRow(int row, bool anchor)
int column = horizontalHeader->logicalIndexAt(q->isRightToLeft() ? viewport->width() : 0);
logicalIndexAt(0) gives logical index, i.e. leftmost visible. And then in
ctrlDragSelectionFlag = verticalHeader->selectionModel()->selectedRows().contains(index) ? QItemSelectionModel::Deselect : QItemSelectionModel::Select;
selectedRows() takes default parameter column=0. And it never contains index with non-zero column so we always get ::Select.
void QTableViewPrivate::selectRow(int row, bool anchor) { Q_Q(QTableView); if (q->selectionBehavior() == QTableView::SelectColumns || (q->selectionMode() == QTableView::SingleSelection && q->selectionBehavior() == QTableView::SelectItems)) return; if (row >= 0 && row < model->rowCount(root)) { int column = horizontalHeader->logicalIndexAt(q->isRightToLeft() ? viewport->width() : 0); QModelIndex index = model->index(row, column, root); QItemSelectionModel::SelectionFlags command = q->selectionCommand(index); selectionModel->setCurrentIndex(index, QItemSelectionModel::NoUpdate); if ((anchor && !(command & QItemSelectionModel::Current)) || (q->selectionMode() == QTableView::SingleSelection)) rowSectionAnchor = row; if (q->selectionMode() != QTableView::SingleSelection && command.testFlag(QItemSelectionModel::Toggle)) { if (anchor) ctrlDragSelectionFlag = verticalHeader->selectionModel()->selectedRows().contains(index) ? QItemSelectionModel::Deselect : QItemSelectionModel::Select; command &= ~QItemSelectionModel::Toggle; command |= ctrlDragSelectionFlag; if (!anchor) command |= QItemSelectionModel::Current; } QModelIndex upper = model->index(qMin(rowSectionAnchor, row), column, root); QModelIndex lower = model->index(qMax(rowSectionAnchor, row), column, root); if ((verticalHeader->sectionsMoved() && upper.row() != lower.row())) { q->setSelection(q->visualRect(upper) | q->visualRect(lower), command | QItemSelectionModel::Rows); } else { selectionModel->select(QItemSelection(upper, lower), command | QItemSelectionModel::Rows); } } }
We need to pass our visible column to selectedRows()
or to always take 0 column in the first place.