QIviSimulationEngine Class

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

Header: #include <QIviSimulationEngine>
qmake: QT += ivicore
Inherits: QQmlApplicationEngine

Public Functions

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 qtivi_convertFromJSON(const QVariant &value)

Macros

QIVI_SIMULATION_TRY_CALL(instance_type, function, ret_type, ...)
QIVI_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 IVI Simulation System.

Compared to a normal QQmlEngine, the QIviSimulationEngine 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 QIviSimulationEngine 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:

QIviSimulationEngine 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 2.0
import Test 1.0

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 2.0
import Test 1.0

MyClass {
    onCurrentTemperatureChanged: print(currentTemperature)
}

The slot is called once the myClass variable is updated:

QIviSimulationEngine 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)
{
    QIVI_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 2.0
import Test 1.0

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 2.0
import Test 1.0

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 QIviSimulationEngine 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:

QTIVI_SIMULATION_OVERRIDE=<identifier>=<file>[;<identifier>=<file>]
QTIVI_SIMULATION_DATA_OVERRIDE=<identifier>=<file>[;<identifier>=<file>]

Member Function Documentation

void QIviSimulationEngine::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:

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

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

void QIviSimulationEngine::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 IviSimulator global object where it can be accessed from QML. This file can be overridden at runtime using the following environment variable:

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

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

See also IviSimulator.

void QIviSimulationEngine::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 QIviSimulationEngine instance. Using it from another QIviSimulationEngine or a QQmlEngine won't work and produces an error.

See also qmlRegisterType.

Related Non-Members

QVariant qtivi_convertFromJSON(const QVariant &value)

Converts value from JSON to valid C++ types.

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

Macro Documentation

QIVI_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 QIVI_SIMULATION_TRY_CALL_FUNC and Forward Calls from the Instance to the Engine.

QIVI_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.

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

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

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