##// END OF EJS Templates
modeldata example documented with explanations....
Marek Rosa -
r935:f71e86066044
parent child
Show More
1 NO CONTENT: new file 100644, binary diff hidden
@@ -25,6 +25,7
25 25 #include <QChartAxis>
26 26 #include <QBarSet>
27 27 #include <QBarSeries>
28 #include <QLegend>
28 29
29 30 #include <QGridLayout>
30 31 #include <QPushButton>
@@ -60,7 +61,7 Widget::Widget(QWidget *parent)
60 61 QPushButton* refreshButton = new QPushButton(tr("Refresh"));
61 62 connect(refreshButton, SIGNAL(clicked()), this, SLOT(refreshChart()));
62 63
63 QPushButton* printButton = new QPushButton(tr("Print chart"));
64 QPushButton* printButton = new QPushButton(tr("Print to pdf"));
64 65 connect(printButton, SIGNAL(clicked()), this, SLOT(printChart()));
65 66
66 67 QVBoxLayout* rightPanelLayout = new QVBoxLayout;
@@ -75,6 +76,7 Widget::Widget(QWidget *parent)
75 76
76 77 QChart *chart = new QChart();
77 78 chart->setTitle("GDP by country");
79 chart->legend()->setVisible(true);
78 80
79 81 // main layout
80 82 chartView = new QChartView(chart);
@@ -101,13 +103,6 Widget::Widget(QWidget *parent)
101 103 while (query.next()) {
102 104 countrieslist->addItem(query.value(0).toString());
103 105 }
104
105 // hide axis X labels
106 //QChartAxis* axis = chartArea->axisX();
107 // axis->
108 // axis->setLabelsVisible(false);
109 // newAxis.setLabelsOrientation(QChartAxis::LabelsOrientationSlide);
110
111 106 }
112 107
113 108 Widget::~Widget()
@@ -136,7 +131,7 void Widget::refreshChart()
136 131 QList<QListWidgetItem*> selectedYearsItems = yearslist->selectedItems();
137 132 for (int i = 0; i < selectedYearsItems.size(); i++)
138 133 selectedYearsInts.append(selectedYearsItems[i]->text().toInt());
139 qSort(selectedYearsInts.begin(), selectedYearsInts.end(), qGreater<int>());
134 qSort(selectedYearsInts.begin(), selectedYearsInts.end());
140 135
141 136 if (barChartRadioButton->isChecked())
142 137 {
@@ -162,7 +157,7 void Widget::refreshChart()
162 157 for (int i = 0; i < selectedYearsInts.size(); i++)
163 158 {
164 159 query.exec("SELECT country,gdpvalue FROM gdp2 where year=" + QString("%1").arg(selectedYearsInts[i]) + " AND " + countriesQuery);
165 QBarSet* barSet = new QBarSet("Barset" + QString::number(i));
160 QBarSet* barSet = new QBarSet(QString::number(selectedYearsInts[i]));
166 161
167 162 // while (query.next()) {
168 163 // qDebug() << query.value(0).toString() << " : " << query.value(1).toString();
@@ -210,6 +205,7 void Widget::refreshChart()
210 205 query.first();
211 206
212 207 QScatterSeries* series = new QScatterSeries;
208 series->setName(selectedCountriesStrings[i]);
213 209 // the data for some of the coutries for some years might be missing.
214 210 for (int k = 0; k < selectedYearsInts.size(); k++)
215 211 {
@@ -4,4 +4,53
4 4 \subtitle
5 5
6 6 This example shows how to use QAbstractItemModel derived model as the data for the series.
7
8 \image modeldata.png
9
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.
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
14 \snippet ../examples/modeldata/tablewidget.cpp 1
15
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.
18
19 \snippet ../examples/modeldata/tablewidget.cpp 2
20
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.
23
24 \snippet ../examples/modeldata/tablewidget.cpp 3
25
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.
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.
30
31 \snippet ../examples/modeldata/tablewidget.cpp 4
32
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.
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.
37
38 \snippet ../examples/modeldata/tablewidget.cpp 5
39
40 The same operations are done with second series. Notice that for this series different columns of the same model are mapped.
41
42 \snippet ../examples/modeldata/tablewidget.cpp 6
43
44 \snippet ../examples/modeldata/tablewidget.cpp 7
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.
47 To make the render look nicer Antialiasing is turned on and the minimum size of the chart is set.
48
49 \snippet ../examples/modeldata/tablewidget.cpp 8
50
51 Finally we place both widgets in a layout and use the layout as the application layout.
52
53 \snippet ../examples/modeldata/tablewidget.cpp 9
54
55 Application is ready. Try modifying the data in the table view and see how it affects the chart.
7 56 */
@@ -33,50 +33,69 TableWidget::TableWidget(QWidget *parent)
33 33 {
34 34 // create simple model for storing data
35 35 // user's table data model
36 //! [1]
36 37 CustomTableModel *model = new CustomTableModel;
38 //! [1]
37 39
40 //! [2]
38 41 // create table view and add model to it
39 42 QTableView *tableView = new QTableView;
40 43 tableView->setModel(model);
44 //! [2]
41 45 tableView->setColumnWidth(0, 56);
42 46 tableView->setColumnWidth(1, 56);
43 47 tableView->setColumnWidth(2, 56);
44 48 tableView->setColumnWidth(3, 56);
45 49
46 QChart *m_chart = new QChart;
47 m_chart->setAnimationOptions(QChart::AllAnimations);
48 QChartView *m_chartView = new QChartView(m_chart);
49 m_chartView->setRenderHint(QPainter::Antialiasing);
50 m_chartView->setMinimumSize(640, 480);
50 //! [3]
51 QChart *chart = new QChart;
52 chart->setAnimationOptions(QChart::AllAnimations);
53 //! [3]
51 54
55 // series 1
56 //! [4]
57 QLineSeries *series = new QLineSeries;
58 series->setModel(model);
59 series->setModelMapping(0, 1, Qt::Vertical);
60 chart->addSeries(series);
61 //! [4]
62
63 //! [5]
52 64 // for storing color hex from the series
53 65 QString seriesColorHex = "#000000";
54 66
55 // series 1
56 QLineSeries *m_series = new QLineSeries;
57 m_series->setModel(model);
58 m_series->setModelMapping(0, 1, Qt::Vertical);
59 m_chart->addSeries(m_series);
60
61 67 // get the color of the series and use it for showing the mapped area
62 seriesColorHex = "#" + QString::number(m_series->pen().color().rgb(), 16).right(6).toUpper();
68 seriesColorHex = "#" + QString::number(series->pen().color().rgb(), 16).right(6).toUpper();
63 69 model->addMapping(seriesColorHex, QRect(0, 0, 2, model->rowCount()));
70 //! [5]
64 71
65 72 // series 2
66 m_series = new QLineSeries;
67 m_series->setModel(model);
68 m_series->setModelMapping(2,3, Qt::Vertical);
69 m_chart->addSeries(m_series);
73 //! [6]
74 series = new QLineSeries;
75 series->setModel(model);
76 series->setModelMapping(2,3, Qt::Vertical);
77 chart->addSeries(series);
78 //! [6]
70 79
80 //! [7]
71 81 // get the color of the series and use it for showing the mapped area
72 seriesColorHex = "#" + QString::number(m_series->pen().color().rgb(), 16).right(6).toUpper();
82 seriesColorHex = "#" + QString::number(series->pen().color().rgb(), 16).right(6).toUpper();
73 83 model->addMapping(seriesColorHex, QRect(2, 0, 2, model->rowCount()));
84 //! [7]
85
86 //! [8]
87 QChartView *chartView = new QChartView(chart);
88 chartView->setRenderHint(QPainter::Antialiasing);
89 chartView->setMinimumSize(640, 480);
90 //! [8]
74 91
92 //! [9]
75 93 // create main layout
76 94 QGridLayout* mainLayout = new QGridLayout;
77 95 mainLayout->addWidget(tableView, 1, 0);
78 mainLayout->addWidget(m_chartView, 1, 1);
96 mainLayout->addWidget(chartView, 1, 1);
79 97 mainLayout->setColumnStretch(1, 1);
80 98 mainLayout->setColumnStretch(0, 0);
81 99 setLayout(mainLayout);
100 //! [9]
82 101 }
@@ -151,11 +151,6 QTCOMMERCIALCHART_BEGIN_NAMESPACE
151 151 */
152 152
153 153 /*!
154 \fn void QChartAxis::handleAxisRangeChanged(qreal min, qreal max)
155 \brief \internal \a min \a max
156 */
157
158 /*!
159 154 Constructs new axis object which is a child of \a parent. Ownership is taken by
160 155 QChatView or QChart when axis added.
161 156 */
@@ -402,6 +397,9 void QChartAxis::hide()
402 397 emit updated();
403 398 }
404 399
400 /*!
401 \internal
402 */
405 403 void QChartAxis::handleAxisRangeChanged(qreal min, qreal max,int count)
406 404 {
407 405 setRange(min,max);
@@ -26,7 +26,7
26 26
27 27
28 28 /*!
29 \enum QChartView::RubberBandPolicy
29 \enum QChartView::RubberBand
30 30
31 31 This enum describes the different types of rubber bands that can be used for zoom rect selection
32 32
@@ -73,7 +73,7
73 73 */
74 74
75 75 /*!
76 \property QString QSeries::name
76 \property QSeries::name
77 77 \brief name of the series property
78 78 */
79 79
@@ -23,7 +23,6
23 23
24 24 #include <qchartglobal.h>
25 25 #include <QObject>
26 //#include <QAbstractItemModel>
27 26 #include <QPen>
28 27
29 28 class QAbstractItemModel;
General Comments 0
You need to be logged in to leave comments. Login now