Comprendre le fichier « main.cpp »

Comprendre le fichier principal pour notre code de moteur.

Le rôle de « main.cpp »

Alors que QML est utilisé pour l'interface utilisateur des applications avec Kirigami, le moteur est généralement écrit en C++, grâce à sa vitesse et à sa flexibilité. Bien que dans les pages précédentes, nous ayons parcouru QML de manière très approfondie, nous aurons besoin de mieux comprendre le code C++ de notre moteur afin de créer des applications plus utiles que ce que nous pouvons réaliser avec du QML pur.

Here, we'll be going over the main.cpp file we created in the Getting Started page so that we can better understand what is going on in the central C++ file of our application. While this is a basic main.cpp, the features we will go over will remain essential no matter what kind of application you decide to create.

Ce que cela fait

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <QApplication>
#include <QQmlApplicationEngine>
#include <QtQml>
#include <QUrl>
#include <QQuickStyle>
#include <KLocalizedContext>
#include <KLocalizedString>

int main(int argc, char *argv[])
{
    QApplication app(argc, argv);
    KLocalizedString::setApplicationDomain("helloworld");
    QCoreApplication::setOrganizationName(QStringLiteral("KDE"));
    QCoreApplication::setOrganizationDomain(QStringLiteral("kde.org"));
    QCoreApplication::setApplicationName(QStringLiteral("Hello World"));

    if (qEnvironmentVariableIsEmpty("QT_QUICK_CONTROLS_STYLE")) {
        QQuickStyle::setStyle(QStringLiteral("org.kde.desktop"));
    }

    QQmlApplicationEngine engine;

    engine.rootContext()->setContextObject(new KLocalizedContext(&engine));
    engine.load(QUrl(QStringLiteral("qrc:/main.qml")));

    if (engine.rootObjects().isEmpty()) {
        return -1;
    }

    return app.exec();
}

First we must include a number of Qt header files, allowing us to use their functions. In this case, we include a number of Qt headers that handle application logic, reading QML files, and accessing files defined in resources.qrc.

We then create a QApplication instance which we call app. We then pass argc and argv to the constructor, letting Qt parse and use arguments.

We also set some metadata relating to the application. These include the organisation that created the application, the organisation's website, and the name of the application. We set these properties by calling QApplication , instantiating an object that comes from QCoreApplication and provides the event loop for applications regardless of whether they have a GUI or not (so if we ran our program without the GUI, this metadata would still be set).

To make our app look native on Plasma, Windows or GNOME, we use QQuickStyle::setStyle() to make use of qqc2-desktop-style.

The QQmlApplicationEngine lets us load an application from a QML file, which we do in the next line. In engine.load(QUrl(QStringLiteral("qrc:/main.qml"))); we load our QML from a URL in our Qt Resource File (resources.qrc).

Next, we check if our engine correctly loaded the QML file by checking that the engine's rootObjects() list is not empty. We can then run our application with app.exec() .