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.h
和fileio.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_OBJECT
和Q_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_ELEMENT
和QML_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的两个模块将相互覆盖。