From 34c987cbd10971e4b5a00d08c6061b2f65967e56 Mon Sep 17 00:00:00 2001 From: Richard Moe Gustavsen Date: Wed, 5 Sep 2018 13:45:38 +0200 Subject: QQuickTableView: clear focus when delegate item with focus is flicked out If we flick out a cell that has keyboard focus, we should clear that focus. Otherwise, the item will be focused also when it is later reused. Change-Id: I0fb79b6d906c1907a352de4ec52e3b488064b55a Reviewed-by: Mitch Curtis --- .../quick/qquicktableview/data/tableviewfocus.qml | 73 ++++++++++++++++++++++ .../quick/qquicktableview/tst_qquicktableview.cpp | 35 +++++++++++ 2 files changed, 108 insertions(+) create mode 100644 tests/auto/quick/qquicktableview/data/tableviewfocus.qml (limited to 'tests/auto/quick') diff --git a/tests/auto/quick/qquicktableview/data/tableviewfocus.qml b/tests/auto/quick/qquicktableview/data/tableviewfocus.qml new file mode 100644 index 0000000000..35f9c06587 --- /dev/null +++ b/tests/auto/quick/qquicktableview/data/tableviewfocus.qml @@ -0,0 +1,73 @@ +/**************************************************************************** +** +** Copyright (C) 2018 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the QtQuick module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 3 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL3 included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 3 requirements +** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 2.0 or (at your option) the GNU General +** Public license version 3 or 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.GPL2 and 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-2.0.html and +** https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +import QtQuick 2.12 +import QtQuick.Window 2.3 + +Item { + width: 640 + height: 450 + + property alias tableView: tableView + + TableView { + id: tableView + width: 600 + height: 400 + clip: true + delegate: tableViewDelegate + } + + Component { + id: tableViewDelegate + Item { + implicitWidth: 100 + implicitHeight: 50 + property alias textInput: textInput + + TextInput { + id: textInput + width: parent.width + height: parent.height + text: "TextInput" + } + } + } + +} diff --git a/tests/auto/quick/qquicktableview/tst_qquicktableview.cpp b/tests/auto/quick/qquicktableview/tst_qquicktableview.cpp index 45b3566574..580500e787 100644 --- a/tests/auto/quick/qquicktableview/tst_qquicktableview.cpp +++ b/tests/auto/quick/qquicktableview/tst_qquicktableview.cpp @@ -110,6 +110,7 @@ private slots: void checkLayoutOfEqualSizedDelegateItems_data(); void checkLayoutOfEqualSizedDelegateItems(); void checkTableMargins_data(); + void checkFocusRemoved(); void checkTableMargins(); void fillTableViewButNothingMore_data(); void fillTableViewButNothingMore(); @@ -737,6 +738,40 @@ void tst_QQuickTableView::checkLayoutOfEqualSizedDelegateItems() } } +void tst_QQuickTableView::checkFocusRemoved() +{ + // Check that we clear the focus of a delegate item when + // it's flicked out of view, and then reused. + LOAD_TABLEVIEW("tableviewfocus.qml"); + + const char *textInputProp = "textInput"; + auto model = TestModelAsVariant(100, 100); + tableView->setModel(model); + + WAIT_UNTIL_POLISHED; + + auto const item = tableViewPrivate->loadedTableItem(QPoint(0, 0))->item; + auto const textInput = qvariant_cast(item->property(textInputProp)); + QVERIFY(textInput); + QCOMPARE(tableView->hasActiveFocus(), false); + QCOMPARE(textInput->hasActiveFocus(), false); + + textInput->forceActiveFocus(); + QCOMPARE(tableView->hasActiveFocus(), true); + QCOMPARE(textInput->hasActiveFocus(), true); + + // Flick the focused cell out, and check that none of the + // items in the table has focus (which means that the reused + // item lost focus when it was flicked out). But the tableview + // itself will maintain active focus. + tableView->setContentX(500); + QCOMPARE(tableView->hasActiveFocus(), true); + for (auto fxItem : tableViewPrivate->loadedItems) { + auto const textInput2 = qvariant_cast(fxItem->item->property(textInputProp)); + QCOMPARE(textInput2->hasActiveFocus(), false); + } +} + void tst_QQuickTableView::checkTableMargins_data() { QTest::addColumn("model"); -- cgit v1.2.3