[][src]Function qt_qml::qml_register_singleton_type

pub unsafe fn qml_register_singleton_type(
    url: impl CastInto<Ref<QUrl>>,
    uri: *const c_char,
    version_major: c_int,
    version_minor: c_int,
    qml_name: *const c_char
) -> c_int

This function may be used to register a singleton type with the name qmlName, in the library imported from uri having the version number composed from versionMajor and versionMinor. The type is defined by the QML file located at url. The url must be an absolute URL, i.e. url.isRelative() == false.

Calls C++ function: int qmlRegisterSingletonType(const QUrl& url, const char* uri, int versionMajor, int versionMinor, const char* qmlName).

C++ documentation:

This function may be used to register a singleton type with the name qmlName, in the library imported from uri having the version number composed from versionMajor and versionMinor. The type is defined by the QML file located at url. The url must be an absolute URL, i.e. url.isRelative() == false.

In addition the type's QML file must have pragma Singleton statement among its import statements.

A singleton type may be referenced via the type name with which it was registered, and this typename may be used as the target in a Connections type or otherwise used as any other type id would. One exception to this is that a singleton type property may not be aliased (because the singleton type name does not identify an object within the same component as any other item).

Usage:

// First, define your QML singleton type which provides the functionality. pragma Singleton import QtQuick 2.0 Item { property int testProp1: 125 }

// Second, register the QML singleton type by calling this function in an initialization function. qmlRegisterSingletonType(QUrl("file:///absolute/path/SingletonType.qml"), "Qt.example.qobjectSingleton", 1, 0, "RegisteredSingleton");

In order to use the registered singleton type in QML, you must import the singleton type.

import QtQuick 2.0 import Qt.example.qobjectSingleton 1.0 Item { id: root property int someValue: RegisteredSingleton.testProp1 }

It is also possible to have QML singleton types registered without using the qmlRegisterSingletonType function. That can be done by adding a pragma Singleton statement among the imports of the type's QML file. In addition the type must be defined in a qmldir file with a singleton keyword and the qmldir must be imported by the QML files using the singleton.