aboutsummaryrefslogtreecommitdiffstats
path: root/tests
diff options
context:
space:
mode:
authorShawn Rutledge <shawn.rutledge@qt.io>2018-12-19 01:42:11 +0100
committerShawn Rutledge <shawn.rutledge@qt.io>2019-02-15 14:17:21 +0000
commit27c0e9d709aba97bd522fd3e53a53c4ff3c4d71b (patch)
tree51454573dc37dd44463bf0013bacdee608daa956 /tests
parent8a62975ecb554f4c3f131aeaf73858e529398cae (diff)
TableModel: add roleDataProvider callback
As an alternative to trying to write smarter C++ in the data() accessor, we give the user full control of data conversion by calling an external JS function if defined, to map role to the value that data() should return. This enables extracting arbitrary values, converting the data in arbitrary ways, or even doing calculations in case the EditRole stores a formula and the DisplayRole should provide the result, or something like that. This callback is implemented somewhat like TableView.columnWidthProvider, but the arguments are more complex: function(row, column, role, rawData) Change-Id: Ifaf5807f4809e0b5ad1d1c403f65c0707b902f10 Reviewed-by: Mitch Curtis <mitch.curtis@qt.io> Reviewed-by: Shawn Rutledge <shawn.rutledge@qt.io>
Diffstat (limited to 'tests')
-rw-r--r--tests/auto/qml/qqmltablemodel/data/roleDataProvider.qml65
-rw-r--r--tests/auto/qml/qqmltablemodel/tst_qqmltablemodel.cpp19
2 files changed, 84 insertions, 0 deletions
diff --git a/tests/auto/qml/qqmltablemodel/data/roleDataProvider.qml b/tests/auto/qml/qqmltablemodel/data/roleDataProvider.qml
new file mode 100644
index 0000000000..825a1ad071
--- /dev/null
+++ b/tests/auto/qml/qqmltablemodel/data/roleDataProvider.qml
@@ -0,0 +1,65 @@
+/****************************************************************************
+**
+** 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
+
+Item {
+ id: root
+ width: 200
+ height: 200
+
+ property alias testModel: testModel
+
+ TableModel {
+ id: testModel
+ objectName: "testModel"
+ rows: [
+ [ { name: "Rex" }, { age: 3 } ],
+ [ { name: "Buster" }, { age: 5 } ]
+ ]
+ roleDataProvider: function(row, column, role, cellData) {
+ if (role === "display") {
+ // Age will now be in dog years
+ if (cellData.hasOwnProperty("age"))
+ return (cellData.age * 7);
+ else if (column === 0)
+ return (cellData.name);
+ }
+ return cellData;
+ }
+ }
+ TableView {
+ anchors.fill: parent
+ model: testModel
+ delegate: Text {
+ id: textItem
+ text: model.display
+ }
+ }
+}
diff --git a/tests/auto/qml/qqmltablemodel/tst_qqmltablemodel.cpp b/tests/auto/qml/qqmltablemodel/tst_qqmltablemodel.cpp
index 825017e811..276f4e2b9f 100644
--- a/tests/auto/qml/qqmltablemodel/tst_qqmltablemodel.cpp
+++ b/tests/auto/qml/qqmltablemodel/tst_qqmltablemodel.cpp
@@ -56,6 +56,7 @@ private slots:
void setRowsImperatively();
void setRowsMultipleTimes();
void defaultDisplayRoles();
+ void roleDataProvider();
};
void tst_QQmlTableModel::appendRemoveRow()
@@ -859,6 +860,24 @@ void tst_QQmlTableModel::defaultDisplayRoles()
QCOMPARE(model->data(model->index(1, 1, QModelIndex()), roleNames.key("display")).toInt(), 33);
}
+void tst_QQmlTableModel::roleDataProvider()
+{
+ QQuickView view(testFileUrl("roleDataProvider.qml"));
+ QCOMPARE(view.status(), QQuickView::Ready);
+ view.show();
+ QVERIFY(QTest::qWaitForWindowActive(&view));
+
+ QQmlTableModel *model = view.rootObject()->property("testModel").value<QQmlTableModel*>();
+ QVERIFY(model);
+
+ const QHash<int, QByteArray> roleNames = model->roleNames();
+ QVERIFY(roleNames.values().contains("display"));
+ QCOMPARE(model->data(model->index(0, 0, QModelIndex()), roleNames.key("display")).toString(), QLatin1String("Rex"));
+ QCOMPARE(model->data(model->index(0, 1, QModelIndex()), roleNames.key("display")).toInt(), 3 * 7);
+ QCOMPARE(model->data(model->index(1, 0, QModelIndex()), roleNames.key("display")).toString(), QLatin1String("Buster"));
+ QCOMPARE(model->data(model->index(1, 1, QModelIndex()), roleNames.key("display")).toInt(), 5 * 7);
+}
+
QTEST_MAIN(tst_QQmlTableModel)
#include "tst_qqmltablemodel.moc"