Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. I can't use QtChart! please help me to correct my code or write simple program with QtChart
Forum Updated to NodeBB v4.3 + New Features

I can't use QtChart! please help me to correct my code or write simple program with QtChart

Scheduled Pinned Locked Moved Solved QML and Qt Quick
11 Posts 5 Posters 3.9k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    PooyaSis
    wrote on last edited by
    #1

    I'm trying for a week and still I'm not able to run this simple program...
    Whats the problem with my codes?
    It gives me this error

    0_1502786201933_error.png

    .pro

    TEMPLATE = app
    
    QT += qml quick
    QT += widgets
    CONFIG += c++11
    
    SOURCES += main.cpp
    
    RESOURCES += qml.qrc
    
    # Additional import path used to resolve QML modules in Qt Creator's code model
    QML_IMPORT_PATH =
    
    # Additional import path used to resolve QML modules just for Qt Quick Designer
    QML_DESIGNER_IMPORT_PATH =
    
    # The following define makes your compiler emit warnings if you use
    # any feature of Qt which as been marked 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
    
    # Default rules for deployment.
    qnx: target.path = /tmp/$${TARGET}/bin
    else: unix:!android: target.path = /opt/$${TARGET}/bin
    !isEmpty(target.path): INSTALLS += target
    
    

    .cpp

    #include <QApplication>
    #include <QQmlApplicationEngine>
    
    int main(int argc, char *argv[])
    {
        QGuiApplication app(argc, argv);
    
        QQmlApplicationEngine engine;
        engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
        if (engine.rootObjects().isEmpty())
            return -1;
    
        return app.exec();
    }
    
    

    .qlm

    import QtQuick 2.6
    import QtQuick.Window 2.2
    import QtCharts 2.2
    
    ChartView {
        width: 400
        height: 300
        theme: ChartView.ChartThemeBrownSand
        antialiasing: true
    
        PieSeries {
            id: pieSeries
            PieSlice { label: "eaten"; value: 94.9 }
            PieSlice { label: "not yet eaten"; value: 5.1 }
        }
    }
    
    E 1 Reply Last reply
    0
    • ? Offline
      ? Offline
      A Former User
      wrote on last edited by
      #2

      Hi! I'm not sure / can't test it right now; maybe you need to add QT += charts to the project file.

      1 Reply Last reply
      2
      • P PooyaSis

        I'm trying for a week and still I'm not able to run this simple program...
        Whats the problem with my codes?
        It gives me this error

        0_1502786201933_error.png

        .pro

        TEMPLATE = app
        
        QT += qml quick
        QT += widgets
        CONFIG += c++11
        
        SOURCES += main.cpp
        
        RESOURCES += qml.qrc
        
        # Additional import path used to resolve QML modules in Qt Creator's code model
        QML_IMPORT_PATH =
        
        # Additional import path used to resolve QML modules just for Qt Quick Designer
        QML_DESIGNER_IMPORT_PATH =
        
        # The following define makes your compiler emit warnings if you use
        # any feature of Qt which as been marked 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
        
        # Default rules for deployment.
        qnx: target.path = /tmp/$${TARGET}/bin
        else: unix:!android: target.path = /opt/$${TARGET}/bin
        !isEmpty(target.path): INSTALLS += target
        
        

        .cpp

        #include <QApplication>
        #include <QQmlApplicationEngine>
        
        int main(int argc, char *argv[])
        {
            QGuiApplication app(argc, argv);
        
            QQmlApplicationEngine engine;
            engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
            if (engine.rootObjects().isEmpty())
                return -1;
        
            return app.exec();
        }
        
        

        .qlm

        import QtQuick 2.6
        import QtQuick.Window 2.2
        import QtCharts 2.2
        
        ChartView {
            width: 400
            height: 300
            theme: ChartView.ChartThemeBrownSand
            antialiasing: true
        
            PieSeries {
                id: pieSeries
                PieSlice { label: "eaten"; value: 94.9 }
                PieSlice { label: "not yet eaten"; value: 5.1 }
            }
        }
        
        E Offline
        E Offline
        Eeli K
        wrote on last edited by
        #3

        @PooyaSis This is hidden in the "Qt Charts QML Types" documentation:

        Note: Since Qt Creator 3.0 the project created with Qt Quick Application wizard based on Qt Quick 2 template uses QGuiApplication by default. As Qt Charts utilizes Qt Graphics View Framework for drawing, QApplication must be used. The project created with the wizard is usable with Qt Charts after the QGuiApplication is replaced with QApplication.

        It seems to require QT += widgets in the project file.

        P 1 Reply Last reply
        3
        • E Eeli K

          @PooyaSis This is hidden in the "Qt Charts QML Types" documentation:

          Note: Since Qt Creator 3.0 the project created with Qt Quick Application wizard based on Qt Quick 2 template uses QGuiApplication by default. As Qt Charts utilizes Qt Graphics View Framework for drawing, QApplication must be used. The project created with the wizard is usable with Qt Charts after the QGuiApplication is replaced with QApplication.

          It seems to require QT += widgets in the project file.

          P Offline
          P Offline
          PooyaSis
          wrote on last edited by
          #4

          @Eeli-K
          I have added QT += charts . But it's not working.

          E 2 Replies Last reply
          0
          • P PooyaSis

            @Eeli-K
            I have added QT += charts . But it's not working.

            E Offline
            E Offline
            Eeli K
            wrote on last edited by
            #5

            @PooyaSis no need for charts, your current project file should be enough because it has the qml related modules and widgets. But you need to use QApplication instead of QGuiApplication in main.cpp as the documentation says.

            B 1 Reply Last reply
            2
            • P PooyaSis

              @Eeli-K
              I have added QT += charts . But it's not working.

              E Offline
              E Offline
              Eeli K
              wrote on last edited by
              #6

              @PooyaSis (You told in private message it still didn't work, I answer here because it may help others with the same kind of problem.) Do you have Qt Charts installed? In the Maintenance Tool it must be selected and installed, it's not installed by default. Otherwise please tell if it's still the same error message or something else.

              P 1 Reply Last reply
              0
              • E Eeli K

                @PooyaSis (You told in private message it still didn't work, I answer here because it may help others with the same kind of problem.) Do you have Qt Charts installed? In the Maintenance Tool it must be selected and installed, it's not installed by default. Otherwise please tell if it's still the same error message or something else.

                P Offline
                P Offline
                PooyaSis
                wrote on last edited by
                #7

                @Eeli-K yes, I've installed Qt Charts.
                now I can run the program without any error, but it's not showing anything!!

                E 1 Reply Last reply
                0
                • P PooyaSis

                  @Eeli-K yes, I've installed Qt Charts.
                  now I can run the program without any error, but it's not showing anything!!

                  E Offline
                  E Offline
                  Eeli K
                  wrote on last edited by
                  #8

                  @PooyaSis Try https://doc.qt.io/qt-5/qtcharts-qmlchart-example.html (available in Qt Creator examples section). Does it work? If it does, the problem may be in your own project. If not, the problem may be in your system or Qt installation. If the example doesn't work, do other QML Quick examples work?

                  P 1 Reply Last reply
                  0
                  • E Eeli K

                    @PooyaSis Try https://doc.qt.io/qt-5/qtcharts-qmlchart-example.html (available in Qt Creator examples section). Does it work? If it does, the problem may be in your own project. If not, the problem may be in your system or Qt installation. If the example doesn't work, do other QML Quick examples work?

                    P Offline
                    P Offline
                    PooyaSis
                    wrote on last edited by
                    #9

                    @Eeli-K hi!
                    examples are working just fine!!!
                    but I don't know whats the matter with my Qt_program...

                    1 Reply Last reply
                    0
                    • S Offline
                      S Offline
                      sDfasd
                      wrote on last edited by sDfasd
                      #10

                      use QApplication instead of QGuiApplication

                      1 Reply Last reply
                      0
                      • E Eeli K

                        @PooyaSis no need for charts, your current project file should be enough because it has the qml related modules and widgets. But you need to use QApplication instead of QGuiApplication in main.cpp as the documentation says.

                        B Offline
                        B Offline
                        bayang
                        wrote on last edited by
                        #11

                        @Eeli-K Thanks, it works for me :)

                        1 Reply Last reply
                        0

                        • Login

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • Users
                        • Groups
                        • Search
                        • Get Qt Extensions
                        • Unsolved