Solved QChartView or QGraphicScene
-
Thanks for your reply.
Yes i did , but it still doesn't work....
here is my .pro just in case :#------------------------------------------------- # # Project created by QtCreator 2017-07-27T14:27:37 # #------------------------------------------------- QT += core gui \ charts greaterThan(QT_MAJOR_VERSION, 4): QT += widgets TARGET = aoFcs TEMPLATE = app # The following define makes your compiler emit warnings if you use # any feature of Qt which as been marked as deprecated (the exact warnings # depend on your compiler). Please consult the documentation of the # deprecated API in order to know how to port your code away from it. DEFINES += QT_DEPRECATED_WARNINGS # You can also make your code fail to compile if you use deprecated APIs. # In order to do so, uncomment the following line. # You can also select to disable deprecated APIs only up to a certain version of Qt. #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0 SOURCES += main.cpp\ tools.cpp \ chartWidget.cpp \ mainWindow.cpp \ settingWidget.cpp HEADERS += \ tools.h \ chartWidget.h \ mainWindow.h \ settingWidget.h
-
And re-ran qmake after adding the module ?
-
It still doesn't work.
I compared qmake and make instruction of my project and a qt example that works and they are striclty the same apart from the directory adress. -
So apparently declaring
#include <QtCharts/QChartGlobal>
was not enough
I had to declare :
#include <QtCharts/QChartGlobal> #include <QtCharts/QChart> #include <QtCharts/QChartView> #include <QtCharts/QLineSeries> #include <QtCharts/QScatterSeries>
Don't ask me why it works in the examples and not in my project...
Thanks for you help! -
What example are you referring to ?
-
it was themedWidget
-
Where did you get that example ?
-
in qt creator.
-
Can you give the exact name please ?
-
@Zhitoune said in QChartView or QGraphicScene:
So apparently declaring
#include <QtCharts/QChartGlobal>
was not enough
I had to declare :
#include <QtCharts/QChartGlobal> #include <QtCharts/QChart> #include <QtCharts/QChartView> #include <QtCharts/QLineSeries> #include <QtCharts/QScatterSeries>
There are two step you should take to do this correctly.
First, use Forward Declarations in your header:
class QChart; class QChartView; class QLineSeries; class QScatterSeries;
You need the forward declarations because your header contains the pointers
QChart*
,QChartView*
,QLineSeries*
, andQScatterSeries*
.Second, include the relevant headers in your .cpp file:
#include <QtCharts/QChart> #include <QtCharts/QChartView> #include <QtCharts/QLineSeries> #include <QtCharts/QScatterSeries> // OR, even better: #include <QChart> #include <QChartView> #include <QLineSeries> #include <QScatterSeries>
Don't ask me why it works in the examples and not in my project...
One reason is because the example does not refer to
QLineSeries
orQScatterSeries
in the header: That's why the example doesn't need to forward-declare these two classes.Note that the example:
- Forward-declares
QChart
andQChartView
in the header file: https://doc.qt.io/qt-5/qtcharts-chartthemes-themewidget-h.html - #includes lots of chart-related classes in the .cpp file: https://doc.qt.io/qt-5/qtcharts-chartthemes-themewidget-cpp.html
Finally, you can get rid of
QT_CHARTS_BEGIN_NAMESPACE
andQT_CHARTS_END_NAMESPACE
from your code. These macros are not strictly necessary. - Forward-declares