PDA

View Full Version : Qt Charts, PieChart setting up



time
15th November 2016, 11:00
Hello everyone :)

I want to start creating charts in Qt 5 with C++ (pie chart, bar graph etc.). I have problems with setting thinks up.
I visited the website: http://doc.qt.io/qt-5/qtcharts-index.html
They wrote:

1. To import Qt Charts QML types, add the following import statement to your .qml file :

import QtCharts 2.0
So, where is that .qml file? Where I should write it? I need to make it new?

2. If you intend to use Qt Charts C++ classes in your application, use the following include and using directives:

#include <QtCharts>
using namespace QtCharts;
That part should go to header (.h) ?

3. To link against the Qt Charts module, add this line to your qmake project file:

QT += charts
If I understand that good, this should go to .pro file?

Here is example of PieChart: http://doc.qt.io/qt-5/qtcharts-piechart-example.html
There are 2 files: piechart/main.cpp and piechart/piechart.pro

Can you write me very precisely where and how add this statements from the beginning of my post (1,2 and 3).
I want to rewrite this PieChart example to my project to learn how it's works. When I created the project, a have the following files:
- mainwindow.cpp
- mainwindow.h
- mainwindow.ui
- PieChartTest.pro
- PieChartTest.pro.user

Should I create something more?
Thank you for help!

anda_skoa
15th November 2016, 12:58
1. To import Qt Charts QML types, add the following import statement to your .qml file :

import QtCharts 2.0
So, where is that .qml file? Where I should write it? I need to make it new?

Well, the QML file you want to use that in.

The QML engine needs to know which modules to look into when you are using element types in your QML code.



2. If you intend to use Qt Charts C++ classes in your application, use the following include and using directives:

#include <QtCharts>
using namespace QtCharts;
That part should go to header (.h) ?

It is very uncommon to put a "using namespace" clause into a header.
If you don't need any of the classes as non-pointer/non-reference objects then a simple forward declaration should do and the include could be solely in your cpp file.



3. To link against the Qt Charts module, add this line to your qmake project file:

QT += charts
If I understand that good, this should go to .pro file?

Yes, the .pro file is the qmake project file mentioned above.


Can you write me very precisely where and how add this statements from the beginning of my post (1,2 and 3).
I want to rewrite this PieChart example to my project to learn how it's works. When I created the project, a have the following files:
- mainwindow.cpp
- mainwindow.h
- mainwindow.ui
- PieChartTest.pro
- PieChartTest.pro.user

Since this is not a QtQuick application you don't need (1).
(3) should be obvious, (2) would likely be in mainwindow.cpp

Cheers,
_

time
15th November 2016, 18:07
Hi, thank you for your answer anda_skoa :)
So I did what you write. But when I adding

#include <QtCharts>
using namespace QtCharts;
to mainwindow.cpp I have an error C1083: cannot open include file 'QtCharts': No such file or directory and in General Messages Project ERROR: Unknown module(s) in QT: charts and it points to line where is #include <QtCharts>
So I need some more files to my project? I don't get it...

To be clear I will add the code, maybe I am doing something wrong what is obvious?

Here I am adding point(3) the QT += charts


QT += core gui charts

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

TARGET = PieChartTest
TEMPLATE = app


SOURCES += main.cpp\
mainwindow.cpp

HEADERS += mainwindow.h

FORMS += mainwindow.ui


Here is mainwindow.cpp where is an error with #include <QtCharts>


#include "mainwindow.h"
#include "ui_mainwindow.h"

#include <QtCharts> //error C1083 here
using namespace QtCharts;

MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
}

MainWindow::~MainWindow()
{
delete ui;
}


It seems that I don't have a library connected with QtCharts. Should I download something additional?
EDIT: I use Open Source Licence for students if it is important

scgrant327
15th November 2016, 18:15
You have to use the QT Maintenance Tool to download Charts for 5.7+.
--Sam

time
15th November 2016, 18:57
You have to use the QT Maintenance Tool to download Charts for 5.7+.
--Sam

Yeap, that's right. It helped, thank you very much.
So, for next generation ;), how to solve this problem in few steps:

1. Go to Qt Maintenance Tool (it is in the folder where Qt is install)
2. Run it
3. Choose Add components
4. Expand list for you version which you have installed. Charts are available from Qt 5.7 (correct me if I'm wrong)
5. Choose QtCharts (I also choosed Qt Data Visualisation)
6. Click next
7. Restart your Qt program
8. Click Build -> Run qmake

Now it should work :)