##// 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
NO CONTENT: new file 100644, binary diff hidden
@@ -22,6 +22,7
22 #include <QVector>
22 #include <QVector>
23 #include <QRect>
23 #include <QRect>
24 #include <QColor>
24 #include <QColor>
25 #include <QDebug>
25
26
26 CustomTableModel::CustomTableModel(QObject *parent) :
27 CustomTableModel::CustomTableModel(QObject *parent) :
27 QAbstractTableModel(parent),
28 QAbstractTableModel(parent),
@@ -42,21 +43,34 int CustomTableModel::columnCount(const QModelIndex & parent) const
42 return m_columnCount;
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 if (role != Qt::DisplayRole)
48 if (role != Qt::DisplayRole)
48 return QVariant();
49 return QVariant();
49
50
50 if (orientation == Qt::Horizontal) {
51 if (orientation == Qt::Horizontal) {
51 if (section % 2 == 0)
52 if (m_rowHeaders.count() > section)
52 return "x";
53 return m_rowHeaders[section];
53 else
54 else
54 return "y";
55 return QAbstractTableModel::headerData(section, orientation, role);
55 } else {
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 QVariant CustomTableModel::data(const QModelIndex &index, int role) const
74 QVariant CustomTableModel::data(const QModelIndex &index, int role) const
61 {
75 {
62 if (role == Qt::DisplayRole) {
76 if (role == Qt::DisplayRole) {
@@ -35,7 +35,8 public:
35
35
36 int rowCount ( const QModelIndex & parent = QModelIndex() ) const;
36 int rowCount ( const QModelIndex & parent = QModelIndex() ) const;
37 int columnCount ( const QModelIndex & parent = QModelIndex() ) const;
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 QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const;
40 QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const;
40 bool setData ( const QModelIndex & index, const QVariant & value, int role = Qt::EditRole );
41 bool setData ( const QModelIndex & index, const QVariant & value, int role = Qt::EditRole );
41 Qt::ItemFlags flags ( const QModelIndex & index ) const;
42 Qt::ItemFlags flags ( const QModelIndex & index ) const;
@@ -47,6 +48,7 public:
47
48
48 private:
49 private:
49 QList<QVector<QVariant> * > m_data;
50 QList<QVector<QVariant> * > m_data;
51 QList<QVariant> m_rowHeaders;
50 int m_columnCount;
52 int m_columnCount;
51 int m_rowCount;
53 int m_rowCount;
52 };
54 };
@@ -29,6 +29,16 DeclarativeTableModelElement::DeclarativeTableModelElement(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 QVariantList DeclarativeTableModelElement::values()
42 QVariantList DeclarativeTableModelElement::values()
33 {
43 {
34 return m_values;
44 return m_values;
@@ -54,7 +64,9 void DeclarativeTableModel::componentComplete()
54 {
64 {
55 foreach (QObject *child, children()) {
65 foreach (QObject *child, children()) {
56 if (qobject_cast<DeclarativeTableModelElement *>(child)) {
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 }
@@ -29,13 +29,17
29 class DeclarativeTableModelElement : public QObject
29 class DeclarativeTableModelElement : public QObject
30 {
30 {
31 Q_OBJECT
31 Q_OBJECT
32 Q_PROPERTY(QString rowHeader READ rowHeader WRITE setRowHeader)
32 Q_PROPERTY(QVariantList values READ values WRITE setValues)
33 Q_PROPERTY(QVariantList values READ values WRITE setValues)
33
34
34 public:
35 public:
35 explicit DeclarativeTableModelElement(QObject *parent = 0);
36 explicit DeclarativeTableModelElement(QObject *parent = 0);
37 QString rowHeader();
38 void setRowHeader(QString header);
36 QVariantList values();
39 QVariantList values();
37 void setValues(QVariantList values);
40 void setValues(QVariantList values);
38 private:
41 private:
42 QString m_rowHeader;
39 QVariantList m_values;
43 QVariantList m_values;
40 };
44 };
41
45
@@ -23,8 +23,7 import QtCommercial.Chart 1.0
23 import QmlCustomModel 1.0
23 import QmlCustomModel 1.0
24
24
25 Rectangle {
25 Rectangle {
26 width: parent.width
26 anchors.fill: parent
27 height: parent.heigh
28
27
29 //![1]
28 //![1]
30 ChartView {
29 ChartView {
@@ -43,13 +42,13 Rectangle {
43 //![2]
42 //![2]
44 CustomModel {
43 CustomModel {
45 id: customModel
44 id: customModel
46 CustomModelElement { values: [0, "Manufacturer", 0, 1, 2, 3, 4] }
45 CustomModelElement { rowHeader: ""; values: [0, "Manufacturer", 0, 1, 2, 3, 4] }
47 CustomModelElement { values: [1, "Volkswagen", 10.3, 12.0, 12.8, 13.0, 13.8] }
46 CustomModelElement { rowHeader: "Volkswagen"; 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] }
47 CustomModelElement { rowHeader: "Toyota"; 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] }
48 CustomModelElement { rowHeader: "Ford"; 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] }
49 CustomModelElement { rowHeader: "Skoda"; 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] }
50 CustomModelElement { rowHeader: "Volvo"; 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] }
51 CustomModelElement { rowHeader: "Others"; values: [6, "Others", 57.7, 54.9, 48.7, 50.5, 51.7] }
53 }
52 }
54 //![2]
53 //![2]
55
54
@@ -97,7 +96,7 Rectangle {
97 HXYModelMapper {
96 HXYModelMapper {
98 model: customModel
97 model: customModel
99 xRow: 0
98 xRow: 0
100 yRow: 2
99 yRow: 3
101 first: 2
100 first: 2
102 }
101 }
103 }
102 }
@@ -108,7 +107,7 Rectangle {
108 HXYModelMapper {
107 HXYModelMapper {
109 model: customModel
108 model: customModel
110 xRow: 0
109 xRow: 0
111 yRow: 3
110 yRow: 4
112 first: 2
111 first: 2
113 }
112 }
114 }
113 }
@@ -119,7 +118,7 Rectangle {
119 HXYModelMapper {
118 HXYModelMapper {
120 model: customModel
119 model: customModel
121 xRow: 0
120 xRow: 0
122 yRow: 4
121 yRow: 5
123 first: 2
122 first: 2
124 }
123 }
125 }
124 }
General Comments 0
You need to be logged in to leave comments. Login now