##// END OF EJS Templates
Documentation for QML custom model demo
Tero Ahola -
r1385:c13c825c33e5
parent child
Show More
@@ -0,0 +1,29
1 /*!
2 \example demos/qmlcustommodel
3 \title QML Custom Model
4 \subtitle
5
6 This example shows how to use your own QAbstractItemModel derived data model as a data source for a ChartView.
7 \image demos_qmlcustommodel.png
8
9 First we create a ChartView:
10 \snippet ../demos/qmlcustommodel/qml/qmlcustommodel/main.qml 1
11
12 Then our data model that contains the shares of top-5 car manufacturers in Finland for the last 5 years. The model
13 could be constructed from various sources depending on the use case, but here we use static data for the sake of
14 simplicity. Check customtablemodel.cpp, declarativemodel.cpp and plugin.cpp to see how to make your own
15 QAbstractItemModel accessible on QML.
16 \snippet ../demos/qmlcustommodel/qml/qmlcustommodel/main.qml 2
17
18 Then we create several series as children for the same ChartView. First a pie series that illustrates the shares of
19 the car manufacturers in Finland in 2011:
20 \snippet ../demos/qmlcustommodel/qml/qmlcustommodel/main.qml 3
21
22 And one line series for each manufacturer presenting the share between 2007-2011, for example this one is for
23 Volkswagen. The series is not visible by default; it is made visible when the user clicks on the Volkswagen slice
24 on the pie series:
25 \snippet ../demos/qmlcustommodel/qml/qmlcustommodel/main.qml 4
26
27 And finally a bar series that shows the share for all the other manufacturers between 2007-2011:
28 \snippet ../demos/qmlcustommodel/qml/qmlcustommodel/main.qml 5
29 */
@@ -1,109 +1,97
1 1 /****************************************************************************
2 2 **
3 3 ** Copyright (C) 2012 Digia Plc
4 4 ** All rights reserved.
5 5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 6 **
7 7 ** This file is part of the Qt Commercial Charts Add-on.
8 8 **
9 9 ** $QT_BEGIN_LICENSE$
10 10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 11 ** accordance with the Qt Commercial License Agreement provided with the
12 12 ** Software or, alternatively, in accordance with the terms contained in
13 13 ** a written agreement between you and Digia.
14 14 **
15 15 ** If you have questions regarding the use of this file, please use
16 16 ** contact form at http://qt.digia.com
17 17 ** $QT_END_LICENSE$
18 18 **
19 19 ****************************************************************************/
20 20
21 21 #include "declarativemodel.h"
22 22 #include <qdeclarativelist.h>
23 23 #include <QDebug>
24 24
25 25 ////////////// Table model element ///////////////////
26 26
27 27 DeclarativeTableModelElement::DeclarativeTableModelElement(QObject *parent)
28 28 : QObject(parent)
29 29 {
30 30 }
31 31
32 32 QVariantList DeclarativeTableModelElement::values()
33 33 {
34 34 return m_values;
35 35 }
36 36
37 37 void DeclarativeTableModelElement::setValues(QVariantList values)
38 38 {
39 39 m_values = values;
40 40 }
41 41
42 42 ////////////// Table model ///////////////////
43 43
44 44 DeclarativeTableModel::DeclarativeTableModel(QObject *parent) :
45 45 CustomTableModel(parent)
46 46 {
47 47 }
48 48
49 49 void DeclarativeTableModel::classBegin()
50 50 {
51 51 }
52 52
53 53 void DeclarativeTableModel::componentComplete()
54 54 {
55 55 foreach (QObject *child, children()) {
56 56 if (qobject_cast<DeclarativeTableModelElement *>(child)) {
57 57 append(qobject_cast<DeclarativeTableModelElement *>(child)->values());
58 58 }
59 59 }
60 60 }
61 61
62 62 QDeclarativeListProperty<QObject> DeclarativeTableModel::modelChildren()
63 63 {
64 64 return QDeclarativeListProperty<QObject>(this, 0, &DeclarativeTableModel::appendModelChild);
65 65 }
66 66
67 67 void DeclarativeTableModel::appendModelChild(QDeclarativeListProperty<QObject> *list,
68 68 QObject *child)
69 69 {
70 70 // childs are added in componentComplete instead
71 71 Q_UNUSED(list)
72 72 Q_UNUSED(child)
73 73 }
74 74
75 75 void DeclarativeTableModel::append(QVariantList values)
76 76 {
77 77 // qDebug() << "DeclarativeTableModel::append:" << values;
78 78
79 79 while (columnCount() < values.count())
80 80 insertColumn(columnCount());
81 81
82 82 insertRow(rowCount());
83 83
84 84 QModelIndex beginIndex = QModelIndex();
85 85 QModelIndex endIndex = QModelIndex();
86 86 for (int i(0); i < values.count(); i++) {
87 87 QModelIndex modelIndex = createIndex(rowCount() - 1, i);
88 88 if (i == 0)
89 89 beginIndex = modelIndex;
90 90 if (i == (values.count() - 1))
91 91 endIndex = modelIndex;
92 92 setData(modelIndex, values.at(i));
93 93 }
94 94 dataChanged(beginIndex, endIndex);
95 95 }
96 96
97 //void DeclarativeTableModel::appendPoint(QXYModelMapper *mapper, DeclarativeXyPoint *point)
98 //{
99 // Q_UNUSED(mapper)
100 // Q_UNUSED(point)
101 // // TODO: XYModelMapper implementation has change, this code has to be updated.
102 //// qDebug() << "DeclarativeTableModel::appendPoint:" << point;
103 //// QVariantList values;
104 //// values.insert(mapper->mapX(), point->x());
105 //// values.insert(mapper->mapY(), point->y());
106 //// append(values);
107 //}
108
109 97 #include "moc_declarativemodel.cpp"
@@ -1,64 +1,63
1 1 /****************************************************************************
2 2 **
3 3 ** Copyright (C) 2012 Digia Plc
4 4 ** All rights reserved.
5 5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 6 **
7 7 ** This file is part of the Qt Commercial Charts Add-on.
8 8 **
9 9 ** $QT_BEGIN_LICENSE$
10 10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 11 ** accordance with the Qt Commercial License Agreement provided with the
12 12 ** Software or, alternatively, in accordance with the terms contained in
13 13 ** a written agreement between you and Digia.
14 14 **
15 15 ** If you have questions regarding the use of this file, please use
16 16 ** contact form at http://qt.digia.com
17 17 ** $QT_END_LICENSE$
18 18 **
19 19 ****************************************************************************/
20 20
21 21 #ifndef DECLARATIVEMODEL_H
22 22 #define DECLARATIVEMODEL_H
23 23
24 24 #include "customtablemodel.h"
25 25 #include <QDeclarativeListProperty>
26 26 #include <QVariant>
27 27 #include <QDeclarativeParserStatus>
28 28
29 29 class DeclarativeTableModelElement : public QObject
30 30 {
31 31 Q_OBJECT
32 32 Q_PROPERTY(QVariantList values READ values WRITE setValues)
33 33
34 34 public:
35 35 explicit DeclarativeTableModelElement(QObject *parent = 0);
36 36 QVariantList values();
37 37 void setValues(QVariantList values);
38 38 private:
39 39 QVariantList m_values;
40 40 };
41 41
42 42 class DeclarativeTableModel : public CustomTableModel, public QDeclarativeParserStatus
43 43 {
44 44 Q_OBJECT
45 45 Q_INTERFACES(QDeclarativeParserStatus)
46 46 Q_PROPERTY(QDeclarativeListProperty<QObject> modelChildren READ modelChildren)
47 47 Q_CLASSINFO("DefaultProperty", "modelChildren")
48 48
49 49 public:
50 50 explicit DeclarativeTableModel(QObject *parent = 0);
51 51 QDeclarativeListProperty<QObject> modelChildren();
52 // void appendPoint(QXYModelMapper *mapper, DeclarativeXyPoint *point);
53 52
54 53 public: // from QDeclarativeParserStatus
55 54 void classBegin();
56 55 void componentComplete();
57 56
58 57 public Q_SLOTS:
59 58 void append(QVariantList slices);
60 59 static void appendModelChild(QDeclarativeListProperty<QObject> *list,
61 60 QObject *element);
62 61 };
63 62
64 63 #endif // DECLARATIVEMODEL_H
@@ -1,146 +1,155
1 1 /****************************************************************************
2 2 **
3 3 ** Copyright (C) 2012 Digia Plc
4 4 ** All rights reserved.
5 5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 6 **
7 7 ** This file is part of the Qt Commercial Charts Add-on.
8 8 **
9 9 ** $QT_BEGIN_LICENSE$
10 10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 11 ** accordance with the Qt Commercial License Agreement provided with the
12 12 ** Software or, alternatively, in accordance with the terms contained in
13 13 ** a written agreement between you and Digia.
14 14 **
15 15 ** If you have questions regarding the use of this file, please use
16 16 ** contact form at http://qt.digia.com
17 17 ** $QT_END_LICENSE$
18 18 **
19 19 ****************************************************************************/
20 20
21 21 import QtQuick 1.0
22 22 import QtCommercial.Chart 1.0
23 23 import QmlCustomModel 1.0
24 24
25 25 Rectangle {
26 26 width: parent.width
27 height: parent.height
27 height: parent.heigh
28 28
29 //![1]
29 30 ChartView {
30 31 id: chartView
31 32 title: "Top-5 car brand shares in Finland"
32 33 anchors.fill: parent
33 theme: ChartView.ChartThemeLight
34 34 axisX.max: 10
35 35 axisX.min: 0
36 36 axisY.max: 20
37 37 axisY.min: 0
38 38 animationOptions: ChartView.SeriesAnimations
39 39 axisXLabels: [0, "2007", 1, "2008", 2, "2009", 3, "2010", 4, "2011", 5, "2012"]
40 // ...
41 //![1]
40 42
41 // For dynamic data we use a custom data model derived from QAbstractiItemModel
43 //![2]
42 44 CustomModel {
43 45 id: customModel
44 46 CustomModelElement { values: [0, "Manufacturer", 0, 1, 2, 3, 4] }
45 47 CustomModelElement { values: [1, "Volkswagen", 10.3, 12.0, 12.8, 13.0, 13.8] }
46 48 CustomModelElement { values: [2, "Toyota", 13.8, 13.5, 16.2, 13.7, 10.7] }
47 49 CustomModelElement { values: [3, "Ford", 6.4, 7.1, 8.9, 8.2, 8.6] }
48 50 CustomModelElement { values: [4, "Skoda", 4.7, 5.8, 6.9, 8.3, 8.2] }
49 51 CustomModelElement { values: [5, "Volvo", 7.1, 6.7, 6.5, 6.3, 7.0] }
50 52 CustomModelElement { values: [6, "Others", 57.7, 54.9, 48.7, 50.5, 51.7] }
51 53 }
54 //![2]
52 55
56 //![5]
53 57 BarSeries {
54 58 name: "Others"
55 59 barMargin: 0
56 60 visible: false
57 61 HBarModelMapper {
58 62 model: customModel
59 63 firstBarSetRow: 6
60 64 lastBarSetRow: 6
61 65 first: 2
62 66 }
63 67 }
68 //![5]
64 69
70 //![4]
65 71 LineSeries {
66 72 name: "Volkswagen"
67 73 visible: false
68 74 HXYModelMapper {
69 75 model: customModel
70 76 xRow: 0
71 77 yRow: 1
72 78 first: 2
73 79 }
74 80 }
81 //![4]
75 82
76 83 LineSeries {
77 84 name: "Toyota"
78 85 visible: false
79 86 HXYModelMapper {
80 87 model: customModel
81 88 xRow: 0
82 89 yRow: 2
83 90 first: 2
84 91 }
85 92 }
86 93
87 94 LineSeries {
88 95 name: "Ford"
89 96 visible: false
90 97 HXYModelMapper {
91 98 model: customModel
92 99 xRow: 0
93 100 yRow: 2
94 101 first: 2
95 102 }
96 103 }
97 104
98 105 LineSeries {
99 106 name: "Skoda"
100 107 visible: false
101 108 HXYModelMapper {
102 109 model: customModel
103 110 xRow: 0
104 111 yRow: 3
105 112 first: 2
106 113 }
107 114 }
108 115
109 116 LineSeries {
110 117 name: "Volvo"
111 118 visible: false
112 119 HXYModelMapper {
113 120 model: customModel
114 121 xRow: 0
115 122 yRow: 4
116 123 first: 2
117 124 }
118 125 }
119 126
127 //![3]
120 128 PieSeries {
121 129 id: pieSeries
122 130 size: 0.4
123 131 horizontalPosition: 0.7
124 132 verticalPosition: 0.4
125 133 onClicked: {
126 134 // Show the selection by exploding the slice
127 135 slice.exploded = !slice.exploded;
128 136
129 137 // Update the line series to show the yearly data for this slice
130 138 for (var i = 0; i < chartView.count; i++) {
131 139 if (chartView.series(i).name == slice.label) {
132 140 chartView.series(i).visible = slice.exploded;
133 141 }
134 142 }
135 143 }
136 144 }
145 //![3]
137 146
138 147 VPieModelMapper {
139 148 series: pieSeries
140 149 model: customModel
141 150 labelsColumn: 1
142 151 valuesColumn: 2
143 152 first: 1
144 153 }
145 154 }
146 155 }
@@ -1,26 +1,27
1 1 /*!
2 2 \page demos.html
3 3 \title Demos
4 4 \keyword Demos
5 5
6 6 \raw HTML
7 7 <table cellpadding="2" cellspacing="1" border="0" width="100%" class="indextable">
8 8 <tr>
9 9 <th class="titleheader" width="33%">
10 10 List of demos
11 11 </th>
12 12 </tr>
13 13 <tr>
14 14 <td valign="top">
15 15 <ul>
16 16 <li><a href="demos-chartthemes.html">Chart themes</a></li>
17 17 <li><a href="demos-piechartcustomization.html">Pie chart customization</a></li>
18 18 <li><a href="demos-dynamicspline.html">Dynamic spline chart</a></li>
19 19 <li><a href="demos-qmlchart.html">Qml Basic Charts</a></li>
20 20 <li><a href="demos-qmlweather.html">Qml Weather</a></li>
21 <li><a href="demos-qmlcustommodel.html">Qml Custom Model</a></li>
21 22 </ul>
22 23 </td>
23 24 </tr>
24 25 </table>
25 26 \endraw
26 27 */
General Comments 0
You need to be logged in to leave comments. Login now