欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

QT Quick 入门一

程序员文章站 2022-03-09 16:25:02
...


创建QT Quick程序后的初始代码

代码解析


#include <QGuiApplication>
#include <QQmlApplicationEngine>

int main(int argc, char *argv[])
{
    QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
    QGuiApplication app(argc, argv);
    QQmlApplicationEngine engine;
    const QUrl url(QStringLiteral("qrc:/main.qml"));

    QObject::connect(&engine, &QQmlApplicationEngine::objectCreated,
                     &app, [url](QObject *obj, const QUrl &objUrl) {
        if (!obj && url == objUrl)
            QCoreApplication::exit(-1);
    }, Qt::QueuedConnection);
    engine.load(url);

    return app.exec();
}

QObject

QT关键类,需要深入理解

QCoreApplication Class

Inherit from: QObject
The QCoreApplication class provides an event loop for Qt applications without UI.
This class is used by non-GUI applications to provide their event loop. For non-GUI application that uses Qt, there should be exactly one QCoreApplication object.

AA_EnableHighDpiScaling

Enables high-DPI scaling in Qt on supported platforms (see also High DPI Displays). Supported platforms are X11, Windows and Android.

QGuiApplication class

Inherit from: QCoreApplication
The QGuiApplication class manages the GUI application’s control flow and main settings.
QGuiApplication contains the main event loop, where all events from the window system and other sources are processed and dispatched. It also handles the application’s initialization and finalization, and provides session management. In addition, QGuiApplication handles most of the system-wide and application-wide settings.
For any GUI application using Qt, there is precisely one QGuiApplication object no matter whether the application has 0, 1, 2 or more windows at any given time. For non-GUI Qt applications, use QCoreApplication instead, as it does not depend on the Qt GUI module. For QWidget based Qt applications, use QApplication instead, as it provides some functionality needed for creating QWidget instances.
The QGuiApplication object is accessible through the instance() function, which returns a pointer equivalent to the global qApp pointer.
QGuiApplication’s main areas of responsibility are:
It initializes the application with the user’s desktop settings, such as palette(), font() and styleHints(). It keeps track of these properties in case the user changes the desktop globally, for example, through some kind of control panel.
It performs event handling, meaning that it receives events from the underlying window system and dispatches them to the relevant widgets. You can send your own events to windows by using sendEvent() and postEvent().
It parses common command line arguments and sets its internal state accordingly. See the constructor documentation below for more details.
It provides localization of strings that are visible to the user via translate().
It provides some magical objects like the clipboard().
It knows about the application’s windows. You can ask which window is at a certain position using topLevelAt(), get a list of topLevelWindows(), etc.
It manages the application’s mouse cursor handling, see setOverrideCursor()
It provides support for sophisticated session management. This makes it possible for applications to terminate gracefully when the user logs out, to cancel a shutdown process if termination isn’t possible and even to preserve the entire application’s state for a future session. See isSessionRestored(), sessionId() and commitDataRequest() and saveStateRequest() for details.
Since the QGuiApplication object does so much initialization, it must be created before any other objects related to the user interface are created. QGuiApplication also deals with common command line arguments. Hence, it is usually a good idea to create it before any interpretation or modification of argv is done in the application itself.

QQmlApplicationEngine Class

Inherits: QQmlEngine
QQmlApplicationEngine provides a convenient way to load an application from a single QML file.
This class combines a QQmlEngine and QQmlComponent to provide a convenient way to load a single QML file. It also exposes some central application functionality to QML, which a C++/QML hybrid application would normally control from C++.

QObject::connect

QMetaObject::Connection QObject::connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type = Qt::AutoConnection)

Creates a connection of the given type from the signal in the sender object to the method in the receiver object. Returns a handle to the connection that can be used to disconnect it later.
You must use the SIGNAL() and SLOT() macros when specifying the signal and the method, for example:

  QLabel *label = new QLabel;
  QScrollBar *scrollBar = new QScrollBar;
  QObject::connect(scrollBar, SIGNAL(valueChanged(int)),
                   label,  SLOT(setNum(int)));

QQmlApplicationEngine::objectCreated

This signal is emitted when an object finishes loading. If loading was successful, object contains a pointer to the loaded object, otherwise the pointer is NULL.
The url to the component the object came from is also provided.
Note: If the path to the component was provided as a QString containing a relative path, the url will contain a fully resolved path to the file.

Qt::QueuedConnection

The slot is invoked when control returns to the event loop of the receiver’s thread. The slot is executed in the receiver’s thread.

相关标签: QT Quick