##// END OF EJS Templates
BarModelMapper example updated
Marek Rosa -
r1393:be0ddde2e42e
parent child
Show More
1 NO CONTENT: new file 100644, binary diff hidden
NO CONTENT: new file 100644, binary diff hidden
@@ -1,55 +1,57
1 /*!
1 /*!
2 \example examples/barmodelmapper
2 \example examples/barmodelmapper
3 \title BarModelMapper example
3 \title BarModelMapper example
4 \subtitle
4 \subtitle
5
5
6 This example shows how to use QAbstractItemModel derived model as the data for the bar series.
6 This example shows how to use QAbstractItemModel derived model as the data for the bar series.
7
7
8 \image examples_barmodelmapper.png
9
8 Let's start by creating an instance of CustomTableModel class.
10 Let's start by creating an instance of CustomTableModel class.
9 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.
10 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.
11
13
12 \snippet ../examples/barmodelmapper/tablewidget.cpp 1
14 \snippet ../examples/barmodelmapper/tablewidget.cpp 1
13
15
14 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.
15 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 have the data presented nicely the minimum width of the table view is set and its headers resize mode changed to stretch.
16
18
17 \snippet ../examples/barmodelmapper/tablewidget.cpp 2
19 \snippet ../examples/barmodelmapper/tablewidget.cpp 2
18
20
19 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.
20 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.
21
23
22 \snippet ../examples/barmodelmapper/tablewidget.cpp 3
24 \snippet ../examples/barmodelmapper/tablewidget.cpp 3
23
25
24 First line of the code below creates new grouped bar series. Variables firstRow and rowCount are used to define a custom model mapping.
26 First line of the code below creates new grouped bar series. Variables firstRow and rowCount are used to define a custom model mapping.
25 Custom mapping allows to take only part of the data from the model. In this case data from 5 rows starting with the row with the index 3.
27 Custom mapping allows to take only part of the data from the model. In this case data from 5 rows starting with the row with the index 3.
26 Following three lines create an instance of QVBarModelMapper class and specifie that data for the bar sets should be taken from the model's columns(Qt::Vertical) with indexes from 1 to 4(inclusive).
28 Following three lines create an instance of QVBarModelMapper class and specifie that data for the bar sets should be taken from the model's columns(Qt::Vertical) with indexes from 1 to 4(inclusive).
27 To create a connection between the series and the model we set both of those objects to QVBarModelMapper.
29 To create a connection between the series and the model we set both of those objects to QVBarModelMapper.
28
30
29 Finally the series is added to the chart.
31 Finally the series is added to the chart.
30
32
31 \snippet ../examples/barmodelmapper/tablewidget.cpp 4
33 \snippet ../examples/barmodelmapper/tablewidget.cpp 4
32
34
33 To show in QTableView which data coresponds with which series this example uses table coloring.
35 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.
36 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.
37 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.
38 Coloring of the view is not a part of the QChart functionality.
37
39
38 \snippet ../examples/barmodelmapper/tablewidget.cpp 5
40 \snippet ../examples/barmodelmapper/tablewidget.cpp 5
39
41
40 We would like to have categories placed on the chart's axis that describe what the data means.
42 We would like to have categories placed on the chart's axis that describe what the data means.
41 Next snippet shows how to do that.
43 Next snippet shows how to do that.
42
44
43 \snippet ../examples/barmodelmapper/tablewidget.cpp 6
45 \snippet ../examples/barmodelmapper/tablewidget.cpp 6
44
46
45 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.
46 To make the render look nicer Antialiasing is turned on and the minimum size of the chartView widget is set.
48 To make the render look nicer Antialiasing is turned on and the minimum size of the chartView widget is set.
47
49
48 \snippet ../examples/barmodelmapper/tablewidget.cpp 7
50 \snippet ../examples/barmodelmapper/tablewidget.cpp 7
49
51
50 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.
51
53
52 \snippet ../examples/barmodelmapper/tablewidget.cpp 8
54 \snippet ../examples/barmodelmapper/tablewidget.cpp 8
53
55
54 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.
55 */
57 */
@@ -1,108 +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 <QGroupedBarSeries>
29 #include <QGroupedBarSeries>
30 #include <QBarSet>
30 #include <QBarSet>
31 #include <QVBarModelMapper>
31 #include <QVBarModelMapper>
32 #include <QHeaderView>
32 #include <QHeaderView>
33
33
34 QTCOMMERCIALCHART_USE_NAMESPACE
34 QTCOMMERCIALCHART_USE_NAMESPACE
35
35
36 TableWidget::TableWidget(QWidget *parent)
36 TableWidget::TableWidget(QWidget *parent)
37 : QWidget(parent)
37 : QWidget(parent)
38 {
38 {
39 // create simple model for storing data
39 // create simple model for storing data
40 // user's table data model
40 // user's table data model
41 //! [1]
41 //! [1]
42 CustomTableModel *model = new CustomTableModel;
42 CustomTableModel *model = new CustomTableModel;
43 //! [1]
43 //! [1]
44
44
45 //! [2]
45 //! [2]
46 // create table view and add model to it
46 // create table view and add model to it
47 QTableView *tableView = new QTableView;
47 QTableView *tableView = new QTableView;
48 tableView->setModel(model);
48 tableView->setModel(model);
49 tableView->horizontalHeader()->setResizeMode(QHeaderView::Stretch);
49 tableView->setMinimumWidth(300);
50 tableView->horizontalHeader()->setResizeMode(QHeaderView::Stretch);
50 tableView->verticalHeader()->setResizeMode(QHeaderView::Stretch);
51 tableView->verticalHeader()->setResizeMode(QHeaderView::Stretch);
51 //! [2]
52 //! [2]
52
53
53 //! [3]
54 //! [3]
54 QChart *chart = new QChart;
55 QChart *chart = new QChart;
55 chart->setAnimationOptions(QChart::AllAnimations);
56 chart->setAnimationOptions(QChart::AllAnimations);
56 //! [3]
57 //! [3]
57
58
58 // series 1
59 // series 1
59 //! [4]
60 //! [4]
60 QGroupedBarSeries *series = new QGroupedBarSeries;
61 QGroupedBarSeries *series = new QGroupedBarSeries;
61
62
62 int first = 3;
63 int first = 3;
63 int count = 5;
64 int count = 5;
64 QVBarModelMapper *mapper = new QVBarModelMapper(this);
65 QVBarModelMapper *mapper = new QVBarModelMapper(this);
65 mapper->setFirstBarSetColumn(1);
66 mapper->setFirstBarSetColumn(1);
66 mapper->setLastBarSetColumn(4);
67 mapper->setLastBarSetColumn(4);
67 mapper->setFirst(3);
68 mapper->setFirst(3);
68 mapper->setCount(count);
69 mapper->setCount(count);
69 mapper->setSeries(series);
70 mapper->setSeries(series);
70 mapper->setModel(model);
71 mapper->setModel(model);
71 chart->addSeries(series);
72 chart->addSeries(series);
72 //! [4]
73 //! [4]
73
74
74 //! [5]
75 //! [5]
75 // for storing color hex from the series
76 // for storing color hex from the series
76 QString seriesColorHex = "#000000";
77 QString seriesColorHex = "#000000";
77
78
78 // get the color of the series and use it for showing the mapped area
79 // get the color of the series and use it for showing the mapped area
79 QList<QBarSet*> barsets = series->barSets();
80 QList<QBarSet*> barsets = series->barSets();
80 for (int i = 0; i < barsets.count(); i++) {
81 for (int i = 0; i < barsets.count(); i++) {
81 seriesColorHex = "#" + QString::number(barsets.at(i)->brush().color().rgb(), 16).right(6).toUpper();
82 seriesColorHex = "#" + QString::number(barsets.at(i)->brush().color().rgb(), 16).right(6).toUpper();
82 model->addMapping(seriesColorHex, QRect(1 + i, first, 1, barsets.at(i)->count()));
83 model->addMapping(seriesColorHex, QRect(1 + i, first, 1, barsets.at(i)->count()));
83 }
84 }
84 //! [5]
85 //! [5]
85
86
86 //! [6]
87 //! [6]
87 QStringList categories;
88 QStringList categories;
88 categories << "April" << "May" << "June" << "July" << "August";
89 categories << "April" << "May" << "June" << "July" << "August";
89
90
90 chart->axisX()->categories()->insert(categories);
91 chart->axisX()->categories()->insert(categories);
91 //! [6]
92 //! [6]
92
93
93 //! [7]
94 //! [7]
94 QChartView *chartView = new QChartView(chart);
95 QChartView *chartView = new QChartView(chart);
95 chartView->setRenderHint(QPainter::Antialiasing);
96 chartView->setRenderHint(QPainter::Antialiasing);
96 chartView->setMinimumSize(640, 480);
97 chartView->setMinimumSize(640, 480);
97 //! [7]
98 //! [7]
98
99
99 //! [8]
100 //! [8]
100 // create main layout
101 // create main layout
101 QGridLayout* mainLayout = new QGridLayout;
102 QGridLayout* mainLayout = new QGridLayout;
102 mainLayout->addWidget(tableView, 1, 0);
103 mainLayout->addWidget(tableView, 1, 0);
103 mainLayout->addWidget(chartView, 1, 1);
104 mainLayout->addWidget(chartView, 1, 1);
104 mainLayout->setColumnStretch(1, 1);
105 mainLayout->setColumnStretch(1, 1);
105 mainLayout->setColumnStretch(0, 0);
106 mainLayout->setColumnStretch(0, 0);
106 setLayout(mainLayout);
107 setLayout(mainLayout);
107 //! [8]
108 //! [8]
108 }
109 }
General Comments 0
You need to be logged in to leave comments. Login now