diff options
author | Richard Moe Gustavsen <richard.gustavsen@qt.io> | 2021-04-14 12:04:13 +0200 |
---|---|---|
committer | Richard Moe Gustavsen <richard.gustavsen@qt.io> | 2021-04-19 14:35:56 +0200 |
commit | 929a32bd8def11c0d7abec173189272e7c4366e3 (patch) | |
tree | 73fadf185c5753e4b196e04fcffa4dd3c4c54bbf | |
parent | 67bb206400d505ab184e9ea53357c0b0aebf6d13 (diff) |
Only stop keypress propagation if TreeView actually uses the key
Only stop keypress propagation if TreeView uses the key.
Otherwise, forward the event to the parent. This will
ensure that TreeView don't stop Keys.onPressed handlers in
anchestor items from working correctly.
Fixes: QTBUG-92517
Change-Id: I6d7ea4062bbb9eef4a64aa6bd18ff29696352dcd
Reviewed-by: Mitch Curtis <mitch.curtis@qt.io>
-rw-r--r-- | src/qquicktreeview.cpp | 1 | ||||
-rw-r--r-- | tests/auto/data/keypress.qml | 66 | ||||
-rw-r--r-- | tests/auto/tst_treeview.cpp | 22 |
3 files changed, 89 insertions, 0 deletions
diff --git a/src/qquicktreeview.cpp b/src/qquicktreeview.cpp index 1af2723..255caae 100644 --- a/src/qquicktreeview.cpp +++ b/src/qquicktreeview.cpp @@ -889,6 +889,7 @@ void QQuickTreeView::keyPressEvent(QKeyEvent *e) toggleModelIndexExpanded(d->m_currentModelIndex); break; default: + QQuickTableView::keyPressEvent(e); break; } } diff --git a/tests/auto/data/keypress.qml b/tests/auto/data/keypress.qml new file mode 100644 index 0000000..b7a18d7 --- /dev/null +++ b/tests/auto/data/keypress.qml @@ -0,0 +1,66 @@ +/**************************************************************************** +** +** Copyright (C) 2021 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of TreeView. +** +** $QT_BEGIN_LICENSE:GPL-MARKETPLACE-QT$ +** +** Marketplace License Usage +** Users, who have licensed the Software under the Qt Marketplace license +** agreement, may use this file in accordance with the Qt Marketplace license +** agreement provided with the Software or, alternatively, in accordance with +** the terms contained in a written agreement between the licensee and The Qt +** Company. For licensing terms and conditions see +** https://www.qt.io/terms-conditions/#marketplace and +** https://www.qt.io/terms-conditions. For further information use the contact +** form at https://www.qt.io/contact-us. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3 or (at your option) any later version +** approved by the KDE Free Qt Foundation. The licenses are as published by +** the Free Software Foundation and appearing in the file LICENSE.GPL3 +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +import QtQuick 2.15 +import QtQuick.Window 2.15 +import QtQuick.TreeView 2.15 +import TestModel 1.0 + +Item { + width: 800 + height: 600 + + property alias treeView: treeView + property int treeViewPressed: 0 + property int parentPressed: 0 + + Item { + anchors.fill: parent + anchors.margins: 10 + + Keys.onPressed: { + parentPressed = event.key + } + + TreeView { + id: treeView + anchors.fill:parent + anchors.margins: 10 + model: TestModel {} + clip: true + + Keys.onPressed: { + treeViewPressed = event.key + } + } + } +} diff --git a/tests/auto/tst_treeview.cpp b/tests/auto/tst_treeview.cpp index 5ba2979..2483963 100644 --- a/tests/auto/tst_treeview.cpp +++ b/tests/auto/tst_treeview.cpp @@ -77,6 +77,7 @@ private slots: void attachedPropertiesChildren(); void emptyModel(); void updatedModifiedModel(); + void keypressOverload(); }; QQuickView *tst_treeview::createView(const QString &filename) @@ -231,6 +232,27 @@ void tst_treeview::updatedModifiedModel() QCOMPARE(rootItemCol1->property("text"), "Changed"); } +void tst_treeview::keypressOverload() +{ + // Check that TreeView only eats the key events it uses + LOAD_TREEVIEW("keypress.qml"); + treeView->forceActiveFocus(); + QMetaObject::invokeMethod(treeView, "expand", Q_ARG(int, 0)); + WAIT_UNTIL_POLISHED; + + QTest::keyEvent(QTest::Click, treeView->window(), Qt::Key_Down); + int treeViewPressed = view->rootObject()->property("treeViewPressed").value<int>(); + int parentPressed = view->rootObject()->property("parentPressed").value<int>(); + QCOMPARE(treeViewPressed, Qt::Key_Down); + QCOMPARE(parentPressed, Qt::Key(0)); + + QTest::keyEvent(QTest::Click, treeView->window(), Qt::Key_A); + treeViewPressed = view->rootObject()->property("treeViewPressed").value<int>(); + parentPressed = view->rootObject()->property("parentPressed").value<int>(); + QCOMPARE(treeViewPressed, Qt::Key_A); + QCOMPARE(parentPressed, Qt::Key_A); +} + QTEST_MAIN(tst_treeview) #include "tst_treeview.moc" |