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>

Links:

Leave a Reply

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