Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Qt Academy Launch in California!

    Unsolved I cannot include "QApplication" into a QtQuick project. I'm using Visual Studio 2022.

    Qt in Education
    3
    6
    200
    Loading More Posts
    • 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.
    • Z
      ZeeoTwo last edited by

      So basically i tried using QtCharts, and i got an error, i found out i need to change my QGuiApplication into QApplication.
      As far as i read on the internet i just had to #include <QtWidgets/qapplication.h>
      and i ended up with code like this:

      #include <QGuiApplication>
      #include <QQmlApplicationEngine>
      #include <QtWidgets/qapplication.h>`
      
      int main(int argc, char* argv[]) {
      #if defined(Q_OS_WIN)
          QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
      #endif
      
          QApplication app(argc, argv);
      
          QQmlApplicationEngine engine;
      
          engine.addImportPath("qrc:/");
      
          engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
          if (engine.rootObjects().isEmpty())
              return -1;
      
          return app.exec();
      }
      
      

      and it should work. Unfortunately it don't.

      How can i fix this error.?

      My compiler can find this file but it can't find qapplication.cpp where all the definitions are.
      I also do not have any .pro file neither qmake or cmake.

      Do i need to install something in qt maintenance tool (i already installed Qt Charts)
      Do i need to create a new project? (i would preffer not to)

      My Qt version: 6.4.0
      My compiler version: 35ea9c48-527e-40dd-9ebc-293d76d0630c-obraz.png

      1 Reply Last reply Reply Quote 0
      • TomZ
        TomZ last edited by

        @ZeeoTwo said in I cannot include "QApplication" into a QtQuick project. I'm using Visual Studio 2022.:

        I also do not have any .pro file neither qmake or cmake.

        So, what buildsystem do you use?

        You likely are simply not using the QWidgets module in your project.

        1 Reply Last reply Reply Quote 0
        • Z
          ZeeoTwo last edited by

          @TomZ said in I cannot include "QApplication" into a QtQuick project. I'm using Visual Studio 2022.:

          So, what buildsystem do you use?

          i found this folder in my project:
          db103f37-9ac0-46e0-b7a0-5be4341f238b-image.png
          so qmake i guess but i do not have any qmake file in acctual project folder

          You likely are simply not using the QWidgets module in your project.

          Soo how can i start using it?
          Can i do it simply and fast or do i need to just learn qmake and figure it out?

          J.Hilk 1 Reply Last reply Reply Quote 0
          • J.Hilk
            J.Hilk Moderators @ZeeoTwo last edited by

            @ZeeoTwo there's a QML version of QCharts:
            https://doc.qt.io/qt-6/qtcharts-qmlchart-example.html

            use that one. Don't mix QML and QWidget if you don't have to & also the qml variant is much more performant than the QWidget one

            Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct

            Qt Needs YOUR vote: https://bugreports.qt.io/browse/QTQAINFRA-4121


            Q: What's that?
            A: It's blue light.
            Q: What does it do?
            A: It turns blue.

            Z 1 Reply Last reply Reply Quote 0
            • Z
              ZeeoTwo @J.Hilk last edited by

              @J-Hilk

              @ZeeoTwo there's a QML version of QCharts:

              i know there is a qml version but i can't use it without changing from QGuiApplication to QApplication

              this is my qml file that im loading using loader into my main.qml:

              import QtQuick
              import QtCharts 2.4
              import QtQuick.Controls 2.15
              import QtQuick.Layouts 1.15
              import Config 1.0
                  Rectangle{
                      id: home
                  
                          ChartView {
                          id: chart
                          title: "Top-5 car brand shares in Finland"
                          anchors.fill: parent
                          legend.alignment: Qt.AlignBottom
                          antialiasing: true
              
                              PieSeries {
                                  id: pieSeries
                                  PieSlice { label: "Volkswagen"; value: 13.5 }
                                  PieSlice { label: "Toyota"; value: 10.9 }
                                  PieSlice { label: "Ford"; value: 8.6 }
                                  PieSlice { label: "Skoda"; value: 8.2 }
                                  PieSlice { label: "Volvo"; value: 6.8 }
                              }
                          }
                  }
              
              

              and after running it using QGuiApplication i get:
              8049efaf-64ec-4e61-b331-910f7f7e220c-image.png
              1c1d324b-a89d-40c3-a850-24d48c234f08-image.png

              1 Reply Last reply Reply Quote 0
              • Z
                ZeeoTwo last edited by

                Okay i got it working.
                I'm using Qt Vs Tools extension.
                To make it work i had to open:
                Extensions > Qt VS Tools > Qt Project Settings > Qt Project Settings (on the left) > Qt Modules
                and inside that check QT Widgets.
                I guess this is something like a pseudo .pro file or something.
                and after including #include <QtWidgets/qapplication.h> inside main.cpp
                and changing from QGuiApplication to QApplication inside main.cpp
                everything worked, i got no errors and a chart.

                Thank you for ur time @J-Hilk and @TomZ

                1 Reply Last reply Reply Quote 1
                • First post
                  Last post