##// END OF EJS Templates
Fixed Qml Custom Model demo header data
Tero Ahola -
r1395:33102af7bf20
parent child
Show More
@@ -37,6 +37,7 Rectangle {
37
37
38 PieSeries {
38 PieSeries {
39 id: wheelOfFortune
39 id: wheelOfFortune
40 horizontalPosition: 0.3
40 }
41 }
41
42
42 SplineSeries {
43 SplineSeries {
@@ -48,9 +48,9 QVariant CustomTableModel::headerData(int section, Qt::Orientation orientation,
48 if (role != Qt::DisplayRole)
48 if (role != Qt::DisplayRole)
49 return QVariant();
49 return QVariant();
50
50
51 if (orientation == Qt::Horizontal) {
51 if (orientation == Qt::Vertical) {
52 if (m_rowHeaders.count() > section)
52 if (m_verticalHeaders.count() > section)
53 return m_rowHeaders[section];
53 return m_verticalHeaders[section];
54 else
54 else
55 return QAbstractTableModel::headerData(section, orientation, role);
55 return QAbstractTableModel::headerData(section, orientation, role);
56 } else {
56 } else {
@@ -60,10 +60,10 QVariant CustomTableModel::headerData(int section, Qt::Orientation orientation,
60
60
61 bool CustomTableModel::setHeaderData(int section, Qt::Orientation orientation, const QVariant &value, int role)
61 bool CustomTableModel::setHeaderData(int section, Qt::Orientation orientation, const QVariant &value, int role)
62 {
62 {
63 if (orientation == Qt::Horizontal) {
63 if (orientation == Qt::Vertical) {
64 while (m_rowHeaders.count() <= section)
64 while (m_verticalHeaders.count() <= section)
65 m_rowHeaders.append(QVariant());
65 m_verticalHeaders.append(QVariant());
66 m_rowHeaders.replace(section, value);
66 m_verticalHeaders.replace(section, value);
67 } else {
67 } else {
68 return QAbstractTableModel::setHeaderData(section, orientation, value, role);
68 return QAbstractTableModel::setHeaderData(section, orientation, value, role);
69 }
69 }
@@ -48,7 +48,7 public:
48
48
49 private:
49 private:
50 QList<QVector<QVariant> * > m_data;
50 QList<QVector<QVariant> * > m_data;
51 QList<QVariant> m_rowHeaders;
51 QList<QVariant> m_verticalHeaders;
52 int m_columnCount;
52 int m_columnCount;
53 int m_rowCount;
53 int m_rowCount;
54 };
54 };
@@ -20,6 +20,7
20
20
21 #include "declarativemodel.h"
21 #include "declarativemodel.h"
22 #include <qdeclarativelist.h>
22 #include <qdeclarativelist.h>
23 #include <QStringList>
23 #include <QDebug>
24 #include <QDebug>
24
25
25 ////////////// Table model element ///////////////////
26 ////////////// Table model element ///////////////////
@@ -29,16 +30,6 DeclarativeTableModelElement::DeclarativeTableModelElement(QObject *parent)
29 {
30 {
30 }
31 }
31
32
32 QString DeclarativeTableModelElement::rowHeader()
33 {
34 return m_rowHeader;
35 }
36
37 void DeclarativeTableModelElement::setRowHeader(QString header)
38 {
39 m_rowHeader = header;
40 }
41
42 QVariantList DeclarativeTableModelElement::values()
33 QVariantList DeclarativeTableModelElement::values()
43 {
34 {
44 return m_values;
35 return m_values;
@@ -66,11 +57,21 void DeclarativeTableModel::componentComplete()
66 if (qobject_cast<DeclarativeTableModelElement *>(child)) {
57 if (qobject_cast<DeclarativeTableModelElement *>(child)) {
67 DeclarativeTableModelElement *element = qobject_cast<DeclarativeTableModelElement *>(child);
58 DeclarativeTableModelElement *element = qobject_cast<DeclarativeTableModelElement *>(child);
68 append(element->values());
59 append(element->values());
69 setHeaderData(rowCount() - 1, Qt::Horizontal, element->rowHeader());
70 }
60 }
71 }
61 }
72 }
62 }
73
63
64 void DeclarativeTableModel::setVerticalHeaders(QStringList headers)
65 {
66 for (int i(0); i < headers.count(); i++)
67 setHeaderData(i, Qt::Vertical, headers.at(i));
68 }
69
70 QStringList DeclarativeTableModel::verticalHeaders()
71 {
72 return QStringList();
73 }
74
74 QDeclarativeListProperty<QObject> DeclarativeTableModel::modelChildren()
75 QDeclarativeListProperty<QObject> DeclarativeTableModel::modelChildren()
75 {
76 {
76 return QDeclarativeListProperty<QObject>(this, 0, &DeclarativeTableModel::appendModelChild);
77 return QDeclarativeListProperty<QObject>(this, 0, &DeclarativeTableModel::appendModelChild);
@@ -29,17 +29,13
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)
33 Q_PROPERTY(QVariantList values READ values WRITE setValues)
32 Q_PROPERTY(QVariantList values READ values WRITE setValues)
34
33
35 public:
34 public:
36 explicit DeclarativeTableModelElement(QObject *parent = 0);
35 explicit DeclarativeTableModelElement(QObject *parent = 0);
37 QString rowHeader();
38 void setRowHeader(QString header);
39 QVariantList values();
36 QVariantList values();
40 void setValues(QVariantList values);
37 void setValues(QVariantList values);
41 private:
38 private:
42 QString m_rowHeader;
43 QVariantList m_values;
39 QVariantList m_values;
44 };
40 };
45
41
@@ -48,11 +44,14 class DeclarativeTableModel : public CustomTableModel, public QDeclarativeParser
48 Q_OBJECT
44 Q_OBJECT
49 Q_INTERFACES(QDeclarativeParserStatus)
45 Q_INTERFACES(QDeclarativeParserStatus)
50 Q_PROPERTY(QDeclarativeListProperty<QObject> modelChildren READ modelChildren)
46 Q_PROPERTY(QDeclarativeListProperty<QObject> modelChildren READ modelChildren)
47 Q_PROPERTY(QStringList verticalHeaders READ verticalHeaders WRITE setVerticalHeaders)
51 Q_CLASSINFO("DefaultProperty", "modelChildren")
48 Q_CLASSINFO("DefaultProperty", "modelChildren")
52
49
53 public:
50 public:
54 explicit DeclarativeTableModel(QObject *parent = 0);
51 explicit DeclarativeTableModel(QObject *parent = 0);
55 QDeclarativeListProperty<QObject> modelChildren();
52 QDeclarativeListProperty<QObject> modelChildren();
53 void setVerticalHeaders(QStringList headers);
54 QStringList verticalHeaders();
56
55
57 public: // from QDeclarativeParserStatus
56 public: // from QDeclarativeParserStatus
58 void classBegin();
57 void classBegin();
@@ -42,13 +42,14 Rectangle {
42 //![2]
42 //![2]
43 CustomModel {
43 CustomModel {
44 id: customModel
44 id: customModel
45 CustomModelElement { rowHeader: ""; values: [0, "Manufacturer", 0, 1, 2, 3, 4] }
45 verticalHeaders: ["Manufacturer", "Volkswagen", "Toyota", "Ford", "Skoda", "Volvo", "Others"]
46 CustomModelElement { rowHeader: "Volkswagen"; values: [1, "Volkswagen", 10.3, 12.0, 12.8, 13.0, 13.8] }
46 CustomModelElement { values: [0, "Manufacturer", 0, 1, 2, 3, 4] }
47 CustomModelElement { rowHeader: "Toyota"; values: [2, "Toyota", 13.8, 13.5, 16.2, 13.7, 10.7] }
47 CustomModelElement { values: [1, "Volkswagen", 10.3, 12.0, 12.8, 13.0, 13.8] }
48 CustomModelElement { rowHeader: "Ford"; values: [3, "Ford", 6.4, 7.1, 8.9, 8.2, 8.6] }
48 CustomModelElement { values: [2, "Toyota", 13.8, 13.5, 16.2, 13.7, 10.7] }
49 CustomModelElement { rowHeader: "Skoda"; values: [4, "Skoda", 4.7, 5.8, 6.9, 8.3, 8.2] }
49 CustomModelElement { values: [3, "Ford", 6.4, 7.1, 8.9, 8.2, 8.6] }
50 CustomModelElement { rowHeader: "Volvo"; values: [5, "Volvo", 7.1, 6.7, 6.5, 6.3, 7.0] }
50 CustomModelElement { values: [4, "Skoda", 4.7, 5.8, 6.9, 8.3, 8.2] }
51 CustomModelElement { rowHeader: "Others"; values: [6, "Others", 57.7, 54.9, 48.7, 50.5, 51.7] }
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] }
52 }
53 }
53 //![2]
54 //![2]
54
55
1 NO CONTENT: modified file, binary diff hidden
NO CONTENT: modified file, binary diff hidden
General Comments 0
You need to be logged in to leave comments. Login now