c++qtqmlqt-creatorqtcharts

How to convert QGuiApplication to QApplication in QtCreator 10 with Qt 6?


I'm trying to use PolarChartView but when I run my code , my application crashed and the qt creator just tells me application crashed. I searched and I found out that I have to convert QGuiApplication to QApplication but when I changed my main.cpp file into this , I got some strange errors. I'm writing a QtQuick Application using QML. What's the correct way of converting ?

Main.cpp file :

#include <QtWidgets/QApplication>
#include <QQmlApplicationEngine>


int main(int argc, char *argv[])
{
    QApplication app(argc, argv);

    QQmlApplicationEngine engine;
    const QUrl url(u"qrc:/RadarChart/Main.qml"_qs);
    QObject::connect(&engine, &QQmlApplicationEngine::objectCreationFailed,
        &app, []() { QCoreApplication::exit(-1); },
        Qt::QueuedConnection);
    engine.load(url);

    return app.exec();
}

Here are the errors :

:-1: error: D:/CppProjects/RadarChart/main.cpp:17: undefined reference to `__imp__ZN12QApplicationD1Ev'
:-1: error: D:/CppProjects/build-radarchart-Desktop_Qt_6_5_1_MinGW_64_bit-Debug/Debug_Desktop__9a53abaa43e654f2/radarchart.8c396ce1/3a52ce780950d4d9/main.cpp.o:D:/CppProjects/RadarChart/main.cpp:7: undefined reference to `__imp__ZN12QApplicationC1ERiPPci'

Solution

  • From Qbs Manual: Creating Dependencies to Qt Modules:

    The Qt modules are grouped using the prefix Qt. If your product depends on the Qt.core and Qt.network modules, you could write:

    Depends { name: "Qt.core" }
    Depends { name: "Qt.network" }
    

    Based on that, you should add Qt.widgets module to your .qbs file if you want to use QApplication:

    Depends { name: "Qt.widgets" }
    

    This was tested on a clean QML project and it works.

    Source:

    Credits: