##// 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
@@ -22,6 +22,7
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),
@@ -42,21 +43,34 int CustomTableModel::columnCount(const QModelIndex & parent) const
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) {
@@ -35,7 +35,8 public:
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;
@@ -47,6 +48,7 public:
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 };
@@ -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 42 QVariantList DeclarativeTableModelElement::values()
33 43 {
34 44 return m_values;
@@ -54,7 +64,9 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 }
@@ -29,13 +29,17
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
@@ -23,8 +23,7 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 {
@@ -43,13 +42,13 Rectangle {
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
@@ -97,7 +96,7 Rectangle {
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 }
@@ -108,7 +107,7 Rectangle {
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 }
@@ -119,7 +118,7 Rectangle {
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 }
General Comments 0
You need to be logged in to leave comments. Login now