aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRichard Moe Gustavsen <richard.gustavsen@qt.io>2018-08-29 14:09:55 +0200
committerRichard Moe Gustavsen <richard.gustavsen@qt.io>2018-09-06 13:06:05 +0000
commit77aefc95cf3fbae342004513fd8c9f3a0184eba9 (patch)
tree4310042298458dd94aac126d833f395ea227d00b
parent638f80c317a42d51ff312c077cc84f60538e5498 (diff)
QQmlTableInstanceModel: don't fall back to use DelegateChooser as delegate
If the application uses a DelegateChooser, but the chooser fails to resolve a delegate for a certain index, it should not use itself as the delegate instead. This will cause the application to crash. Instead, we just print a warning, and return nullptr, which will let TableView handle the situation gracefully. Change-Id: Ibaf9da09fd11149362f5b674fc61db47593de10c Reviewed-by: Mitch Curtis <mitch.curtis@qt.io>
-rw-r--r--src/qml/types/qqmltableinstancemodel.cpp10
-rw-r--r--tests/auto/quick/qquicktableview/data/usechooserwithoutdefault.qml64
-rw-r--r--tests/auto/quick/qquicktableview/tst_qquicktableview.cpp12
3 files changed, 82 insertions, 4 deletions
diff --git a/src/qml/types/qqmltableinstancemodel.cpp b/src/qml/types/qqmltableinstancemodel.cpp
index 33d13e3bad..1054158dc8 100644
--- a/src/qml/types/qqmltableinstancemodel.cpp
+++ b/src/qml/types/qqmltableinstancemodel.cpp
@@ -109,19 +109,19 @@ QQmlTableInstanceModel::~QQmlTableInstanceModel()
QQmlComponent *QQmlTableInstanceModel::resolveDelegate(int index)
{
- QQmlComponent *delegate = nullptr;
if (m_delegateChooser) {
const int row = m_adaptorModel.rowAt(index);
const int column = m_adaptorModel.columnAt(index);
+ QQmlComponent *delegate = nullptr;
QQmlAbstractDelegateComponent *chooser = m_delegateChooser;
do {
delegate = chooser->delegate(&m_adaptorModel, row, column);
chooser = qobject_cast<QQmlAbstractDelegateComponent *>(delegate);
} while (chooser);
+ return delegate;
}
- if (!delegate)
- delegate = m_delegate;
- return delegate;
+
+ return m_delegate;
}
QQmlDelegateModelItem *QQmlTableInstanceModel::resolveModelItem(int index)
@@ -132,6 +132,8 @@ QQmlDelegateModelItem *QQmlTableInstanceModel::resolveModelItem(int index)
return modelItem;
QQmlComponent *delegate = resolveDelegate(index);
+ if (!delegate)
+ return nullptr;
// Check if the pool contains an item that can be reused
modelItem = takeFromReusableItemsPool(delegate);
diff --git a/tests/auto/quick/qquicktableview/data/usechooserwithoutdefault.qml b/tests/auto/quick/qquicktableview/data/usechooserwithoutdefault.qml
new file mode 100644
index 0000000000..847500d71f
--- /dev/null
+++ b/tests/auto/quick/qquicktableview/data/usechooserwithoutdefault.qml
@@ -0,0 +1,64 @@
+/****************************************************************************
+**
+** 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
+import Qt.labs.qmlmodels 1.0
+
+Item {
+ width: 640
+ height: 450
+
+ property alias tableView: tableView
+
+ TableView {
+ id: tableView
+ width: 600
+ height: 400
+ delegate: DelegateChooser {
+ DelegateChoice {
+ row: 0
+ delegate: Item {
+ implicitWidth: 100
+ implicitHeight: 100
+ }
+ }
+ }
+ }
+}
diff --git a/tests/auto/quick/qquicktableview/tst_qquicktableview.cpp b/tests/auto/quick/qquicktableview/tst_qquicktableview.cpp
index 4ba36cd687..45b3566574 100644
--- a/tests/auto/quick/qquicktableview/tst_qquicktableview.cpp
+++ b/tests/auto/quick/qquicktableview/tst_qquicktableview.cpp
@@ -135,6 +135,7 @@ private slots:
void checkRowAndColumnChangedButNotIndex();
void checkChangingModelFromDelegate();
void checkRebuildViewportOnly();
+ void useDelegateChooserWithoutDefault();
};
tst_QQuickTableView::tst_QQuickTableView()
@@ -1756,6 +1757,17 @@ void tst_QQuickTableView::checkRebuildViewportOnly()
QCOMPARE(countAfterRebuild, itemCountBeforeRebuild);
}
+void tst_QQuickTableView::useDelegateChooserWithoutDefault()
+{
+ // Check that the application issues a warning (but doesn't e.g
+ // crash) if the delegate chooser doesn't cover all cells
+ QTest::ignoreMessage(QtWarningMsg, QRegularExpression(".*failed"));
+ LOAD_TABLEVIEW("usechooserwithoutdefault.qml");
+ auto model = TestModelAsVariant(2, 1);
+ tableView->setModel(model);
+ WAIT_UNTIL_POLISHED;
+};
+
QTEST_MAIN(tst_QQuickTableView)
#include "tst_qquicktableview.moc"