##// END OF EJS Templates
Copy database to bin directory
Marek Rosa -
r242:2883e9a758d6
parent child
Show More
@@ -1,23 +1,31
1 1 #-------------------------------------------------
2 2 #
3 3 # Project created by QtCreator 2012-02-14T15:27:32
4 4 #
5 5 #-------------------------------------------------
6 6
7 7 !include( ../../common.pri ) {
8 8 error( "Couldn't find the common.pri file!" )
9 9 }
10 10 !include( ../../integrated.pri ) {
11 11 error( "Couldn't find the integrated.pri file !")
12 12 }
13 13
14 14 QT += core gui sql
15 15
16 16 TARGET = gdpbarchart
17 17 TEMPLATE = app
18 18
19 19
20 20 SOURCES += main.cpp\
21 21 widget.cpp
22 22
23 23 HEADERS += widget.h
24
25 COPY_DATABASE_COMMAND = "cp gdpData $$CHART_BUILD_BIN_DIR/gdpData"
26
27 win32:{
28 COPY_DATABASE_COMMAND = $$replace(COPY_DATABASE_COMMAND, "/","\\")
29 }
30
31 QMAKE_POST_LINK = $$COPY_DATABASE_COMMAND
@@ -1,155 +1,155
1 1 /*!
2 2 \class Widget
3 3 \brief Ui for the application.
4 4 */
5 5
6 6 #include "widget.h"
7 7 #include <QGridLayout>
8 8 #include <QPushButton>
9 9 #include <QLabel>
10 10
11 11 #include <QSqlQuery>
12 12 #include <qscatterseries.h>
13 13 #include <qchartview.h>
14 14 #include <qchartaxis.h>
15 15 #include <qbarchartseries.h>
16 16 #include <qbarcategory.h>
17 17 #include <qbarset.h>
18 18 #include <QListWidget>
19 19
20 20 QTCOMMERCIALCHART_USE_NAMESPACE
21 21
22 22 Widget::Widget(QWidget *parent)
23 23 : QWidget(parent)
24 24 {
25 setGeometry(100, 100, 800, 600);
25 setGeometry(100, 100, 1000, 600);
26 26
27 27 // right panel layout
28 28 countrieslist = new QListWidget;
29 29 countrieslist->setSelectionMode(QAbstractItemView::MultiSelection);
30 30
31 31 yearslist = new QListWidget;
32 32 yearslist->setSelectionMode(QAbstractItemView::ExtendedSelection);
33 33 for (int i = 1990; i < 2011; i++)
34 34 yearslist->addItem(QString("%1").arg(i));
35 35
36 36 QPushButton* refreshButton = new QPushButton(tr("Refresh"));
37 37 connect(refreshButton, SIGNAL(clicked()), this, SLOT(refreshChart()));
38 38
39 39 QVBoxLayout* rightPanelLayout = new QVBoxLayout;
40 40 rightPanelLayout->addWidget(countrieslist);
41 41 rightPanelLayout->addWidget(yearslist);
42 42 rightPanelLayout->addWidget(refreshButton);
43 43 rightPanelLayout->setStretch(0, 1);
44 44 rightPanelLayout->setStretch(1, 0);
45 45
46 46 // main layout
47 47 chartArea = new QChartView(this);
48 48 chartArea->setChartTitle("GDP by country");
49 49 QGridLayout* mainLayout = new QGridLayout;
50 50 mainLayout->addWidget(chartArea, 0, 0);
51 51 mainLayout->addLayout(rightPanelLayout, 0, 1);
52 52 mainLayout->setColumnStretch(0,1);
53 53 setLayout(mainLayout);
54 54
55 55 // connect to the database
56 56 db = QSqlDatabase::addDatabase("QSQLITE");
57 db.setDatabaseName("../example/gdpbarchart/gdpData");
57 db.setDatabaseName("gdpData");
58 58 if(!db.open())
59 59 {
60 60 qDebug() << "could not open database. SQLite db file missing (?)";
61 61 return;
62 62 }
63 63
64 64 // get the list of all countires and regions.
65 65 QSqlQuery query;
66 66 query.exec("SELECT DISTINCT country FROM gdp2");
67 67
68 68 // add the countries to the country filter
69 69 while (query.next()) {
70 70 countrieslist->addItem(query.value(0).toString());
71 71 }
72 72
73 73 // hide axis X labels
74 74 QChartAxis* axis = chartArea->axisX();
75 75 axis->setLabelsVisible(false);
76 76 // newAxis.setLabelsOrientation(QChartAxis::LabelsOrientationSlide);
77 77
78 78 }
79 79
80 80 Widget::~Widget()
81 81 {
82 82 //
83 83 db.close();
84 84 }
85 85
86 86 /*!
87 87 refreshes the chart
88 88 */
89 89 void Widget::refreshChart()
90 90 {
91 91 // selected countries items list is not sorted. copy the values to QStringlist and sort them.
92 92 QStringList selectedCountriesStrings;
93 93 QList<QListWidgetItem*> selectedCountriesItems = countrieslist->selectedItems();
94 94 for (int i = 0; i < selectedCountriesItems.size(); i++)
95 95 selectedCountriesStrings.append(selectedCountriesItems[i]->text());
96 96 selectedCountriesStrings.sort();
97 97
98 98 // use the sorted selected coutries list to initialize BarCategory
99 99 QBarCategory* category = new QBarCategory;
100 100 for (int i = 0; i < selectedCountriesStrings.size(); i++)
101 101 *category << selectedCountriesStrings[i];
102 102 QBarChartSeries* series0 = new QBarChartSeries(category);
103 103
104 104 // prepare the selected counries SQL query
105 105 QString countriesQuery = "country IN (";
106 106 for (int i = 0; i < selectedCountriesStrings.size(); i++)
107 107 {
108 108 countriesQuery.append("'" + selectedCountriesStrings[i] + "'");
109 109 if ( i < selectedCountriesStrings.size() - 1)
110 110 countriesQuery.append(",");
111 111 else
112 112 countriesQuery.append(")");
113 113 }
114 114
115 115 QSqlQuery query;
116 116 // selected years items list is not sorted. copy the values to QList<Integer> and sort them.
117 117 QList<int> selectedYearsInts;
118 118 QList<QListWidgetItem*> selectedYearsItems = yearslist->selectedItems();
119 119 for (int i = 0; i < selectedYearsItems.size(); i++)
120 120 selectedYearsInts.append(selectedYearsItems[i]->text().toInt());
121 121 qSort(selectedYearsInts);
122 122
123 123 // perform a query for each selected year
124 124 for (int i = 0; i < selectedYearsInts.size(); i++)
125 125 {
126 126 query.exec("SELECT country,gdpvalue FROM gdp2 where year=" + QString("%1").arg(selectedYearsInts[i]) + " AND " + countriesQuery);
127 127 QBarSet* barSet = new QBarSet;
128 128 // while (query.next()) {
129 129 // qDebug() << query.value(0).toString() << " : " << query.value(1).toString();
130 130 // }
131 131 query.first();
132 132
133 133 // the data for some of the coutries for some years might be missing.
134 134 // QBarChart needs bars to have same size
135 135 for (int k = 0; k < selectedCountriesStrings.size(); k++)
136 136 {
137 137 if (selectedCountriesStrings[k] == query.value(0).toString())
138 138 {
139 139 *barSet << query.value(1).toReal();
140 140 qDebug() << query.value(0).toString() << query.value(1).toReal() << " : " << QString("%1").arg(selectedYearsInts[i]);
141 141 query.next();
142 142 }
143 143 else
144 144 {
145 145 // data missing, put 0
146 146 *barSet << 0.0f;
147 147 qDebug() << "Putting 0 for Bosnia" << " : " << QString("%1").arg(selectedYearsInts[i]);
148 148 }
149 149 }
150 150 series0->addBarSet(barSet);
151 151 }
152 152
153 153 // add the serie to the chart
154 154 chartArea->addSeries(series0);
155 155 }
General Comments 0
You need to be logged in to leave comments. Login now