diff options
author | Richard Moe Gustavsen <richard.gustavsen@qt.io> | 2022-11-18 22:39:31 +0100 |
---|---|---|
committer | Richard Moe Gustavsen <richard.gustavsen@qt.io> | 2022-11-25 00:04:03 +0100 |
commit | 2becb1c2073b3bcc0a108c4d7d9ce5b60f29306b (patch) | |
tree | 0270aaf559aa560675ba7182a43b3da9aa480bf4 | |
parent | ac4fea75379467dde9065825d3f15da3b86e9ad8 (diff) |
QQuickTableView: respect activeFocusOnTab
QQuickItem has a activeFocusOnTab property
which should also be respected by TableView.
When disabled, TableView should not transfer
focus between the cells when the user
hits the tab key.
Change-Id: I234286926b58753fa50923321302d4fa108a4515
Reviewed-by: Oliver Eftevaag <oliver.eftevaag@qt.io>
-rw-r--r-- | src/quick/items/qquicktableview.cpp | 9 | ||||
-rw-r--r-- | tests/auto/quick/qquicktableview/tst_qquicktableview.cpp | 46 |
2 files changed, 55 insertions, 0 deletions
diff --git a/src/quick/items/qquicktableview.cpp b/src/quick/items/qquicktableview.cpp index 1af135e6aa..1a1fafa2a2 100644 --- a/src/quick/items/qquicktableview.cpp +++ b/src/quick/items/qquicktableview.cpp @@ -4306,6 +4306,7 @@ void QQuickTableViewPrivate::init() Q_Q(QQuickTableView); q->setFlag(QQuickItem::ItemIsFocusScope); + q->setActiveFocusOnTab(true); positionXAnimation.setTargetObject(q); positionXAnimation.setProperty(QStringLiteral("contentX")); @@ -4430,6 +4431,14 @@ bool QQuickTableViewPrivate::setCurrentIndexFromKeyEvent(QKeyEvent *e) const QPoint currentCell = q->cellAtIndex(currentIndex); const bool select = (e->modifiers() & Qt::ShiftModifier) && (e->key() != Qt::Key_Backtab); + if (!q->activeFocusOnTab()) { + switch (e->key()) { + case Qt::Key_Tab: + case Qt::Key_Backtab: + return false; + } + } + if (!cellIsValid(currentCell)) { switch (e->key()) { case Qt::Key_Up: diff --git a/tests/auto/quick/qquicktableview/tst_qquicktableview.cpp b/tests/auto/quick/qquicktableview/tst_qquicktableview.cpp index f04816b167..08cd1b81fb 100644 --- a/tests/auto/quick/qquicktableview/tst_qquicktableview.cpp +++ b/tests/auto/quick/qquicktableview/tst_qquicktableview.cpp @@ -210,6 +210,7 @@ private slots: void moveCurrentIndexUsingPageUpDownKeys(); void moveCurrentIndexUsingTabKey_data(); void moveCurrentIndexUsingTabKey(); + void respectActiveFocusOnTabDisabled(); void setCurrentIndexOnFirstKeyPress_data(); void setCurrentIndexOnFirstKeyPress(); void setCurrentIndexFromMouse(); @@ -4957,6 +4958,8 @@ void tst_QQuickTableView::moveCurrentIndexUsingTabKey() WAIT_UNTIL_POLISHED; + QVERIFY(tableView->activeFocusOnTab()); + QCOMPARE(tableView->currentColumn(), -1); QCOMPARE(tableView->currentRow(), -1); @@ -5017,6 +5020,49 @@ void tst_QQuickTableView::moveCurrentIndexUsingTabKey() QVERIFY(!selectionModel.hasSelection()); } +void tst_QQuickTableView::respectActiveFocusOnTabDisabled() +{ + // Ensure that we don't move focus for tab or backtab + // when TableView.setActiveFocusOnTab is false. + LOAD_TABLEVIEW("tableviewwithselected1.qml"); + + TestModel model(3, 3); + QItemSelectionModel selectionModel(&model); + + tableView->setModel(QVariant::fromValue(&model)); + tableView->setSelectionModel(&selectionModel); + tableView->setActiveFocusOnTab(false); + tableView->setFocus(true); + + QQuickWindow *window = tableView->window(); + const char kCurrent[] = "current"; + + WAIT_UNTIL_POLISHED; + + QCOMPARE(tableView->currentColumn(), -1); + QCOMPARE(tableView->currentRow(), -1); + QVERIFY(!tableView->activeFocusOnTab()); + + // Start by making cell 0, 0 current + const QPoint cell0_0(0, 0); + selectionModel.setCurrentIndex(tableView->modelIndex(cell0_0), QItemSelectionModel::NoUpdate); + QVERIFY(tableView->itemAtCell(cell0_0)->property(kCurrent).toBool()); + QCOMPARE(tableView->currentColumn(), cell0_0.x()); + QCOMPARE(tableView->currentRow(), cell0_0.y()); + + // Press Tab + const QPoint cell1_0(1, 0); + QTest::keyPress(window, Qt::Key_Tab); + QCOMPARE(selectionModel.currentIndex(), tableView->modelIndex(cell0_0)); + QVERIFY(tableView->itemAtCell(cell0_0)->property(kCurrent).toBool()); + QVERIFY(!tableView->itemAtCell(cell1_0)->property(kCurrent).toBool()); + + // Press Backtab + QTest::keyPress(window, Qt::Key_Backtab); + QCOMPARE(selectionModel.currentIndex(), tableView->modelIndex(cell0_0)); + QVERIFY(tableView->itemAtCell(cell0_0)->property(kCurrent).toBool()); +} + void tst_QQuickTableView::setCurrentIndexOnFirstKeyPress_data() { QTest::addColumn<Qt::Key>("arrowKey"); |