Using QML_ELEMENT with CMake

There is an example of using QML_ELEMENT in qt5\qtdeclarative\examples\quick\scenegraph\openglunderqml and qt_add_qml_module in CMake:

qt_add_qml_module(openglunderqml
    URI OpenGLUnderQML
    VERSION 1.0
    QML_FILES main.qml
    RESOURCE_PREFIX /scenegraph/openglunderqml
    NO_RESOURCE_TARGET_PATH
)

In QML it imports the module:

import OpenGLUnderQML 1.0

Also there is

qt_add_library(qmltextballoon)
qt_add_qml_module(qmltextballoon
    VERSION 1.0
    URI "TextBalloon"
    PLUGIN_TARGET qmltextballoon
    SOURCES
        textballoon.cpp textballoon.h
)

in qt5\qtdeclarative\examples\quick\customitems\painteditem\TextBalloon

Another example is:

qt_add_qml_module(canvasexample
    URI canvas
    VERSION 1.0
    QML_FILES
        "LabeledSlider.qml"
        "bezierCurve/bezierCurve.qml"
        "canvas.qml"
        "clip/clip.qml"
        "quadraticCurveTo/quadraticCurveTo.qml"
        "roundedrect/roundedrect.qml"
        "smile/smile.qml"
        "squircle/squircle.qml"
        "tiger/tiger.js"
        "tiger/tiger.qml"
    RESOURCES
        "contents/qt-logo.png"
        "squircle/squircle.png"
)

I added this to my CMakeLists.txt:

qt_add_qml_module(${PROJECT_NAME}
    URI ${PROJECT_NAME}
    VERSION 1.0
)

and it started to generate a file like this:

/****************************************************************************
** Generated QML type registration code
**
** WARNING! All changes made in this file will be lost!
*****************************************************************************/

#include <QtQml/qqml.h>
#include <QtQml/qqmlmoduleregistration.h>

#include <ExchangeModel.h>
#include <LogFilter.h>
#include <MarketFilter.h>
#include <OrderFilter.h>
#include <PriceChart.h>

#if !defined(QT_STATIC)
#define Q_QMLTYPE_EXPORT Q_DECL_EXPORT
#else
#define Q_QMLTYPE_EXPORT
#endif

Q_QMLTYPE_EXPORT void qml_register_types_TradeClient()
{
    qmlRegisterTypesAndRevisions<qtil::LogFilter>("TradeClient", 1);
    qmlRegisterTypesAndRevisions<tradeclient::ExchangeModel>("TradeClient", 1);
    qmlRegisterTypesAndRevisions<tradeclient::MarketFilter>("TradeClient", 1);
    qmlRegisterTypesAndRevisions<tradeclient::OrderFilter>("TradeClient", 1);
    qmlRegisterTypesAndRevisions<tradeclient::PriceChart>("TradeClient", 1);
    qmlRegisterModule("TradeClient", 1, 0);
}

static const QQmlModuleRegistration registration("TradeClient", qml_register_types_TradeClient);

the only problem is that the headers should be included with subdirectories:

#include <ExchangeModel.h>
#include <Qtil/LogFilter.h>
#include <MarketFilter.h>
#include <OrderFilter.h>
#include <Charts/PriceChart.h>

Investigating QT sources

qt_add_qml_module is probably implemented in the following source files:

qt-everywhere-src-6.2.2\qtdeclarative\src\qml\Qt6QmlBuildInternals.cmake
qt-everywhere-src-6.2.2\qtdeclarative\src\qml\Qt6QmlMacros.cmake
qt-everywhere-src-6.2.2\qtdeclarative\src\qmltyperegistrar\qmltyperegistrar.cpp

My project build generates json files:

moc_inappproduct.cpp
moc_inappproduct.cpp.d
moc_inappproduct.cpp.json
moc_inapppurchasebackend.cpp
moc_inapppurchasebackend.cpp.d
moc_inapppurchasebackend.cpp.json
moc_inappstore.cpp
moc_inappstore.cpp.d
moc_inappstore.cpp.json
moc_inapptransaction.cpp
moc_inapptransaction.cpp.d
moc_inapptransaction.cpp.json

that have inputFile without the header filename without the path:

{
    "classes": [
        {
            "className": "InAppProduct",
            "enums": [
                {
                    "isClass": false,
                    "isFlag": false,
                    "name": "ProductType",
                    "values": [
                        "Consumable",
                        "Unlockable"
                    ]
                }
            ],
            "methods": [
                {
                    "access": "public",
                    "name": "purchase",
                    "returnType": "void"
                }
            ],
            "object": true,
            "properties": [
                {
                    "constant": true,
                    "designable": true,
                    "final": false,
                    "index": 0,
                    "name": "identifier",
                    "read": "identifier",
                    "required": false,
                    "scriptable": true,
                    "stored": true,
                    "type": "QString",
                    "user": false
                },
                {
                    "constant": true,
                    "designable": true,
                    "final": false,
                    "index": 1,
                    "name": "productType",
                    "read": "productType",
                    "required": false,
                    "scriptable": true,
                    "stored": true,
                    "type": "ProductType",
                    "user": false
                },
                {
                    "constant": true,
                    "designable": true,
                    "final": false,
                    "index": 2,
                    "name": "price",
                    "read": "price",
                    "required": false,
                    "scriptable": true,
                    "stored": true,
                    "type": "QString",
                    "user": false
                },
                {
                    "constant": true,
                    "designable": true,
                    "final": false,
                    "index": 3,
                    "name": "title",
                    "read": "title",
                    "required": false,
                    "scriptable": true,
                    "stored": true,
                    "type": "QString",
                    "user": false
                },
                {
                    "constant": true,
                    "designable": true,
                    "final": false,
                    "index": 4,
                    "name": "description",
                    "read": "description",
                    "required": false,
                    "scriptable": true,
                    "stored": true,
                    "type": "QString",
                    "user": false
                }
            ],
            "qualifiedClassName": "InAppProduct",
            "superClasses": [
                {
                    "access": "public",
                    "name": "QObject"
                }
            ]
        }
    ],
    "inputFile": "inappproduct.h",
    "outputRevision": 68
}

Links:

Leave a Reply

Your email address will not be published. Required fields are marked *