QChartView or QGraphicScene



  • Dear All,

    I'm currently learning how to use QChart and I don't know if I need to use the easy way with QchartView or the hard one using QGraphic Scene.
    what are the difference between the two methods? I'm developping an app that's going to display a lot of data (100k points) every 100ms. Is Qchart view as performant as the long way?
    thanks in a advance



  • I actually have a more pressing matter

    I'm trying to compile :

    #ifndef CHARTWIDGET_H
    #define CHARTWIDGET_H
    
    #include <QObject>
    #include <QtWidgets/QWidget>
    
    #include <QtCharts/QChartGlobal>
    
    QT_CHARTS_BEGIN_NAMESPACE
    class QChartView;
    class QChart;
    QT_CHARTS_END_NAMESPACE
    
    
    class chartWidget : public QWidget
    {
        Q_OBJECT
    
    public:
        explicit chartWidget(QWidget *parent);
        ~chartWidget();
    
    private:   
        QChart *m_chart;
        QScatterSeries *m_scSerie;
        QLineSeries *m_lineSerie;
        QChartView *m_chartView;
    
    };
    #endif // CHART_H
    

    And I get "QChart does not name a type"
    I' don't know what is wrong with this code I almost entirely copied the code from the "chartthemes" exemple....
    any help would be welcome :D


  • Lifetime Qt Champion

    Hi

    Did you add QT += charts to your .pro file ?



  • 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
    
    

  • Lifetime Qt Champion

    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!


  • Lifetime Qt Champion

    What example are you referring to ?



  • it was themedWidget


  • Lifetime Qt Champion

    Where did you get that example ?



  • in qt creator.


  • Lifetime Qt Champion

    Can you give the exact name please ?


  • Moderators

    @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*, and QScatterSeries*.

    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 or QScatterSeries in the header: That's why the example doesn't need to forward-declare these two classes.

    Note that the example:

    1. Forward-declares QChart and QChartView in the header file: https://doc.qt.io/qt-5/qtcharts-chartthemes-themewidget-h.html
    2. #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 and QT_CHARTS_END_NAMESPACE from your code. These macros are not strictly necessary.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.