I cannot include "QApplication" into a QtQuick project. I'm using Visual Studio 2022.
-
So basically i tried using QtCharts, and i got an error, i found out i need to change my
QGuiApplication
intoQApplication
.
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: -
@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.
-
@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:
so qmake i guess but i do not have anyqmake
file in acctual project folderYou 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? -
@ZeeoTwo there's a QML version of QCharts:
https://doc.qt.io/qt-6/qtcharts-qmlchart-example.htmluse 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
-
@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
toQApplication
this is my qml file that im loading using
loader
into mymain.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:
-
Okay i got it working.
I'm usingQt 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 checkQT Widgets
.
I guess this is something like a pseudo.pro
file or something.
and after including#include <QtWidgets/qapplication.h>
insidemain.cpp
and changing fromQGuiApplication
toQApplication
insidemain.cpp
everything worked, i got no errors and a chart. -