##// END OF EJS Templates
Improved modelData example visual appearance
Marek Rosa -
r1030:5594c69cffa0
parent child
Show More
1 NO CONTENT: new file 100644, binary diff hidden
NO CONTENT: new file 100644, binary diff hidden
@@ -1,56 +1,56
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 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.
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 Then let's create two QLineSeries and tell them to use the data from the model.
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 First line of the code below creates new line series. Line number two sets the model as the data source for the series.
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 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.
29 Finally the series is added to the chart.
29 Finally the series is added to the chart.
30
30
31 \snippet ../examples/modeldata/tablewidget.cpp 4
31 \snippet ../examples/modeldata/tablewidget.cpp 4
32
32
33 To show in QTableView which data coresponds with which series this example uses table coloring.
33 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.
34 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.
35 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.
36 Coloring of the view is not a part of the QChart functionality.
37
37
38 \snippet ../examples/modeldata/tablewidget.cpp 5
38 \snippet ../examples/modeldata/tablewidget.cpp 5
39
39
40 The same operations are done with second series. Notice that for this series different columns of the same model are mapped.
40 The same operations are done with second series. Notice that for this series different columns of the same model are mapped.
41
41
42 \snippet ../examples/modeldata/tablewidget.cpp 6
42 \snippet ../examples/modeldata/tablewidget.cpp 6
43
43
44 \snippet ../examples/modeldata/tablewidget.cpp 7
44 \snippet ../examples/modeldata/tablewidget.cpp 7
45
45
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.
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 make the render look nicer Antialiasing is turned on and the minimum size of the chart is set.
47 To make the render look nicer Antialiasing is turned on and the minimum size of the chart is set.
48
48
49 \snippet ../examples/modeldata/tablewidget.cpp 8
49 \snippet ../examples/modeldata/tablewidget.cpp 8
50
50
51 Finally we place both widgets in a layout and use the layout as the application layout.
51 Finally we place both widgets in a layout and use the layout as the application layout.
52
52
53 \snippet ../examples/modeldata/tablewidget.cpp 9
53 \snippet ../examples/modeldata/tablewidget.cpp 9
54
54
55 Application is ready. Try modifying the data in the table view and see how it affects the chart.
55 Application is ready. Try modifying the data in the table view and see how it affects the chart.
56 */
56 */
@@ -1,101 +1,100
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 <QHeaderView>
28
29
29 QTCOMMERCIALCHART_USE_NAMESPACE
30 QTCOMMERCIALCHART_USE_NAMESPACE
30
31
31 TableWidget::TableWidget(QWidget *parent)
32 TableWidget::TableWidget(QWidget *parent)
32 : QWidget(parent)
33 : QWidget(parent)
33 {
34 {
34 // create simple model for storing data
35 // create simple model for storing data
35 // user's table data model
36 // user's table data model
36 //! [1]
37 //! [1]
37 CustomTableModel *model = new CustomTableModel;
38 CustomTableModel *model = new CustomTableModel;
38 //! [1]
39 //! [1]
39
40
40 //! [2]
41 //! [2]
41 // create table view and add model to it
42 // create table view and add model to it
42 QTableView *tableView = new QTableView;
43 QTableView *tableView = new QTableView;
43 tableView->setModel(model);
44 tableView->setModel(model);
45 tableView->horizontalHeader()->setResizeMode(QHeaderView::Stretch);
46 tableView->verticalHeader()->setResizeMode(QHeaderView::Stretch);
44 //! [2]
47 //! [2]
45 tableView->setColumnWidth(0, 56);
46 tableView->setColumnWidth(1, 56);
47 tableView->setColumnWidth(2, 56);
48 tableView->setColumnWidth(3, 56);
49
48
50 //! [3]
49 //! [3]
51 QChart *chart = new QChart;
50 QChart *chart = new QChart;
52 chart->setAnimationOptions(QChart::AllAnimations);
51 chart->setAnimationOptions(QChart::AllAnimations);
53 //! [3]
52 //! [3]
54
53
55 // series 1
54 // series 1
56 //! [4]
55 //! [4]
57 QLineSeries *series = new QLineSeries;
56 QLineSeries *series = new QLineSeries;
58 series->setModel(model);
57 series->setModel(model);
59 series->setModelMapping(0, 1, Qt::Vertical);
58 series->setModelMapping(0, 1, Qt::Vertical);
60 chart->addSeries(series);
59 chart->addSeries(series);
61 //! [4]
60 //! [4]
62
61
63 //! [5]
62 //! [5]
64 // for storing color hex from the series
63 // for storing color hex from the series
65 QString seriesColorHex = "#000000";
64 QString seriesColorHex = "#000000";
66
65
67 // get the color of the series and use it for showing the mapped area
66 // get the color of the series and use it for showing the mapped area
68 seriesColorHex = "#" + QString::number(series->pen().color().rgb(), 16).right(6).toUpper();
67 seriesColorHex = "#" + QString::number(series->pen().color().rgb(), 16).right(6).toUpper();
69 model->addMapping(seriesColorHex, QRect(0, 0, 2, model->rowCount()));
68 model->addMapping(seriesColorHex, QRect(0, 0, 2, model->rowCount()));
70 //! [5]
69 //! [5]
71
70
72 // series 2
71 // series 2
73 //! [6]
72 //! [6]
74 series = new QLineSeries;
73 series = new QLineSeries;
75 series->setModel(model);
74 series->setModel(model);
76 series->setModelMapping(2,3, Qt::Vertical);
75 series->setModelMapping(2,3, Qt::Vertical);
77 chart->addSeries(series);
76 chart->addSeries(series);
78 //! [6]
77 //! [6]
79
78
80 //! [7]
79 //! [7]
81 // get the color of the series and use it for showing the mapped area
80 // get the color of the series and use it for showing the mapped area
82 seriesColorHex = "#" + QString::number(series->pen().color().rgb(), 16).right(6).toUpper();
81 seriesColorHex = "#" + QString::number(series->pen().color().rgb(), 16).right(6).toUpper();
83 model->addMapping(seriesColorHex, QRect(2, 0, 2, model->rowCount()));
82 model->addMapping(seriesColorHex, QRect(2, 0, 2, model->rowCount()));
84 //! [7]
83 //! [7]
85
84
86 //! [8]
85 //! [8]
87 QChartView *chartView = new QChartView(chart);
86 QChartView *chartView = new QChartView(chart);
88 chartView->setRenderHint(QPainter::Antialiasing);
87 chartView->setRenderHint(QPainter::Antialiasing);
89 chartView->setMinimumSize(640, 480);
88 chartView->setMinimumSize(640, 480);
90 //! [8]
89 //! [8]
91
90
92 //! [9]
91 //! [9]
93 // create main layout
92 // create main layout
94 QGridLayout* mainLayout = new QGridLayout;
93 QGridLayout* mainLayout = new QGridLayout;
95 mainLayout->addWidget(tableView, 1, 0);
94 mainLayout->addWidget(tableView, 1, 0);
96 mainLayout->addWidget(chartView, 1, 1);
95 mainLayout->addWidget(chartView, 1, 1);
97 mainLayout->setColumnStretch(1, 1);
96 mainLayout->setColumnStretch(1, 1);
98 mainLayout->setColumnStretch(0, 0);
97 mainLayout->setColumnStretch(0, 0);
99 setLayout(mainLayout);
98 setLayout(mainLayout);
100 //! [9]
99 //! [9]
101 }
100 }
1 NO CONTENT: file was removed, binary diff hidden
NO CONTENT: file was removed, binary diff hidden
General Comments 0
You need to be logged in to leave comments. Login now