summaryrefslogtreecommitdiff
path: root/tests/manual/tableviewmodels
diff options
context:
space:
mode:
authorNils Jeisecke <jeisecke@saltation.de>2013-01-07 10:32:40 +0100
committerThe Qt Project <gerrit-noreply@qt-project.org>2013-01-09 15:22:40 +0100
commit0ab3b717e97b5f03f5964b6b5629c7a87bc45a43 (patch)
tree1f061e8af6922cd0399b8caec5a0bf856d3b71d1 /tests/manual/tableviewmodels
parent58532c55d7f893ec440bd0cc06f0c154256c3316 (diff)
downloadqtquickcontrols-0ab3b717e97b5f03f5964b6b5629c7a87bc45a43.tar.gz
Fix TableView delegate to work with C++ models again.
Use the views's delegate 'model' property for accessing both Qml ListModel and C++ QAbstractItemModel values. For Qml objects, QObject and QObjectList based models modelData properties will be used. For models without roles (Integer, Array, QStringList) modelData is used directly. A new manual test for all model types is included. Change-Id: I9934ff9737e1b687b852f2413b2d7484248c4e6e Reviewed-by: Jens Bache-Wiig <jens.bache-wiig@digia.com>
Diffstat (limited to 'tests/manual/tableviewmodels')
-rw-r--r--tests/manual/tableviewmodels/main.cpp75
-rw-r--r--tests/manual/tableviewmodels/qml/main.qml127
-rw-r--r--tests/manual/tableviewmodels/resources.qrc5
-rw-r--r--tests/manual/tableviewmodels/tableviewmodels.pro7
-rw-r--r--tests/manual/tableviewmodels/testmodel.cpp61
-rw-r--r--tests/manual/tableviewmodels/testmodel.h73
6 files changed, 348 insertions, 0 deletions
diff --git a/tests/manual/tableviewmodels/main.cpp b/tests/manual/tableviewmodels/main.cpp
new file mode 100644
index 00000000..07e958bc
--- /dev/null
+++ b/tests/manual/tableviewmodels/main.cpp
@@ -0,0 +1,75 @@
+/****************************************************************************
+**
+** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
+** Contact: http://www.qt-project.org/legal
+**
+** This file is part of the Qt Components project.
+**
+** $QT_BEGIN_LICENSE:BSD$
+** You may use this file under the terms of the BSD license as follows:
+**
+** "Redistribution and use in source and binary forms, with or without
+** modification, are permitted provided that the following conditions are
+** met:
+** * Redistributions of source code must retain the above copyright
+** notice, this list of conditions and the following disclaimer.
+** * Redistributions in binary form must reproduce the above copyright
+** notice, this list of conditions and the following disclaimer in
+** the documentation and/or other materials provided with the
+** distribution.
+** * Neither the name of Digia Plc and its Subsidiary(-ies) nor the names
+** of its contributors may be used to endorse or promote products derived
+** from this software without specific prior written permission.
+**
+**
+** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
+**
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+
+#include <QApplication>
+#include <QtQuick>
+
+#include "testmodel.h"
+
+int main(int argc, char *argv[])
+{
+ QApplication app(argc, argv);
+
+ QQuickView viewer;
+ viewer.setResizeMode(QQuickView::SizeRootObjectToView);
+
+ QObjectList model_qobjectlist;
+ model_qobjectlist << new TestObject(0);
+ model_qobjectlist << new TestObject(1);
+ model_qobjectlist << new TestObject(2);
+ viewer.rootContext()->setContextProperty("model_qobjectlist", QVariant::fromValue(model_qobjectlist));
+
+ TestObject *model_qobject = new TestObject(0);
+ viewer.rootContext()->setContextProperty("model_qobject", QVariant::fromValue(model_qobject));
+
+ QStringList model_qstringlist;
+ model_qstringlist << QStringLiteral("A");
+ model_qstringlist << QStringLiteral("B");
+ model_qstringlist << QStringLiteral("C");
+ viewer.rootContext()->setContextProperty("model_qstringlist", model_qstringlist);
+
+ TestModel *model_qaim = new TestModel;
+ viewer.rootContext()->setContextProperty("model_qaim", model_qaim);
+
+ viewer.setSource(QStringLiteral("qrc:/qml/main.qml"));
+ viewer.show();
+
+ return app.exec();
+}
diff --git a/tests/manual/tableviewmodels/qml/main.qml b/tests/manual/tableviewmodels/qml/main.qml
new file mode 100644
index 00000000..2c4c359c
--- /dev/null
+++ b/tests/manual/tableviewmodels/qml/main.qml
@@ -0,0 +1,127 @@
+/****************************************************************************
+**
+** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
+** Contact: http://www.qt-project.org/legal
+**
+** This file is part of the Qt Components project.
+**
+** $QT_BEGIN_LICENSE:BSD$
+** You may use this file under the terms of the BSD license as follows:
+**
+** "Redistribution and use in source and binary forms, with or without
+** modification, are permitted provided that the following conditions are
+** met:
+** * Redistributions of source code must retain the above copyright
+** notice, this list of conditions and the following disclaimer.
+** * Redistributions in binary form must reproduce the above copyright
+** notice, this list of conditions and the following disclaimer in
+** the documentation and/or other materials provided with the
+** distribution.
+** * Neither the name of Digia Plc and its Subsidiary(-ies) nor the names
+** of its contributors may be used to endorse or promote products derived
+** from this software without specific prior written permission.
+**
+**
+** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
+**
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+
+import QtQuick 2.0
+import QtDesktop 1.0
+
+Rectangle {
+ width: 360
+ height: 600
+
+ ListModel {
+ id: model_listmodel
+ ListElement { value: "A" }
+ ListElement { value: "B" }
+ ListElement { value: "C" }
+ }
+
+ Column {
+ anchors { left: parent.left; right: parent.right }
+ TableView {
+ model: model_listmodel // qml
+ anchors { left: parent.left; right: parent.right }
+ height: 70
+ TableColumn {
+ role: "value"
+ width: 100
+ }
+ }
+ TableView {
+ model: 3 // qml
+ anchors { left: parent.left; right: parent.right }
+ height: 70
+ TableColumn {
+ width: 100
+ }
+ }
+ TableView {
+ model: ["A", "B", "C"] // qml
+ anchors { left: parent.left; right: parent.right }
+ height: 70
+ TableColumn {
+ width: 100
+ }
+ }
+ TableView {
+ model: Item { x: 10 } // qml
+ anchors { left: parent.left; right: parent.right }
+ height: 70
+ TableColumn {
+ role: "x"
+ width: 100
+ }
+ }
+ TableView {
+ model: model_qobjectlist // c++
+ anchors { left: parent.left; right: parent.right }
+ height: 70
+ TableColumn {
+ role: "value"
+ width: 100
+ }
+ }
+ TableView {
+ model: model_qaim // c++
+ anchors { left: parent.left; right: parent.right }
+ height: 70
+ TableColumn {
+ role: "test"
+ width: 100
+ }
+ }
+ TableView {
+ model: model_qstringlist // c++
+ anchors { left: parent.left; right: parent.right }
+ height: 70
+ TableColumn {
+ width: 100
+ }
+ }
+ TableView {
+ model: model_qobject // c++
+ anchors { left: parent.left; right: parent.right }
+ height: 70
+ TableColumn {
+ role: "value"
+ width: 100
+ }
+ }
+ }
+}
diff --git a/tests/manual/tableviewmodels/resources.qrc b/tests/manual/tableviewmodels/resources.qrc
new file mode 100644
index 00000000..69145a82
--- /dev/null
+++ b/tests/manual/tableviewmodels/resources.qrc
@@ -0,0 +1,5 @@
+<RCC>
+ <qresource prefix="/">
+ <file>qml/main.qml</file>
+ </qresource>
+</RCC>
diff --git a/tests/manual/tableviewmodels/tableviewmodels.pro b/tests/manual/tableviewmodels/tableviewmodels.pro
new file mode 100644
index 00000000..89f56db8
--- /dev/null
+++ b/tests/manual/tableviewmodels/tableviewmodels.pro
@@ -0,0 +1,7 @@
+SOURCES += main.cpp testmodel.cpp
+HEADERS += testmodel.h
+
+RESOURCES += \
+ resources.qrc
+
+QT += quick widgets
diff --git a/tests/manual/tableviewmodels/testmodel.cpp b/tests/manual/tableviewmodels/testmodel.cpp
new file mode 100644
index 00000000..b4cc95c7
--- /dev/null
+++ b/tests/manual/tableviewmodels/testmodel.cpp
@@ -0,0 +1,61 @@
+/****************************************************************************
+**
+** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
+** Contact: http://www.qt-project.org/legal
+**
+** This file is part of the Qt Components project.
+**
+** $QT_BEGIN_LICENSE:BSD$
+** You may use this file under the terms of the BSD license as follows:
+**
+** "Redistribution and use in source and binary forms, with or without
+** modification, are permitted provided that the following conditions are
+** met:
+** * Redistributions of source code must retain the above copyright
+** notice, this list of conditions and the following disclaimer.
+** * Redistributions in binary form must reproduce the above copyright
+** notice, this list of conditions and the following disclaimer in
+** the documentation and/or other materials provided with the
+** distribution.
+** * Neither the name of Digia Plc and its Subsidiary(-ies) nor the names
+** of its contributors may be used to endorse or promote products derived
+** from this software without specific prior written permission.
+**
+**
+** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
+**
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+
+#include "testmodel.h"
+
+TestModel::TestModel(QObject *parent) :
+ QAbstractListModel(parent)
+{
+}
+
+QVariant TestModel::data(const QModelIndex &index, int role) const
+{
+ if (role == TestRole)
+ return QString("Row %1").arg(index.row());
+ else
+ return QVariant();
+}
+
+QHash<int, QByteArray> TestModel::roleNames() const
+{
+ QHash<int, QByteArray> rn = QAbstractItemModel::roleNames();
+ rn[TestRole] = "test";
+ return rn;
+}
diff --git a/tests/manual/tableviewmodels/testmodel.h b/tests/manual/tableviewmodels/testmodel.h
new file mode 100644
index 00000000..4815196f
--- /dev/null
+++ b/tests/manual/tableviewmodels/testmodel.h
@@ -0,0 +1,73 @@
+/****************************************************************************
+**
+** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
+** Contact: http://www.qt-project.org/legal
+**
+** This file is part of the Qt Components project.
+**
+** $QT_BEGIN_LICENSE:BSD$
+** You may use this file under the terms of the BSD license as follows:
+**
+** "Redistribution and use in source and binary forms, with or without
+** modification, are permitted provided that the following conditions are
+** met:
+** * Redistributions of source code must retain the above copyright
+** notice, this list of conditions and the following disclaimer.
+** * Redistributions in binary form must reproduce the above copyright
+** notice, this list of conditions and the following disclaimer in
+** the documentation and/or other materials provided with the
+** distribution.
+** * Neither the name of Digia Plc and its Subsidiary(-ies) nor the names
+** of its contributors may be used to endorse or promote products derived
+** from this software without specific prior written permission.
+**
+**
+** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
+**
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+
+#ifndef TESTMODEL_H
+#define TESTMODEL_H
+
+#include <QAbstractListModel>
+
+class TestObject : public QObject
+{
+ Q_OBJECT
+ Q_PROPERTY(int value READ value CONSTANT)
+public:
+ TestObject(int value) : m_value(value) {}
+ int value() const { return m_value; }
+private:
+ int m_value;
+};
+
+class TestModel : public QAbstractListModel
+{
+ Q_OBJECT
+public:
+ explicit TestModel(QObject *parent = 0);
+
+ enum {
+ TestRole = Qt::UserRole + 1
+ };
+
+ QVariant data(const QModelIndex &index, int role) const;
+ int rowCount(const QModelIndex &parent) const { Q_UNUSED(parent); return 10; }
+
+ QHash<int, QByteArray> roleNames() const;
+};
+
+#endif // TESTMODEL_H