##// END OF EJS Templates
ModelData example description updated
Marek Rosa -
r1358:761e6f4113b7
parent child
Show More
@@ -1,56 +1,57
1 /*!
1 /*!
2 \example examples/modeldata
2 \example examples/modeldata
3 \title Model data example
3 \title Model data example
4 \subtitle
4 \subtitle
5
5
6 This example shows how to use QAbstractItemModel derived model as the data for the series.
6 This example shows how to use QAbstractItemModel derived model as the data for the series.
7
7
8 \image examples_modeldata.png
8 \image examples_modeldata.png
9
9
10 Let's start by creating an instance of CustomTableModel class.
10 Let's start by creating an instance of CustomTableModel class.
11 CustomTableModel class is derived from QAbstractTableModel and it was created for the purpose of this example.
11 CustomTableModel class is derived from QAbstractTableModel and it was created for the purpose of this example.
12 The constructor of this class populates the internal data store of the model with the data that is good for our chart example.
12 The constructor of this class populates the internal data store of the model with the data that is good for our chart example.
13
13
14 \snippet ../examples/modeldata/tablewidget.cpp 1
14 \snippet ../examples/modeldata/tablewidget.cpp 1
15
15
16 We now have a model with data that we would like to display both on the chart and in a QTableView.
16 We now have a model with data that we would like to display both on the chart and in a QTableView.
17 First, we create QTableView and tell it use the model as a data source. To make the data cells fill the table view we also change headers resize mode.
17 First, we create QTableView and tell it use the model as a data source. To make the data cells fill the table view we also change headers resize mode.
18
18
19 \snippet ../examples/modeldata/tablewidget.cpp 2
19 \snippet ../examples/modeldata/tablewidget.cpp 2
20
20
21 Now we need QChart instance to display the same data on the chart.
21 Now we need QChart instance to display the same data on the chart.
22 We also enable animations. It makes it easier to see how modifying the model's data affect the chart.
22 We also enable animations. It makes it easier to see how modifying the model's data affect the chart.
23
23
24 \snippet ../examples/modeldata/tablewidget.cpp 3
24 \snippet ../examples/modeldata/tablewidget.cpp 3
25
25
26 Then let's create two QLineSeries and tell them to use the data from the model.
26 First line of the code below creates new line series. Line number two creates an instance of QVXYModelMapper class.
27 First line of the code below creates new line series. Line number two sets the model as the data source for the series.
27 Next two lines specifie that x coordinates are taken from the model's column(Qt::Vertical) with index 0 and the y coordinates are taken from the model's column with index 1.
28 Third line specifies that x coordinates are taken from the model's column(Qt::Vertical) with index 0 and the y coordinates are taken from the model's column with index 1.
28 To create a connection between the series and the model we set both of those objects to QVXYModelMapper.
29
29 Finally the series is added to the chart.
30 Finally the series is added to the chart.
30
31
31 \snippet ../examples/modeldata/tablewidget.cpp 4
32 \snippet ../examples/modeldata/tablewidget.cpp 4
32
33
33 To show in QTableView which data coresponds with which series this example uses table coloring.
34 To show in QTableView which data coresponds with which series this example uses table coloring.
34 When series is added to the chart it is assigned a color beased on the currently selected theme.
35 When series is added to the chart it is assigned a color beased on the currently selected theme.
35 Code below extracts that color from the series and uses it to create colored QTableView.
36 Code below extracts that color from the series and uses it to create colored QTableView.
36 Coloring of the view is not a part of the QChart functionality.
37 Coloring of the view is not a part of the QChart functionality.
37
38
38 \snippet ../examples/modeldata/tablewidget.cpp 5
39 \snippet ../examples/modeldata/tablewidget.cpp 5
39
40
40 The same operations are done with second series. Notice that for this series different columns of the same model are mapped.
41 The same operations are done with second series. Notice that for this series different columns of the same model are mapped.
41
42
42 \snippet ../examples/modeldata/tablewidget.cpp 6
43 \snippet ../examples/modeldata/tablewidget.cpp 6
43
44
44 \snippet ../examples/modeldata/tablewidget.cpp 7
45 \snippet ../examples/modeldata/tablewidget.cpp 7
45
46
46 To avoid setting up the QGraphicsScene we use QChartView class that does it for us. QChart object pointer is used as a parameter of the QChartView constructor.
47 To avoid setting up the QGraphicsScene we use QChartView class that does it for us. QChart object pointer is used as a parameter of the QChartView constructor.
47 To make the render look nicer Antialiasing is turned on and the minimum size of the chart is set.
48 To make the render look nicer Antialiasing is turned on and the minimum size of the chartView widget is set.
48
49
49 \snippet ../examples/modeldata/tablewidget.cpp 8
50 \snippet ../examples/modeldata/tablewidget.cpp 8
50
51
51 Finally we place both widgets in a layout and use the layout as the application layout.
52 Finally we place both widgets in a layout and use the layout as the application layout.
52
53
53 \snippet ../examples/modeldata/tablewidget.cpp 9
54 \snippet ../examples/modeldata/tablewidget.cpp 9
54
55
55 Application is ready. Try modifying the data in the table view and see how it affects the chart.
56 Application is ready. Try modifying the data in the table view and see how it affects the chart.
56 */
57 */
@@ -1,109 +1,109
1 /****************************************************************************
1 /****************************************************************************
2 **
2 **
3 ** Copyright (C) 2012 Digia Plc
3 ** Copyright (C) 2012 Digia Plc
4 ** All rights reserved.
4 ** All rights reserved.
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 **
6 **
7 ** This file is part of the Qt Commercial Charts Add-on.
7 ** This file is part of the Qt Commercial Charts Add-on.
8 **
8 **
9 ** $QT_BEGIN_LICENSE$
9 ** $QT_BEGIN_LICENSE$
10 ** Licensees holding valid Qt Commercial licenses may use this file in
10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 ** accordance with the Qt Commercial License Agreement provided with the
11 ** accordance with the Qt Commercial License Agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.
13 ** a written agreement between you and Digia.
14 **
14 **
15 ** If you have questions regarding the use of this file, please use
15 ** If you have questions regarding the use of this file, please use
16 ** contact form at http://qt.digia.com
16 ** contact form at http://qt.digia.com
17 ** $QT_END_LICENSE$
17 ** $QT_END_LICENSE$
18 **
18 **
19 ****************************************************************************/
19 ****************************************************************************/
20
20
21 #include "tablewidget.h"
21 #include "tablewidget.h"
22 #include "customtablemodel.h"
22 #include "customtablemodel.h"
23 #include <QGridLayout>
23 #include <QGridLayout>
24 #include <QTableView>
24 #include <QTableView>
25 #include <QChart>
25 #include <QChart>
26 #include <QChartView>
26 #include <QChartView>
27 #include <QLineSeries>
27 #include <QLineSeries>
28 #include <QVXYModelMapper>
28 #include <QVXYModelMapper>
29 #include <QHeaderView>
29 #include <QHeaderView>
30
30
31 QTCOMMERCIALCHART_USE_NAMESPACE
31 QTCOMMERCIALCHART_USE_NAMESPACE
32
32
33 TableWidget::TableWidget(QWidget *parent)
33 TableWidget::TableWidget(QWidget *parent)
34 : QWidget(parent)
34 : QWidget(parent)
35 {
35 {
36 // create simple model for storing data
36 // create simple model for storing data
37 // user's table data model
37 // user's table data model
38 //! [1]
38 //! [1]
39 CustomTableModel *model = new CustomTableModel;
39 CustomTableModel *model = new CustomTableModel;
40 //! [1]
40 //! [1]
41
41
42 //! [2]
42 //! [2]
43 // create table view and add model to it
43 // create table view and add model to it
44 QTableView *tableView = new QTableView;
44 QTableView *tableView = new QTableView;
45 tableView->setModel(model);
45 tableView->setModel(model);
46 tableView->horizontalHeader()->setResizeMode(QHeaderView::Stretch);
46 tableView->horizontalHeader()->setResizeMode(QHeaderView::Stretch);
47 tableView->verticalHeader()->setResizeMode(QHeaderView::Stretch);
47 tableView->verticalHeader()->setResizeMode(QHeaderView::Stretch);
48 //! [2]
48 //! [2]
49
49
50 //! [3]
50 //! [3]
51 QChart *chart = new QChart;
51 QChart *chart = new QChart;
52 chart->setAnimationOptions(QChart::AllAnimations);
52 chart->setAnimationOptions(QChart::AllAnimations);
53 //! [3]
53 //! [3]
54
54
55 // series 1
55 // series 1
56 //! [4]
56 //! [4]
57 QLineSeries *series = new QLineSeries;
57 QLineSeries *series = new QLineSeries;
58 QVXYModelMapper *mapper = new QVXYModelMapper(this);
58 QVXYModelMapper *mapper = new QVXYModelMapper(this);
59 mapper->setXColumn(0);
59 mapper->setXColumn(0);
60 mapper->setYColumn(1);
60 mapper->setYColumn(1);
61 mapper->setSeries(series);
61 mapper->setSeries(series);
62 mapper->setModel(model);
62 mapper->setModel(model);
63 chart->addSeries(series);
63 chart->addSeries(series);
64 //! [4]
64 //! [4]
65
65
66 //! [5]
66 //! [5]
67 // for storing color hex from the series
67 // for storing color hex from the series
68 QString seriesColorHex = "#000000";
68 QString seriesColorHex = "#000000";
69
69
70 // get the color of the series and use it for showing the mapped area
70 // get the color of the series and use it for showing the mapped area
71 seriesColorHex = "#" + QString::number(series->pen().color().rgb(), 16).right(6).toUpper();
71 seriesColorHex = "#" + QString::number(series->pen().color().rgb(), 16).right(6).toUpper();
72 model->addMapping(seriesColorHex, QRect(0, 0, 2, model->rowCount()));
72 model->addMapping(seriesColorHex, QRect(0, 0, 2, model->rowCount()));
73 //! [5]
73 //! [5]
74
74
75
75
76 // series 2
76 // series 2
77 //! [6]
77 //! [6]
78 series = new QLineSeries;
78 series = new QLineSeries;
79
79
80 mapper = new QVXYModelMapper(this);
80 mapper = new QVXYModelMapper(this);
81 mapper->setXColumn(2);
81 mapper->setXColumn(2);
82 mapper->setYColumn(3);
82 mapper->setYColumn(3);
83 mapper->setSeries(series);
83 mapper->setSeries(series);
84 mapper->setModel(model);
84 mapper->setModel(model);
85 chart->addSeries(series);
85 chart->addSeries(series);
86 //! [6]
86 //! [6]
87
87
88 //! [7]
88 //! [7]
89 // get the color of the series and use it for showing the mapped area
89 // get the color of the series and use it for showing the mapped area
90 seriesColorHex = "#" + QString::number(series->pen().color().rgb(), 16).right(6).toUpper();
90 seriesColorHex = "#" + QString::number(series->pen().color().rgb(), 16).right(6).toUpper();
91 model->addMapping(seriesColorHex, QRect(2, 0, 2, model->rowCount()));
91 model->addMapping(seriesColorHex, QRect(2, 0, 2, model->rowCount()));
92 //! [7]
92 //! [7]
93
93
94 //! [8]
94 //! [8]
95 QChartView *chartView = new QChartView(chart);
95 QChartView *chartView = new QChartView(chart);
96 chartView->setRenderHint(QPainter::Antialiasing);
96 chartView->setRenderHint(QPainter::Antialiasing);
97 chartView->setMinimumSize(640, 480);
97 // chartView->setMinimumSize(640, 480);
98 //! [8]
98 //! [8]
99
99
100 //! [9]
100 //! [9]
101 // create main layout
101 // create main layout
102 QGridLayout* mainLayout = new QGridLayout;
102 QGridLayout* mainLayout = new QGridLayout;
103 mainLayout->addWidget(tableView, 1, 0);
103 mainLayout->addWidget(tableView, 1, 0);
104 mainLayout->addWidget(chartView, 1, 1);
104 mainLayout->addWidget(chartView, 1, 1);
105 mainLayout->setColumnStretch(1, 1);
105 mainLayout->setColumnStretch(1, 1);
106 mainLayout->setColumnStretch(0, 0);
106 mainLayout->setColumnStretch(0, 0);
107 setLayout(mainLayout);
107 setLayout(mainLayout);
108 //! [9]
108 //! [9]
109 }
109 }
General Comments 0
You need to be logged in to leave comments. Login now