aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRichard Moe Gustavsen <richard.gustavsen@qt.io>2018-09-05 13:45:38 +0200
committerRichard Moe Gustavsen <richard.gustavsen@qt.io>2018-09-06 13:08:16 +0000
commit34c987cbd10971e4b5a00d08c6061b2f65967e56 (patch)
treea4fee67fea1e2715eca845807790c69797e88445
parent77aefc95cf3fbae342004513fd8c9f3a0184eba9 (diff)
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 <mitch.curtis@qt.io>
-rw-r--r--src/quick/items/qquicktableview.cpp27
-rw-r--r--tests/auto/quick/qquicktableview/data/tableviewfocus.qml73
-rw-r--r--tests/auto/quick/qquicktableview/tst_qquicktableview.cpp35
3 files changed, 129 insertions, 6 deletions
diff --git a/src/quick/items/qquicktableview.cpp b/src/quick/items/qquicktableview.cpp
index 110373f95d..0638a057e0 100644
--- a/src/quick/items/qquicktableview.cpp
+++ b/src/quick/items/qquicktableview.cpp
@@ -748,20 +748,34 @@ void QQuickTableViewPrivate::releaseLoadedItems(QQmlTableInstanceModel::Reusable
void QQuickTableViewPrivate::releaseItem(FxTableItem *fxTableItem, QQmlTableInstanceModel::ReusableFlag reusableFlag)
{
- Q_TABLEVIEW_ASSERT(fxTableItem->item, fxTableItem->index);
+ Q_Q(QQuickTableView);
+ auto item = fxTableItem->item;
+ Q_TABLEVIEW_ASSERT(item, fxTableItem->index);
if (fxTableItem->ownItem) {
- delete fxTableItem->item;
+ delete item;
} else {
// Only QQmlTableInstanceModel supports reusing items
auto releaseFlag = tableModel ?
- tableModel->release(fxTableItem->item, reusableFlag) :
- model->release(fxTableItem->item);
+ tableModel->release(item, reusableFlag) :
+ model->release(item);
if (releaseFlag != QQmlInstanceModel::Destroyed) {
- // When items are not released, it typically means that the item is reused, or
- // that the model is an ObjectModel. If so, we just hide the item instead.
+ // When items are not destroyed, it typically means that the
+ // item is reused, or that the model is an ObjectModel. If
+ // so, we just hide the item instead.
fxTableItem->setVisible(false);
+
+ if (QQuickWindow *window = item->window()) {
+ // If the item (or a descendant) has focus, remove it, so
+ // that the item doesn't enter with focus when it's reused.
+ const auto focusItem = static_cast<QQuickItem *>(window->focusObject());
+ const bool hasFocus = item->isAncestorOf(focusItem);
+ if (hasFocus) {
+ const auto focusChild = QQuickItemPrivate::get(q)->subFocusItem;
+ QQuickWindowPrivate::get(window)->clearFocusInScope(q, focusChild, Qt::OtherFocusReason);
+ }
+ }
}
}
@@ -1717,6 +1731,7 @@ bool QQuickTableViewPrivate::updatePolishIfPossible() const
QQuickTableView::QQuickTableView(QQuickItem *parent)
: QQuickFlickable(*(new QQuickTableViewPrivate), parent)
{
+ setFlag(QQuickItem::ItemIsFocusScope);
connect(this, &QQuickFlickable::contentWidthChanged, this, &QQuickTableView::contentWidthOverrideChanged);
connect(this, &QQuickFlickable::contentHeightChanged, this, &QQuickTableView::contentHeightOverrideChanged);
}
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<QQuickItem *>(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<QQuickItem *>(fxItem->item->property(textInputProp));
+ QCOMPARE(textInput2->hasActiveFocus(), false);
+ }
+}
+
void tst_QQuickTableView::checkTableMargins_data()
{
QTest::addColumn<QVariant>("model");