Investigar o main.cpp

Compreender o ficheiro central do nosso código de infra-estrutura

O papel do main.cpp

Embora o QML seja usado para a interface das aplicações do Kirigami, a infra-estrutura normalmente é feita em C++, graças à velocidade e flexibilidade desta linguagem. Embora nas páginas anteriores tenhamos feito uma análise profunda do QML, teremos de ganhar alguma compreensão do nosso código de C++ da infra-estrutura, para criar aplicações que são mais úteis que as que conseguimos com QML puro.

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.

O que faz

 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
32
33
34
35
#include <QApplication>
#include <QQmlApplicationEngine>
#include <QtQml>
#include <QUrl>
#include <QQuickStyle>
#include <KLocalizedContext>
#include <KLocalizedString>
#include <KIconTheme>

int main(int argc, char *argv[])
{
    KIconTheme::initTheme();
    QApplication app(argc, argv);
    KLocalizedString::setApplicationDomain("tutorial");
    QApplication::setOrganizationName(QStringLiteral("KDE"));
    QApplication::setOrganizationDomain(QStringLiteral("kde.org"));
    QApplication::setApplicationName(QStringLiteral("Kirigami Tutorial"));
    QApplication::setDesktopFileName(QStringLiteral("org.kde.tutorial"));

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

    QQmlApplicationEngine engine;

    engine.rootContext()->setContextObject(new KLocalizedContext(&engine));
    engine.loadFromModule("org.kde.tutorial", "Main");

    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, and to allow us to read QML files.

We then create a QApplication instance which we call app. Passing argc and argv to the constructor is required for the call.

Também definimos alguns meta-dados relacionados com a aplicação. Estes incluem a organização que criou a aplicação, a página Web da organização e o nome da aplicação. Definimos estas propriedades no QApplication , um objecto que herda do QCoreApplication e que fornece o ciclo de eventos das aplicações, independentemente se têm uma interface gráfica ou não (por isso, se executarmos o nosso programa sem essa interface, estes meta-dados ficarão definidos à mesma).

To make our app look good with KDE's Breeze icons and Breeze style on non-Plasma environments such as Windows or GNOME, we need to do three things:

The call to KIconTheme::initTheme() needs to be done before creating the QApplication and lets the app find Breeze icons to use. Setting the QStyle to Breeze is needed because we used QApplication for our app instead of QGuiApplication . Actual interface controls in the window like buttons and checkboxes will follow Breeze by using qqc2-desktop-style.

The QQmlApplicationEngine lets us load an application from a QML file, which we do in the next line. In engine.loadFromModule("org.kde.tutorial", "Main"); we load our QML from the URI import defined in CMake.

De seguida iremos verificar se o nosso motor carregou correctamente o ficheiro QML, verificando se a lista rootObjects() do motor não está vazia. Nesse caso, poderemos executar a nossa aplicação com o app.exec() .