aboutsummaryrefslogtreecommitdiffstats
path: root/tests/auto/qml/qqmltablemodel
diff options
context:
space:
mode:
authorShawn Rutledge <shawn.rutledge@qt.io>2018-12-18 23:48:11 +0100
committerShawn Rutledge <shawn.rutledge@qt.io>2019-02-22 17:32:10 +0000
commitb15654d77f7886ae23f06d760b90973983e12779 (patch)
tree8e21d6e4022c77e41180190ccd66c37b57db4cf1 /tests/auto/qml/qqmltablemodel
parent22435f49499342ff102411c025efb8007dfa62a2 (diff)
TableModel: add QML-invokable overloads for index(), data() and setData()
data() needs to be invokable because it does more processing to ensure that DisplayRole will give us something, and we want to be able to access it in JS code too, not only via the role context property in a delegate binding. index() needs to be invokable to use it when calling data(). It's useful for setData() to be invokable so that TableView delegates can be used to edit the model. However since we don't normally expose numeric roles to QML, we use string roles, and have to look them up in the roleNames() hash. Change-Id: I38904ac995fc2bac514bde2dd37a95e0b911c00c Reviewed-by: Richard Moe Gustavsen <richard.gustavsen@qt.io> Reviewed-by: Mitch Curtis <mitch.curtis@qt.io>
Diffstat (limited to 'tests/auto/qml/qqmltablemodel')
-rw-r--r--tests/auto/qml/qqmltablemodel/data/dataAndSetData.qml66
-rw-r--r--tests/auto/qml/qqmltablemodel/tst_qqmltablemodel.cpp22
2 files changed, 88 insertions, 0 deletions
diff --git a/tests/auto/qml/qqmltablemodel/data/dataAndSetData.qml b/tests/auto/qml/qqmltablemodel/data/dataAndSetData.qml
new file mode 100644
index 0000000000..d61c50ba2c
--- /dev/null
+++ b/tests/auto/qml/qqmltablemodel/data/dataAndSetData.qml
@@ -0,0 +1,66 @@
+/****************************************************************************
+**
+** Copyright (C) 2019 The Qt Company Ltd.
+** Contact: https://www.qt.io/licensing/
+**
+** This file is part of the test suite of the Qt Toolkit.
+**
+** $QT_BEGIN_LICENSE:GPL-EXCEPT$
+** 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 General Public License Usage
+** Alternatively, this file may be used under the terms of the GNU
+** General Public License version 3 as published by the Free Software
+** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
+** 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.12
+import Qt.labs.qmlmodels 1.0
+
+TableView {
+ width: 200; height: 200
+ model: TableModel {
+ id: testModel
+ objectName: "testModel"
+ rows: [
+ [
+ { name: "John", someOtherRole1: "foo" }, // column 0
+ { age: 22, someOtherRole2: "foo" } // column 1
+ ],
+ [
+ { name: "Oliver", someOtherRole1: "foo" }, // column 0
+ { age: 33, someOtherRole2: "foo" } // column 1
+ ]
+ ]
+
+ // This is silly: in real life, store the birthdate instead of the age,
+ // and let the delegate calculate the age, so it won't need updating
+ function happyBirthday(dude) {
+ var row = -1;
+ for (var r = 0; row < 0 && r < testModel.rowCount; ++r)
+ if (testModel.data(testModel.index(r, 0), "name") === dude)
+ row = r;
+ var index = testModel.index(row, 1)
+ testModel.setData(index, "age", testModel.data(index, "age") + 1)
+ }
+ }
+ delegate: Text {
+ id: textItem
+ text: model.display
+ TapHandler {
+ onTapped: testModel.happyBirthday(testModel.data(testModel.index(row, 0), "name"))
+ }
+ }
+}
diff --git a/tests/auto/qml/qqmltablemodel/tst_qqmltablemodel.cpp b/tests/auto/qml/qqmltablemodel/tst_qqmltablemodel.cpp
index 276f4e2b9f..eb1fbda45a 100644
--- a/tests/auto/qml/qqmltablemodel/tst_qqmltablemodel.cpp
+++ b/tests/auto/qml/qqmltablemodel/tst_qqmltablemodel.cpp
@@ -57,6 +57,7 @@ private slots:
void setRowsMultipleTimes();
void defaultDisplayRoles();
void roleDataProvider();
+ void dataAndEditing();
};
void tst_QQmlTableModel::appendRemoveRow()
@@ -878,6 +879,27 @@ void tst_QQmlTableModel::roleDataProvider()
QCOMPARE(model->data(model->index(1, 1, QModelIndex()), roleNames.key("display")).toInt(), 5 * 7);
}
+void tst_QQmlTableModel::dataAndEditing()
+{
+ QQuickView view(testFileUrl("dataAndSetData.qml"));
+ QCOMPARE(view.status(), QQuickView::Ready);
+ view.show();
+ QVERIFY(QTest::qWaitForWindowActive(&view));
+
+ QQmlTableModel *model = view.rootObject()->property("model").value<QQmlTableModel*>();
+ QVERIFY(model);
+
+ const QHash<int, QByteArray> roleNames = model->roleNames();
+ QVERIFY(roleNames.values().contains("display"));
+ QCOMPARE(model->data(model->index(0, 1, QModelIndex()), roleNames.key("display")).toInt(), 22);
+ QCOMPARE(model->data(model->index(1, 1, QModelIndex()), roleNames.key("display")).toInt(), 33);
+ QVERIFY(QMetaObject::invokeMethod(model, "happyBirthday", Q_ARG(QVariant, QLatin1String("Oliver"))));
+ QCOMPARE(model->data(model->index(0, 0, QModelIndex()), roleNames.key("display")).toString(), QLatin1String("John"));
+ QCOMPARE(model->data(model->index(0, 1, QModelIndex()), roleNames.key("display")).toInt(), 22);
+ QCOMPARE(model->data(model->index(1, 0, QModelIndex()), roleNames.key("display")).toString(), QLatin1String("Oliver"));
+ QCOMPARE(model->data(model->index(1, 1, QModelIndex()), roleNames.key("display")).toInt(), 34);
+}
+
QTEST_MAIN(tst_QQmlTableModel)
#include "tst_qqmltablemodel.moc"