##// 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
@@ -1,56 +1,56
1 1 /*!
2 2 \example examples/modeldata
3 3 \title Model data example
4 4 \subtitle
5 5
6 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 10 Let's start by creating an instance of CustomTableModel class.
11 11 CustomTableModel class is derived from QAbstractTableModel and it was created for the purpose of this example.
12 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 14 \snippet ../examples/modeldata/tablewidget.cpp 1
15 15
16 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 19 \snippet ../examples/modeldata/tablewidget.cpp 2
20 20
21 21 Now we need QChart instance to display the same data on the chart.
22 22 We also enable animations. It makes it easier to see how modifying the model's data affect the chart.
23 23
24 24 \snippet ../examples/modeldata/tablewidget.cpp 3
25 25
26 26 Then let's create two QLineSeries and tell them to use the data from the model.
27 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 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 29 Finally the series is added to the chart.
30 30
31 31 \snippet ../examples/modeldata/tablewidget.cpp 4
32 32
33 33 To show in QTableView which data coresponds with which series this example uses table coloring.
34 34 When series is added to the chart it is assigned a color beased on the currently selected theme.
35 35 Code below extracts that color from the series and uses it to create colored QTableView.
36 36 Coloring of the view is not a part of the QChart functionality.
37 37
38 38 \snippet ../examples/modeldata/tablewidget.cpp 5
39 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 41
42 42 \snippet ../examples/modeldata/tablewidget.cpp 6
43 43
44 44 \snippet ../examples/modeldata/tablewidget.cpp 7
45 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 47 To make the render look nicer Antialiasing is turned on and the minimum size of the chart is set.
48 48
49 49 \snippet ../examples/modeldata/tablewidget.cpp 8
50 50
51 51 Finally we place both widgets in a layout and use the layout as the application layout.
52 52
53 53 \snippet ../examples/modeldata/tablewidget.cpp 9
54 54
55 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 3 ** Copyright (C) 2012 Digia Plc
4 4 ** All rights reserved.
5 5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 6 **
7 7 ** This file is part of the Qt Commercial Charts Add-on.
8 8 **
9 9 ** $QT_BEGIN_LICENSE$
10 10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 11 ** accordance with the Qt Commercial License Agreement provided with the
12 12 ** Software or, alternatively, in accordance with the terms contained in
13 13 ** a written agreement between you and Digia.
14 14 **
15 15 ** If you have questions regarding the use of this file, please use
16 16 ** contact form at http://qt.digia.com
17 17 ** $QT_END_LICENSE$
18 18 **
19 19 ****************************************************************************/
20 20
21 21 #include "tablewidget.h"
22 22 #include "customtablemodel.h"
23 23 #include <QGridLayout>
24 24 #include <QTableView>
25 25 #include <QChart>
26 26 #include <QChartView>
27 27 #include <QLineSeries>
28 #include <QHeaderView>
28 29
29 30 QTCOMMERCIALCHART_USE_NAMESPACE
30 31
31 32 TableWidget::TableWidget(QWidget *parent)
32 33 : QWidget(parent)
33 34 {
34 35 // create simple model for storing data
35 36 // user's table data model
36 37 //! [1]
37 38 CustomTableModel *model = new CustomTableModel;
38 39 //! [1]
39 40
40 41 //! [2]
41 42 // create table view and add model to it
42 43 QTableView *tableView = new QTableView;
43 44 tableView->setModel(model);
45 tableView->horizontalHeader()->setResizeMode(QHeaderView::Stretch);
46 tableView->verticalHeader()->setResizeMode(QHeaderView::Stretch);
44 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 49 //! [3]
51 50 QChart *chart = new QChart;
52 51 chart->setAnimationOptions(QChart::AllAnimations);
53 52 //! [3]
54 53
55 54 // series 1
56 55 //! [4]
57 56 QLineSeries *series = new QLineSeries;
58 57 series->setModel(model);
59 58 series->setModelMapping(0, 1, Qt::Vertical);
60 59 chart->addSeries(series);
61 60 //! [4]
62 61
63 62 //! [5]
64 63 // for storing color hex from the series
65 64 QString seriesColorHex = "#000000";
66 65
67 66 // get the color of the series and use it for showing the mapped area
68 67 seriesColorHex = "#" + QString::number(series->pen().color().rgb(), 16).right(6).toUpper();
69 68 model->addMapping(seriesColorHex, QRect(0, 0, 2, model->rowCount()));
70 69 //! [5]
71 70
72 71 // series 2
73 72 //! [6]
74 73 series = new QLineSeries;
75 74 series->setModel(model);
76 75 series->setModelMapping(2,3, Qt::Vertical);
77 76 chart->addSeries(series);
78 77 //! [6]
79 78
80 79 //! [7]
81 80 // get the color of the series and use it for showing the mapped area
82 81 seriesColorHex = "#" + QString::number(series->pen().color().rgb(), 16).right(6).toUpper();
83 82 model->addMapping(seriesColorHex, QRect(2, 0, 2, model->rowCount()));
84 83 //! [7]
85 84
86 85 //! [8]
87 86 QChartView *chartView = new QChartView(chart);
88 87 chartView->setRenderHint(QPainter::Antialiasing);
89 88 chartView->setMinimumSize(640, 480);
90 89 //! [8]
91 90
92 91 //! [9]
93 92 // create main layout
94 93 QGridLayout* mainLayout = new QGridLayout;
95 94 mainLayout->addWidget(tableView, 1, 0);
96 95 mainLayout->addWidget(chartView, 1, 1);
97 96 mainLayout->setColumnStretch(1, 1);
98 97 mainLayout->setColumnStretch(0, 0);
99 98 setLayout(mainLayout);
100 99 //! [9]
101 100 }
1 NO CONTENT: file was removed, binary diff hidden
General Comments 0
You need to be logged in to leave comments. Login now