QQmlEngineExtensionPlugin 类

The QQmlEngineExtensionPlugin class provides an abstract base for custom QML extension plugins. 更多...

头: #include <QQmlEngineExtensionPlugin>
CMake: find_package(Qt6 REQUIRED COMPONENTS Qml)
target_link_libraries(mytarget PRIVATE Qt6::Qml)
qmake: QT += qml
继承: QObject

公共函数

QQmlEngineExtensionPlugin (QObject * parent = nullptr)

重实现公共函数

virtual void initializeEngine (QQmlEngine * engine , const char * uri ) override

(从 6.2 起) Q_IMPORT_QML_PLUGIN ( PluginName )

详细描述

QQmlEngineExtensionPlugin is a plugin interface that lets you create QML extensions that can be loaded dynamically into QML applications. These extensions allow custom QML types to be made available to the QML engine.

To write a QML extension plugin:

  1. 子类 QQmlEngineExtensionPlugin 和使用 Q_PLUGIN_METADATA () macro to register the plugin with the Qt meta object system.
  2. 使用 QML_ELEMENT and QML_NAMED_ELEMENT () macros to declare QML types.
  3. Configure your build file.

    CMake:

    qt_add_qml_module(<target>
        URI <my.import.name>
        VERSION 1.0
        QML_FILES <app.qml>
        NO_RESOURCE_TARGET_PATH
    )
    							

    qmake:

    CONFIG += qmltypes
    QML_IMPORT_NAME = <my.import.name>
    QML_IMPORT_MAJOR_VERSION = <version>
    							
  4. If you're using qmake, create a qmldir 文件 to describe the plugin. Note that CMake will, by default, automatically generate the qmldir 文件 .

QML extension plugins are for either application-specific or library-like plugins. Library plugins should limit themselves to registering types, as any manipulation of the engine's root context may cause conflicts or other issues in the library user's code.

注意: When using the CMake qt_add_qml_module API, a plugin will be generated automatically for you. It will take care of type registration. You only need to write a custom plugin if you have special requirements, such as registering custom image providers. In that case, pass NO_GENERATE_PLUGIN_SOURCE qt_add_qml_module call to disable the generation of the default plugin.

The linker might erroneously remove the generated type registration function as an optimization. You can prevent that by declaring a synthetic volatile pointer to the function somewhere in your code. If your module is called "my.module", you would add the forward declaration in global scope:

void qml_register_types_my_module();
					

Then add the following snippet of code in the implementation of any function that's part of the same binary as the registration:

volatile auto registration = &qml_register_types_my_module;
Q_UNUSED(registration);
					

The 采用 C++ 编写 QML 扩展 tutorial also contains a chapter on creating QML plugins.

另请参阅 QQmlEngine::importPlugin () 和 如何创建 Qt 插件 .

成员函数文档编制

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

Constructs a QML extension plugin with the given parent .

Note that this constructor is invoked automatically by the Q_PLUGIN_METADATA () macro, so there is no need for calling it explicitly.

[override virtual] void QQmlEngineExtensionPlugin:: initializeEngine ( QQmlEngine * engine , const char * uri )

Initializes the extension from the uri 使用 engine . Here an application plugin might, for example, expose some data or objects to QML, as context properties on the engine's root context.

宏文档编制

[since 6.2] Q_IMPORT_QML_PLUGIN ( PluginName )

Ensures the plugin whose metadata-declaring plugin extension class is named PluginName is linked into static builds. For the modules created using qt_add_qml_module , the default plugin extension class name is computed from the QML module URI by replacing dots with underscores, unless the CLASS_NAME argument is specified.

例如:

qt_add_qml_module(myplugin
    # The plugin extension class name in this case is my_Company_QmlComponents.
    URI my.Company.QmlComponents
    ...
)
					

This macro was introduced in Qt 6.2.

另请参阅 Q_IMPORT_PLUGIN .