On this page

Initializing the QML Runtime

QML documents are loaded and run by the QML runtime. The QML runtime also provides access to other Qt or third-party QML modules and types.

In addition, the Qt Framework provides the qml tool, which loads .qml files. This tool is useful for developing and testing QML code without having to write a C++ application to load the QML runtime.

To run an application that uses QML, your application must invoke the QML runtime. To invoke the QML runtime:

Initializing the QML Runtime in C++ Applications

Initializing with QQuickView

QQuickView is a QWindow-based class that can load QML files. For example, if there is a QML file, application.qml, it will look like this:

import QtQuick

Rectangle { width: 100; height: 100; color: "red" }

It can be loaded in a Qt application's main.cpp file like this:

#include <QGuiApplication>
#include <QQuickView>

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

    QQuickView view;
    view.setSource(QUrl::fromLocalFile("application.qml"));
    view.show();

    return app.exec();
}

This creates a QWindow-based view that displays the contents of application.qml.

Build files

Use the find_package() command to locate the needed module component in the Qt6 package:

find_package(Qt6 REQUIRED COMPONENTS Quick)
target_link_libraries(mytarget PRIVATE Qt6::Quick)

For more details, see the Build with CMake overview.

To configure the module for building with qmake, add the module as a value of the QT variable in the project's .pro file:

QT += quick

For more information, see Creating Project Files.

Creating a QQmlEngine directly

If application.qml doesn't have any graphical components, or if it's preferred to avoid QQuickView for other reasons, the QQmlEngine can be constructed directly instead. In this case, application.qml is loaded as a QQmlComponent instance rather than placed into a view:

#include <QGuiApplication>
#include <QQmlEngine>
#include <QQmlContext>
#include <QQmlComponent>

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

    QQmlEngine engine;
    QQmlContext *objectContext = new QQmlContext(engine.rootContext());

    QQmlComponent component(&engine, "application.qml");
    QObject *object = component.create(objectContext);

    // ... delete object and objectContext when necessary

    return app.exec();
}

If you're not using any graphical items from Qt Quick, you can replace QGuiApplication with a QCoreApplication in the code above. This way, you can use QML as a language without any dependencies to the Qt GUI module.

© 2026 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.