##// END OF EJS Templates
New simple model example
Marek Rosa -
r889:98e25d8db2c1
parent child
Show More
@@ -0,0 +1,125
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Digia Plc
4 ** All rights reserved.
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 **
7 ** This file is part of the Qt Commercial Charts Add-on.
8 **
9 ** $QT_BEGIN_LICENSE$
10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 ** accordance with the Qt Commercial License Agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.
14 **
15 ** If you have questions regarding the use of this file, please use
16 ** contact form at http://qt.digia.com
17 ** $QT_END_LICENSE$
18 **
19 ****************************************************************************/
20
21 #include "customtablemodel.h"
22 #include <QVector>
23 #include <QTime>
24 #include <QRect>
25 #include <QColor>
26
27 CustomTableModel::CustomTableModel(QObject *parent) :
28 QAbstractTableModel(parent)
29 {
30 qsrand(QTime(0,0,0).secsTo(QTime::currentTime()));
31
32 m_columnCount = 4;
33 m_rowCount = 15;
34
35 // m_data
36 for (int i = 0; i < m_rowCount; i++)
37 {
38 QVector<qreal>* dataVec = new QVector<qreal>(m_columnCount);
39 for (int k = 0; k < dataVec->size(); k++)
40 {
41 if (k%2 == 0)
42 dataVec->replace(k, i * 50 + qrand()%20);
43 else
44 dataVec->replace(k, qrand()%100);
45 }
46 m_data.append(dataVec);
47 }
48 }
49
50 int CustomTableModel::rowCount(const QModelIndex & parent) const
51 {
52 Q_UNUSED(parent)
53 return m_data.count();
54 }
55
56 int CustomTableModel::columnCount(const QModelIndex & parent) const
57 {
58 Q_UNUSED(parent)
59 return m_columnCount;
60 }
61
62 QVariant CustomTableModel::headerData (int section, Qt::Orientation orientation, int role ) const
63 {
64 if (role != Qt::DisplayRole)
65 return QVariant();
66
67 if (orientation == Qt::Horizontal)
68 {
69 if (section%2 == 0)
70 return "x";
71 else
72 return "y";
73 }
74 else
75 return QString("%1").arg(section + 1);
76 }
77
78 QVariant CustomTableModel::data(const QModelIndex & index, int role) const
79 {
80 if (role == Qt::DisplayRole)
81 {
82 return m_data[index.row()]->at(index.column());
83 }
84 else if (role == Qt::EditRole)
85 {
86 return m_data[index.row()]->at(index.column());
87 }
88 else if (role == Qt::BackgroundRole)
89 {
90 QRect rect;
91 foreach(rect, m_mapping)
92 if(rect.contains(index.column(), index.row()))
93 return QColor(m_mapping.key(rect));
94
95 // cell not mapped return white color
96 return QColor(Qt::white);
97 }
98 return QVariant();
99 }
100
101 bool CustomTableModel::setData ( const QModelIndex & index, const QVariant & value, int role)
102 {
103 if (index.isValid() && role == Qt::EditRole)
104 {
105 m_data[index.row()]->replace(index.column(), value.toDouble());
106 emit dataChanged(index, index);
107 return true;
108 }
109 return false;
110 }
111
112 Qt::ItemFlags CustomTableModel::flags ( const QModelIndex & index ) const
113 {
114 return QAbstractItemModel::flags(index) | Qt::ItemIsEditable;
115 }
116
117 void CustomTableModel::addMapping(QString color, QRect area)
118 {
119 m_mapping.insertMulti(color, area);
120 }
121
122 void CustomTableModel::addMapping(QString color, int left, int top, int right, int bottom)
123 {
124 addMapping(color, QRect(QPoint(left, top), QPoint(right, bottom)));
125 }
@@ -0,0 +1,55
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Digia Plc
4 ** All rights reserved.
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 **
7 ** This file is part of the Qt Commercial Charts Add-on.
8 **
9 ** $QT_BEGIN_LICENSE$
10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 ** accordance with the Qt Commercial License Agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.
14 **
15 ** If you have questions regarding the use of this file, please use
16 ** contact form at http://qt.digia.com
17 ** $QT_END_LICENSE$
18 **
19 ****************************************************************************/
20
21 #ifndef XYPOINTSMODEL_H
22 #define XYPOINTSMODEL_H
23
24 #include <QAbstractTableModel>
25 #include <QPointF>
26 #include <QStringList>
27 //#include <QColor>
28 #include <QHash>
29 #include <QRect>
30
31 class CustomTableModel : public QAbstractTableModel
32 {
33 Q_OBJECT
34 public:
35 explicit CustomTableModel(QObject *parent = 0);
36
37 int rowCount ( const QModelIndex & parent = QModelIndex() ) const;
38 int columnCount ( const QModelIndex & parent = QModelIndex() ) const;
39 QVariant headerData (int section, Qt::Orientation orientation, int role = Qt::DisplayRole ) const;
40 QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const;
41 bool setData ( const QModelIndex & index, const QVariant & value, int role = Qt::EditRole );
42 Qt::ItemFlags flags ( const QModelIndex & index ) const;
43
44 void addMapping(QString color, QRect area);
45 void addMapping(QString color, int left, int top, int right, int bottom);
46 void clearMapping() { m_mapping.clear(); }
47
48 private:
49 QList<QVector<qreal> * > m_data;
50 QHash<QString, QRect> m_mapping;
51 int m_columnCount;
52 int m_rowCount;
53 };
54
55 #endif // XYPOINTSMODEL_H
@@ -0,0 +1,31
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Digia Plc
4 ** All rights reserved.
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 **
7 ** This file is part of the Qt Commercial Charts Add-on.
8 **
9 ** $QT_BEGIN_LICENSE$
10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 ** accordance with the Qt Commercial License Agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.
14 **
15 ** If you have questions regarding the use of this file, please use
16 ** contact form at http://qt.digia.com
17 ** $QT_END_LICENSE$
18 **
19 ****************************************************************************/
20
21 #include <QtGui/QApplication>
22 #include "tablewidget.h"
23
24 int main(int argc, char *argv[])
25 {
26 QApplication a(argc, argv);
27 TableWidget w;
28 w.show();
29
30 return a.exec();
31 }
@@ -0,0 +1,22
1 #-------------------------------------------------
2 #
3 # Project created by QtCreator 2012-03-08T14:30:24
4 #
5 #-------------------------------------------------
6
7 !include( ../examples.pri ) {
8 error( "Couldn't find the examples.pri file!" )
9 }
10
11 QT += core gui
12
13 TARGET = modeldata
14 TEMPLATE = app
15
16
17 SOURCES += main.cpp\
18 tablewidget.cpp \
19 customtablemodel.cpp
20
21 HEADERS += tablewidget.h \
22 customtablemodel.h
@@ -0,0 +1,87
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Digia Plc
4 ** All rights reserved.
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 **
7 ** This file is part of the Qt Commercial Charts Add-on.
8 **
9 ** $QT_BEGIN_LICENSE$
10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 ** accordance with the Qt Commercial License Agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.
14 **
15 ** If you have questions regarding the use of this file, please use
16 ** contact form at http://qt.digia.com
17 ** $QT_END_LICENSE$
18 **
19 ****************************************************************************/
20
21 #include "tablewidget.h"
22 #include "customtablemodel.h"
23 #include <QGridLayout>
24 #include <QTableView>
25 #include <QChart>
26 #include <QChartView>
27 #include <QLineSeries>
28
29 QTCOMMERCIALCHART_USE_NAMESPACE
30
31 TableWidget::TableWidget(QWidget *parent)
32 : QWidget(parent)
33 {
34 // create simple model for storing data
35 // user's table data model
36 CustomTableModel *model = new CustomTableModel;
37
38 // create table view and add model to it
39 QTableView *tableView = new QTableView;
40 tableView->setModel(model);
41 tableView->setMinimumWidth(200);
42 // tableView->setMaximumWidth(200);
43 // m_tableView->resizeColumnsToContents();
44 tableView->setColumnWidth(0, 50);
45 tableView->setColumnWidth(1, 50);
46 tableView->setColumnWidth(2, 50);
47 tableView->setColumnWidth(3, 50);
48
49 QChart *m_chart = new QChart;
50 m_chart->setAnimationOptions(QChart::AllAnimations);
51 QChartView *m_chartView = new QChartView(m_chart);
52 m_chartView->setRenderHint(QPainter::Antialiasing);
53 m_chartView->setMinimumSize(640, 480);
54
55 // for storing color hex from the series
56 QString seriesColorHex = "#000000";
57
58 // series 1
59 QLineSeries *m_series = new QLineSeries;
60 m_series->setModel(model);
61 m_series->setModelMapping(0, 1, Qt::Vertical);
62 m_chart->addSeries(m_series);
63
64 // get the color of the series and use it for showing the mapped area
65 seriesColorHex = "#" + QString::number(m_series->pen().color().rgb(), 16).right(6).toUpper();
66 model->addMapping(seriesColorHex, QRect(0, 0, 2, model->rowCount()));
67
68 // series 2
69 m_series = new QLineSeries;
70 m_series->setModel(model);
71 m_series->setModelMapping(2,3, Qt::Vertical);
72 m_chart->addSeries(m_series);
73
74 // get the color of the series and use it for showing the mapped area
75 seriesColorHex = "#" + QString::number(m_series->pen().color().rgb(), 16).right(6).toUpper();
76 model->addMapping(seriesColorHex, QRect(2, 0, 2, model->rowCount()));
77
78 // m_chart->axisX()->setRange(0, 800);
79 // m_chart->axisY()->setRange(0, 120);
80
81 // create main layout
82 QGridLayout* mainLayout = new QGridLayout;
83 mainLayout->addWidget(tableView, 1, 0);
84 mainLayout->addWidget(m_chartView, 1, 1);
85 mainLayout->setColumnStretch(1, 1);
86 setLayout(mainLayout);
87 }
@@ -0,0 +1,34
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Digia Plc
4 ** All rights reserved.
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 **
7 ** This file is part of the Qt Commercial Charts Add-on.
8 **
9 ** $QT_BEGIN_LICENSE$
10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 ** accordance with the Qt Commercial License Agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.
14 **
15 ** If you have questions regarding the use of this file, please use
16 ** contact form at http://qt.digia.com
17 ** $QT_END_LICENSE$
18 **
19 ****************************************************************************/
20
21 #ifndef TABLEWIDGET_H
22 #define TABLEWIDGET_H
23
24 #include <QtGui/QWidget>
25
26 class TableWidget : public QWidget
27 {
28 Q_OBJECT
29
30 public:
31 TableWidget(QWidget *parent = 0);
32 };
33
34 #endif // TABLEWIDGET_H
@@ -15,7 +15,8 SUBDIRS += \
15 stackedbarchart \
15 stackedbarchart \
16 stackedbarchartdrilldown \
16 stackedbarchartdrilldown \
17 tablemodelchart \
17 tablemodelchart \
18 zoomlinechart
18 zoomlinechart \
19 modeldata
19
20
20
21
21
22
General Comments 0
You need to be logged in to leave comments. Login now