##// END OF EJS Templates
Header data to QML custom model demo
Tero Ahola -
r1387:122ce0e59427
parent child
Show More
1 NO CONTENT: new file 100644, binary diff hidden
@@ -1,122 +1,136
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 "customtablemodel.h"
22 22 #include <QVector>
23 23 #include <QRect>
24 24 #include <QColor>
25 #include <QDebug>
25 26
26 27 CustomTableModel::CustomTableModel(QObject *parent) :
27 28 QAbstractTableModel(parent),
28 29 m_columnCount(0),
29 30 m_rowCount(0)
30 31 {
31 32 }
32 33
33 34 int CustomTableModel::rowCount(const QModelIndex & parent) const
34 35 {
35 36 Q_UNUSED(parent)
36 37 return m_data.count();
37 38 }
38 39
39 40 int CustomTableModel::columnCount(const QModelIndex & parent) const
40 41 {
41 42 Q_UNUSED(parent)
42 43 return m_columnCount;
43 44 }
44 45
45 QVariant CustomTableModel::headerData (int section, Qt::Orientation orientation, int role ) const
46 QVariant CustomTableModel::headerData(int section, Qt::Orientation orientation, int role) const
46 47 {
47 48 if (role != Qt::DisplayRole)
48 49 return QVariant();
49 50
50 51 if (orientation == Qt::Horizontal) {
51 if (section % 2 == 0)
52 return "x";
52 if (m_rowHeaders.count() > section)
53 return m_rowHeaders[section];
53 54 else
54 return "y";
55 return QAbstractTableModel::headerData(section, orientation, role);
55 56 } else {
56 return QString("%1").arg(section + 1);
57 return QAbstractTableModel::headerData(section, orientation, role);
57 58 }
58 59 }
59 60
61 bool CustomTableModel::setHeaderData(int section, Qt::Orientation orientation, const QVariant &value, int role)
62 {
63 if (orientation == Qt::Horizontal) {
64 while (m_rowHeaders.count() <= section)
65 m_rowHeaders.append(QVariant());
66 m_rowHeaders.replace(section, value);
67 } else {
68 return QAbstractTableModel::setHeaderData(section, orientation, value, role);
69 }
70 emit headerDataChanged(orientation, section, section);
71 return true;
72 }
73
60 74 QVariant CustomTableModel::data(const QModelIndex &index, int role) const
61 75 {
62 76 if (role == Qt::DisplayRole) {
63 77 return m_data[index.row()]->at(index.column());
64 78 } else if (role == Qt::EditRole) {
65 79 return m_data[index.row()]->at(index.column());
66 80 }
67 81 return QVariant();
68 82 }
69 83
70 84 bool CustomTableModel::setData(const QModelIndex &index, const QVariant &value, int role)
71 85 {
72 86 if (index.isValid() && role == Qt::EditRole) {
73 87 m_data[index.row()]->replace(index.column(), value);
74 88 emit dataChanged(index, index);
75 89 return true;
76 90 }
77 91 return false;
78 92 }
79 93
80 94 QVariant CustomTableModel::at(int row, int column)
81 95 {
82 96 return data(index(row, column));
83 97 }
84 98
85 99 void CustomTableModel::insertColumn(int column, const QModelIndex &parent)
86 100 {
87 101 beginInsertColumns(parent, column, column);
88 102 m_columnCount++;
89 103 endInsertColumns();
90 104 }
91 105
92 106 void CustomTableModel::insertRow(int row, const QModelIndex &parent)
93 107 {
94 108 beginInsertRows(parent, row, row);
95 109 QVector<QVariant>* dataVec = new QVector<QVariant>(m_columnCount);
96 110 m_data.insert(row, dataVec);
97 111 endInsertRows();
98 112 }
99 113
100 114 bool CustomTableModel::removeRow(int row, const QModelIndex &parent)
101 115 {
102 116 return QAbstractTableModel::removeRow(row, parent);
103 117 }
104 118
105 119 bool CustomTableModel::removeRows(int row, int count, const QModelIndex &parent)
106 120 {
107 121 beginRemoveRows(parent, row, row + count - 1);
108 122 bool removed(false);
109 123 for (int i(row); i < (row + count); i++) {
110 124 m_data.removeAt(i);
111 125 removed = true;
112 126 }
113 127 endRemoveRows();
114 128 return removed;
115 129 }
116 130
117 131 Qt::ItemFlags CustomTableModel::flags ( const QModelIndex & index ) const
118 132 {
119 133 return QAbstractItemModel::flags(index) | Qt::ItemIsEditable;
120 134 }
121 135
122 136 #include "moc_customtablemodel.cpp"
@@ -1,54 +1,56
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 CUSTOMTABLEMODEL_H
22 22 #define CUSTOMTABLEMODEL_H
23 23
24 24 #include <QAbstractTableModel>
25 25 #include <QHash>
26 26
27 27 class CustomTableModel : public QAbstractTableModel
28 28 {
29 29 Q_OBJECT
30 30 Q_PROPERTY(int rowCount READ rowCount)
31 31 Q_PROPERTY(int columnCount READ columnCount)
32 32
33 33 public:
34 34 explicit CustomTableModel(QObject *parent = 0);
35 35
36 36 int rowCount ( const QModelIndex & parent = QModelIndex() ) const;
37 37 int columnCount ( const QModelIndex & parent = QModelIndex() ) const;
38 QVariant headerData (int section, Qt::Orientation orientation, int role = Qt::DisplayRole ) const;
38 QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole ) const;
39 bool setHeaderData(int section, Qt::Orientation orientation, const QVariant &value, int role = Qt::EditRole);
39 40 QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const;
40 41 bool setData ( const QModelIndex & index, const QVariant & value, int role = Qt::EditRole );
41 42 Qt::ItemFlags flags ( const QModelIndex & index ) const;
42 43 void insertColumn(int column, const QModelIndex &parent = QModelIndex());
43 44 void insertRow(int row, const QModelIndex &parent = QModelIndex());
44 45 Q_INVOKABLE bool removeRows(int row, int count, const QModelIndex & parent = QModelIndex());
45 46 Q_INVOKABLE bool removeRow (int row, const QModelIndex &parent = QModelIndex());
46 47 Q_INVOKABLE QVariant at(int row, int column);
47 48
48 49 private:
49 50 QList<QVector<QVariant> * > m_data;
51 QList<QVariant> m_rowHeaders;
50 52 int m_columnCount;
51 53 int m_rowCount;
52 54 };
53 55
54 56 #endif // CUSTOMTABLEMODEL_H
@@ -1,97 +1,109
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 QString DeclarativeTableModelElement::rowHeader()
33 {
34 return m_rowHeader;
35 }
36
37 void DeclarativeTableModelElement::setRowHeader(QString header)
38 {
39 m_rowHeader = header;
40 }
41
32 42 QVariantList DeclarativeTableModelElement::values()
33 43 {
34 44 return m_values;
35 45 }
36 46
37 47 void DeclarativeTableModelElement::setValues(QVariantList values)
38 48 {
39 49 m_values = values;
40 50 }
41 51
42 52 ////////////// Table model ///////////////////
43 53
44 54 DeclarativeTableModel::DeclarativeTableModel(QObject *parent) :
45 55 CustomTableModel(parent)
46 56 {
47 57 }
48 58
49 59 void DeclarativeTableModel::classBegin()
50 60 {
51 61 }
52 62
53 63 void DeclarativeTableModel::componentComplete()
54 64 {
55 65 foreach (QObject *child, children()) {
56 66 if (qobject_cast<DeclarativeTableModelElement *>(child)) {
57 append(qobject_cast<DeclarativeTableModelElement *>(child)->values());
67 DeclarativeTableModelElement *element = qobject_cast<DeclarativeTableModelElement *>(child);
68 append(element->values());
69 setHeaderData(rowCount() - 1, Qt::Horizontal, element->rowHeader());
58 70 }
59 71 }
60 72 }
61 73
62 74 QDeclarativeListProperty<QObject> DeclarativeTableModel::modelChildren()
63 75 {
64 76 return QDeclarativeListProperty<QObject>(this, 0, &DeclarativeTableModel::appendModelChild);
65 77 }
66 78
67 79 void DeclarativeTableModel::appendModelChild(QDeclarativeListProperty<QObject> *list,
68 80 QObject *child)
69 81 {
70 82 // childs are added in componentComplete instead
71 83 Q_UNUSED(list)
72 84 Q_UNUSED(child)
73 85 }
74 86
75 87 void DeclarativeTableModel::append(QVariantList values)
76 88 {
77 89 // qDebug() << "DeclarativeTableModel::append:" << values;
78 90
79 91 while (columnCount() < values.count())
80 92 insertColumn(columnCount());
81 93
82 94 insertRow(rowCount());
83 95
84 96 QModelIndex beginIndex = QModelIndex();
85 97 QModelIndex endIndex = QModelIndex();
86 98 for (int i(0); i < values.count(); i++) {
87 99 QModelIndex modelIndex = createIndex(rowCount() - 1, i);
88 100 if (i == 0)
89 101 beginIndex = modelIndex;
90 102 if (i == (values.count() - 1))
91 103 endIndex = modelIndex;
92 104 setData(modelIndex, values.at(i));
93 105 }
94 106 dataChanged(beginIndex, endIndex);
95 107 }
96 108
97 109 #include "moc_declarativemodel.cpp"
@@ -1,63 +1,67
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 Q_PROPERTY(QString rowHeader READ rowHeader WRITE setRowHeader)
32 33 Q_PROPERTY(QVariantList values READ values WRITE setValues)
33 34
34 35 public:
35 36 explicit DeclarativeTableModelElement(QObject *parent = 0);
37 QString rowHeader();
38 void setRowHeader(QString header);
36 39 QVariantList values();
37 40 void setValues(QVariantList values);
38 41 private:
42 QString m_rowHeader;
39 43 QVariantList m_values;
40 44 };
41 45
42 46 class DeclarativeTableModel : public CustomTableModel, public QDeclarativeParserStatus
43 47 {
44 48 Q_OBJECT
45 49 Q_INTERFACES(QDeclarativeParserStatus)
46 50 Q_PROPERTY(QDeclarativeListProperty<QObject> modelChildren READ modelChildren)
47 51 Q_CLASSINFO("DefaultProperty", "modelChildren")
48 52
49 53 public:
50 54 explicit DeclarativeTableModel(QObject *parent = 0);
51 55 QDeclarativeListProperty<QObject> modelChildren();
52 56
53 57 public: // from QDeclarativeParserStatus
54 58 void classBegin();
55 59 void componentComplete();
56 60
57 61 public Q_SLOTS:
58 62 void append(QVariantList slices);
59 63 static void appendModelChild(QDeclarativeListProperty<QObject> *list,
60 64 QObject *element);
61 65 };
62 66
63 67 #endif // DECLARATIVEMODEL_H
@@ -1,155 +1,154
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 width: parent.width
27 height: parent.heigh
26 anchors.fill: parent
28 27
29 28 //![1]
30 29 ChartView {
31 30 id: chartView
32 31 title: "Top-5 car brand shares in Finland"
33 32 anchors.fill: parent
34 33 axisX.max: 10
35 34 axisX.min: 0
36 35 axisY.max: 20
37 36 axisY.min: 0
38 37 animationOptions: ChartView.SeriesAnimations
39 38 axisXLabels: [0, "2007", 1, "2008", 2, "2009", 3, "2010", 4, "2011", 5, "2012"]
40 39 // ...
41 40 //![1]
42 41
43 42 //![2]
44 43 CustomModel {
45 44 id: customModel
46 CustomModelElement { values: [0, "Manufacturer", 0, 1, 2, 3, 4] }
47 CustomModelElement { values: [1, "Volkswagen", 10.3, 12.0, 12.8, 13.0, 13.8] }
48 CustomModelElement { values: [2, "Toyota", 13.8, 13.5, 16.2, 13.7, 10.7] }
49 CustomModelElement { values: [3, "Ford", 6.4, 7.1, 8.9, 8.2, 8.6] }
50 CustomModelElement { values: [4, "Skoda", 4.7, 5.8, 6.9, 8.3, 8.2] }
51 CustomModelElement { values: [5, "Volvo", 7.1, 6.7, 6.5, 6.3, 7.0] }
52 CustomModelElement { values: [6, "Others", 57.7, 54.9, 48.7, 50.5, 51.7] }
45 CustomModelElement { rowHeader: ""; values: [0, "Manufacturer", 0, 1, 2, 3, 4] }
46 CustomModelElement { rowHeader: "Volkswagen"; values: [1, "Volkswagen", 10.3, 12.0, 12.8, 13.0, 13.8] }
47 CustomModelElement { rowHeader: "Toyota"; values: [2, "Toyota", 13.8, 13.5, 16.2, 13.7, 10.7] }
48 CustomModelElement { rowHeader: "Ford"; values: [3, "Ford", 6.4, 7.1, 8.9, 8.2, 8.6] }
49 CustomModelElement { rowHeader: "Skoda"; values: [4, "Skoda", 4.7, 5.8, 6.9, 8.3, 8.2] }
50 CustomModelElement { rowHeader: "Volvo"; values: [5, "Volvo", 7.1, 6.7, 6.5, 6.3, 7.0] }
51 CustomModelElement { rowHeader: "Others"; values: [6, "Others", 57.7, 54.9, 48.7, 50.5, 51.7] }
53 52 }
54 53 //![2]
55 54
56 55 //![5]
57 56 BarSeries {
58 57 name: "Others"
59 58 barMargin: 0
60 59 visible: false
61 60 HBarModelMapper {
62 61 model: customModel
63 62 firstBarSetRow: 6
64 63 lastBarSetRow: 6
65 64 first: 2
66 65 }
67 66 }
68 67 //![5]
69 68
70 69 //![4]
71 70 LineSeries {
72 71 name: "Volkswagen"
73 72 visible: false
74 73 HXYModelMapper {
75 74 model: customModel
76 75 xRow: 0
77 76 yRow: 1
78 77 first: 2
79 78 }
80 79 }
81 80 //![4]
82 81
83 82 LineSeries {
84 83 name: "Toyota"
85 84 visible: false
86 85 HXYModelMapper {
87 86 model: customModel
88 87 xRow: 0
89 88 yRow: 2
90 89 first: 2
91 90 }
92 91 }
93 92
94 93 LineSeries {
95 94 name: "Ford"
96 95 visible: false
97 96 HXYModelMapper {
98 97 model: customModel
99 98 xRow: 0
100 yRow: 2
99 yRow: 3
101 100 first: 2
102 101 }
103 102 }
104 103
105 104 LineSeries {
106 105 name: "Skoda"
107 106 visible: false
108 107 HXYModelMapper {
109 108 model: customModel
110 109 xRow: 0
111 yRow: 3
110 yRow: 4
112 111 first: 2
113 112 }
114 113 }
115 114
116 115 LineSeries {
117 116 name: "Volvo"
118 117 visible: false
119 118 HXYModelMapper {
120 119 model: customModel
121 120 xRow: 0
122 yRow: 4
121 yRow: 5
123 122 first: 2
124 123 }
125 124 }
126 125
127 126 //![3]
128 127 PieSeries {
129 128 id: pieSeries
130 129 size: 0.4
131 130 horizontalPosition: 0.7
132 131 verticalPosition: 0.4
133 132 onClicked: {
134 133 // Show the selection by exploding the slice
135 134 slice.exploded = !slice.exploded;
136 135
137 136 // Update the line series to show the yearly data for this slice
138 137 for (var i = 0; i < chartView.count; i++) {
139 138 if (chartView.series(i).name == slice.label) {
140 139 chartView.series(i).visible = slice.exploded;
141 140 }
142 141 }
143 142 }
144 143 }
145 144 //![3]
146 145
147 146 VPieModelMapper {
148 147 series: pieSeries
149 148 model: customModel
150 149 labelsColumn: 1
151 150 valuesColumn: 2
152 151 first: 1
153 152 }
154 153 }
155 154 }
General Comments 0
You need to be logged in to leave comments. Login now