Uploaded image for project: 'Qt'
  1. Qt
  2. QTBUG-79092

Can not deselect row in QTableView if 0 column is not visible

    XMLWordPrintable

    Details

    • Type: Bug
    • Status: Closed
    • Priority: P2: Important
    • Resolution: Done
    • Affects Version/s: 5.12.0
    • Fix Version/s: 5.14
    • Component/s: Widgets: Itemviews
    • Labels:
      None
    • Platform/s:
      All
    • Commits:
      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.

        Attachments

        For Gerrit Dashboard: QTBUG-79092
        # Subject Branch Project Status CR V

          Activity

            People

            Assignee:
            qt.team.quick.subscriptions Qt Quick and Widgets Team
            Reporter:
            andrew.smeltzov Andrew Smeltzov
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

              Dates

              Created:
              Updated:
              Resolved:

                Gerrit Reviews

                There are no open Gerrit changes