Qt6 QML Book/扩展QML/创建插件

Creating the plugin

创建插件

Qt Creator contains a wizard to create a QtQuick 2 QML Extension Plugin, found under Library when creating a new project. We use it to create a plugin called fileio with a FileIO object to start within the module org.example.io.

Qt Creator包含创建QtQuick 2 QML扩展插件的向导,可在创建新项目时在库下找到。我们使用它来创建一个名为fileio的插件,该插件带有一个FileIO对象,可以在模块org.example.io启用。

TIP

The current wizard generates a QMake based project. Please use the example from this chapter as a starting point to build a CMake based project.

当前向导将生成基于QMake的项目。请使用本章中的示例作为构建基于CMake的项目的起点。

The project should consist of the fileio.h and fileio.cpp, that declare and implement the FileIO type, and a fileio_plugin.cpp that contains the actual plugin class that allows the QML engine to discover out extension.

项目应该由fileio.hfileio.cpp组成,声明并实现FileIO类型,以及fileio_plugin.cpp。包含允许QML引擎发现扩展的实际插件类。

The plugin class is derived from the QQmlEngineExtensionPlugin class, and contains a the Q_OBJECT and Q_PLUGIN_METADATA macros. The entire file can be seen below.

插件类派生自QQmlEngineExtensionPlugin类,包含Q_OBJECTQ_PLUGIN_METADATA宏。整个文件可以在下面看到。

#include <QQmlEngineExtensionPlugin>

class FileioPlugin : public QQmlEngineExtensionPlugin
{
    Q_OBJECT
    Q_PLUGIN_METADATA(IID QQmlEngineExtensionInterface_iid)
};

#include "fileio_plugin.moc"

The extension will automatically discover and register all types marked with QML_ELEMENT and QML_NAMED_ELEMENT. We will see how this is done in the FileIO Implementation section below.

扩展将自动发现并注册所有标记有QML_ELEMENTQML_NAMED_ELEMENT的类型。我们将在下面的FileIO实现部分中看到这是如何实现的。

For the import of the module to work, the user also needs to specify a URI, e.g. import org.example.io. Interestingly we cannot see the module URI anywhere. This is set from the outside using a qmldir file, alternatively in the CMakeLists.txt file of your project.

为了使模块的导入工作,用户还需要指定一个URI,例如import org.example.io。有趣的是,我们在任何地方都看不到模块URI。这是使用qmldir文件从外部设置的,也可以在你项目的CMakeLists.txt文件中设置。

The qmldir file specifies the content of your QML plugin or better the QML side of your plugin. A hand-written qmldir file for our plugin should look something like this:

qmldir文件指定QML插件的内容,或者更好地指定插件的QML端。我们插件的手写qmldir文件应该如下所示:

module org.example.io
plugin fileio

The module is the URI that the user imports, and after it you name which plugin to load for the said URI. The plugin line must be identical with your plugin file name (under mac this would be libfileio_debug.dylib on the file system and fileio in the qmldir, for a Linux system, the same line would look for libfileio.so). These files are created by Qt Creator based on the given information.

模块是用户导入的URI,在它之后,您可以为所述URI命名要加载的插件。插件行必须与插件文件名相同(在mac下,文件系统中的libfileio_debug.dylib和qmldir中的fileio是同一行,在Linux系统中,同一行将查找libfileio.so)。这些文件由Qt Creator根据给定信息创建。

The easier way to create a correct qmldir file is in the CMakeLists.txt for your project, in the qt_add_qml_module macro. Here, the URI parameter is used to specify the URI of the plugin, e.g. org.example.io. This way, the qmldir file is generated when the project is built.

创建正确的qmldir文件的更简单方法是在CMakeLists.txt中。在qt_add_qml_module宏中。在这里,URI参数用于指定插件的URI,例如org.example.io。这样,在构建项目时会生成qmldir文件。

::: TODO How to install the module? :::

::TODO如何安装模块?::

When importing a module called “org.example.io”, the QML engine will look in one of the import paths and tries to locate the “org/example/io” path with a qmldir. The qmldir then will tell the engine which library to load as a QML extension plugin using which module URI. Two modules with the same URI will override each other.

导入名为“org.example.io”的模块时,QML引擎将查找其中一个导入路径,并尝试使用qmldir定位“org/example/io”路径。然后,qmldir将告诉引擎使用哪个模块URI加载哪个库作为QML扩展插件。具有相同URI的两个模块将相互覆盖。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值