QIfSimulationEngine Class

Provides a way to script a simulation backend from QML. More...

Header: #include <QIfSimulationEngine>
qmake: QT += interfaceframework
Inherits: QQmlApplicationEngine

Public Functions

QIfSimulationEngine(QObject *parent = nullptr)
QIfSimulationEngine(const QString &identifier, QObject *parent = nullptr)
void loadSimulation(const QUrl &file)
void loadSimulationData(const QString &dataFile)
void registerSimulationInstance(T *instance, const char *uri, int versionMajor, int versionMinor, const char *qmlName)
QVariant qtif_convertFromJSON(const QVariant &value)

Macros

QIF_SIMULATION_TRY_CALL(instance_type, function, ret_type, ...)
QIF_SIMULATION_TRY_CALL_FUNC(instance_type, function, ret_func, ...)

Detailed Description

This class is an extended QQmlApplicationEngine which can be used to load QML files. It is made especially for simulation backends to script its behavior from QML. For an overview of its functionality, see Qt Interface Framework Simulation System.

Compared to a normal QQmlEngine, the QIfSimulationEngine provides an extra template function called registerSimulationInstance(). Use this function to register a class instance as a QML type. Within a QML file, this QML type can be used to define the behavior for function calls, update properties, or emit signals.

Register an Instance

You can register any instance of a class derived from QObject to the QIfSimulationEngine by calling registerSimulationInstance(). Similar to qmlRegisterTypes, the URI, version, and name provided are used to import the type from within QML.

class MyClass : public QObject
{
    Q_OBJECT
    Q_PROPERTY(int currentTemperature READ currentTemperature WRITE setCurrentTemperature NOTIFY currentTemperatureChanged)

    ...
}

An instance of this simple class can be registered like this:

QIfSimulationEngine engine;
MyClass myClass;
engine.registerSimulationInstance<MyClass>(&myClass, "Test", 1, 0, "MyClass");
engine.loadSimulation("simulation.qml")

The registered instance has the same constraints as any other C++ classes exposed to QML and needs to use Q_PROPERTY, Q_INVOKABLE, or slots to make the functionality available to QML.

Use the Type from QML

Once an instance is registered to the engine, the type can be used like any other QML element in a declarative form:

import QtQuick
import Test

Item {
    MyClass {
        id: myClass

        Component.onCompleted: currentTemperature = 10;
    }

    Timer {
        running: true
        repeat: true
        interval: 1000
        onTriggered: myClass.currentTemperature++;
    }
}

This QML file initializes the currentTemperature of myClass with a value of 10 and increases it every second.

In the same way, values can be updated from the C++ side and the QML side can react to the change. For example, the following QML snippet prints the currentTemperature whenever it changes:

import QtQuick
import Test

MyClass {
    onCurrentTemperatureChanged: print(currentTemperature)
}

The slot is called once the myClass variable is updated:

QIfSimulationEngine engine;
MyClass myClass;
engine.registerSimulationInstance<MyClass>(&myClass, "Test", 1, 0, "MyClass");
engine.loadSimulation("simulation.qml")
...
myClass.setCurrentTemperature(100);

Forward Calls from the Instance to the Engine

You can also provide the behavior for invokable functions in QML as well, but this use case requires you to extend the exposed class. For example, by adding the following line to the setCurrentTemperature setter:

void MyClass::setCurrentTemperature(int currentTemperature)
{
    QIF_SIMULATION_TRY_CALL(MyClass, "setCurrentTemperature", void, currentTemperature);

    if (m_currentTemperature == currentTemperature)
        return;
    m_currentTemperature = currentTemperature;
    emit currentTemperatureChanged(m_currentTemperature);
}

Calling setCurrentTemperature() now tries to forward the call to the QML instance, if a function matching the signature is defined in QML. When successful, setCurrentTemperature() uses its returned value and avoids running the original C++ function.

By using the following QML snippet, the C++ setter is skipped and only an error is emitted on the console:

import QtQuick
import Test

MyClass {
    function setCurrentTemperature(temperature) {
        print("Updating the temperature is not possible")
    }
}

Reuse Existing Behavior in the Instance

Replacing the C++ functionality with a QML behavior is not always desired. However, it's also possible to call the original C++ behavior from QML. In this case, the original C++ function needs to be a Q_INVOKABLE or a slot. Additionally, the functionality works in a similar manner to function overriding in C++, where the functionality of the overridden function can be accessed by calling <BaseClass>::<function>. In the exposed QML type this is possible by calling the function in the Base object.

import QtQuick
import Test

MyClass {
    function setCurrentTemperature(temperature) {
        print("Updating the temperature: " + temperature )
        Base.setCurrentTemperature(temperature)
    }
}

This QML snippet overrides the setCurrentTemperature() behavior in QML and prints a debug message for the new value. The original C++ behavior is called using Base.setCurrentTemperature(temperature).

Multiple QML Instances

The registered instance is exposed as a normal QML type. This makes it possible to have multiple declarations in QML, and in turn, have multiple QML instances linked to the same C++ instance. Updating and reacting to property changes and signal emissions is possible in all instances, but should be used with care as this can result in property update loops and other issues.

Forwarding C++ function calls to QML is limited. Every call is forwarded to only one QML instance as the return value is used from this call. If multiple QML instances define the same method, the C++ call is always forwarded to the first registered QML instance.

Runtime Override

Each QIfSimulationEngine can take an extra identifier which can be used to override the simulation QML file or the simulation data file at runtime. The environment variables need to be in the following format:

QTIF_SIMULATION_OVERRIDE=<identifier>=<file>[;<identifier>=<file>]
QTIF_SIMULATION_DATA_OVERRIDE=<identifier>=<file>[;<identifier>=<file>]

Member Function Documentation

[explicit] QIfSimulationEngine::QIfSimulationEngine(QObject *parent = nullptr)

Constructs a new QIfSimulationEngine with the given parent.

[explicit] QIfSimulationEngine::QIfSimulationEngine(const QString &identifier, QObject *parent = nullptr)

Constructs a new QIfSimulationEngine with the given identifier and parent.

The identifier can be used to override the simulation QML file or the simulation data file.

See also Runtime Override.

void QIfSimulationEngine::loadSimulation(const QUrl &file)

Loads the QML file as the simulation behavior.

In addition to QQmlApplicationEngine::load(), this function provides functionality to change the simulation file used via an environment variable in the following format:

QTIF_SIMULATION_OVERRIDE=<identifier>=<file>[;<identifier>=<file>]

The simulation engine's identifier can be set in its constructor.

Note: Since 6.8 all root objects are reparented automatically to the engine.

void QIfSimulationEngine::loadSimulationData(const QString &dataFile)

Loads the simulation data file provided as dataFile.

The given file must be in JSON format and is parsed here for errors before it's passed to the IfSimulator global object where it can be accessed from QML. This file can be overridden at runtime using the following environment variable:

QTIF_SIMULATION_DATA_OVERRIDE=<identifier>=<file>[;<identifier>=<file>]

The simulation engine's identifier can be set in its constructor.

See also IfSimulator.

template <typename T> void QIfSimulationEngine::registerSimulationInstance(T *instance, const char *uri, int versionMajor, int versionMinor, const char *qmlName)

Registers the specified instance in the QML system with the name qmlName, in the library imported from uri, with a version number composed from versionMajor and versionMinor.

Note: The registered instance is only available to this QIfSimulationEngine instance. Using it from another QIfSimulationEngine or a QQmlEngine won't work and produces an error.

See also qmlRegisterType.

Related Non-Members

QVariant qtif_convertFromJSON(const QVariant &value)

Converts value from JSON to valid C++ types.

The provided JSON value needs to follow the IfSimulator Data Format.

Macro Documentation

QIF_SIMULATION_TRY_CALL(instance_type, function, ret_type, ...)

Tries to call function in the QML instances registered for the instance_type. The variadic arguments are passed as arguments to the function in QML.

If the call is successful, a return value of ret_type is returned and all code after this macro won't run.

See also QIF_SIMULATION_TRY_CALL_FUNC and Forward Calls from the Instance to the Engine.

QIF_SIMULATION_TRY_CALL_FUNC(instance_type, function, ret_func, ...)

Tries to call function in the QML instances registered for the instance_type. The variadic arguments are passed as arguments to the function in QML.

If the call is successful, the code passed via ret_func is run. This can be useful for situations when the return value needs to be converted first. The original return value is available as return_value.

QIF_SIMULATION_TRY_CALL_FUNC(MyClass, "contactList", return return_value.toStringList());

See also QIF_SIMULATION_TRY_CALL and Forward Calls from the Instance to the Engine.

© 2024 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.