QTableView: properly deselect row when column 0 is hidden/not visible

When the first column is hidden or not visible in the current viewport,
it is not possible to deselect the current row.
Fix it by passing the correct column to
QItemSelectionModel::selectedRows() when testing if the current index is
selected.

Fixes: QTBUG-79092
Change-Id: I9d8082d2b29ad2f799156aee910c6ff6e3217771
Reviewed-by: David Faure <david.faure@kdab.com>
Reviewed-by: Friedemann Kleint <Friedemann.Kleint@qt.io>
This commit is contained in:
Christian Ehrlicher 2019-10-19 19:18:55 +02:00
parent fcbf15c97b
commit 5edf34848a
2 changed files with 27 additions and 1 deletions

View File

@ -3331,7 +3331,7 @@ void QTableViewPrivate::selectRow(int row, bool anchor)
if (q->selectionMode() != QTableView::SingleSelection
&& command.testFlag(QItemSelectionModel::Toggle)) {
if (anchor)
ctrlDragSelectionFlag = verticalHeader->selectionModel()->selectedRows().contains(index)
ctrlDragSelectionFlag = verticalHeader->selectionModel()->selectedRows(column).contains(index)
? QItemSelectionModel::Deselect : QItemSelectionModel::Select;
command &= ~QItemSelectionModel::Toggle;
command |= ctrlDragSelectionFlag;

View File

@ -418,6 +418,7 @@ private slots:
void taskQTBUG_10169_sizeHintForRow();
void taskQTBUG_30653_doItemsLayout();
void taskQTBUG_50171_selectRowAfterSwapColumns();
void deselectRow();
#if QT_CONFIG(wheelevent)
void mouseWheel_data();
@ -4492,6 +4493,31 @@ void tst_QTableView::taskQTBUG_50171_selectRowAfterSwapColumns()
}
}
class DeselectTableWidget : public QTableWidget
{
public:
using QTableWidget::QTableWidget;
QItemSelectionModel::SelectionFlags selectionCommand(const QModelIndex &,
const QEvent * = nullptr) const override
{
return QItemSelectionModel::Toggle;
}
};
void tst_QTableView::deselectRow()
{
DeselectTableWidget tw(20, 20);
tw.show();
QVERIFY(QTest::qWaitForWindowExposed(&tw));
tw.hideColumn(0);
QVERIFY(tw.isColumnHidden(0));
tw.selectRow(1);
QVERIFY(tw.selectionModel()->isRowSelected(1, QModelIndex()));
tw.selectRow(1);
// QTBUG-79092 - deselection was not possible when column 0 was hidden
QVERIFY(!tw.selectionModel()->isRowSelected(1, QModelIndex()));
}
// This has nothing to do with QTableView, but it's convenient to reuse the QtTestTableModel
#if QT_CONFIG(textmarkdownwriter)