Using QML Modules with Plugins
将QML模块与插件一起使用
QML modules may use C++ plugins to expose components defined in C++ to QML applications.
QML模块可以使用C++插件将C++中定义的组件公开给QML应用程序。
To create a QML module:
要创建QML模块,请执行以下操作:
- Create custom components and place all the
.qml
files in a directory dedicated to your module. For example:imports\asset_imports
. - 创建自定义零部件并放置所有.qml文件位于模块专用目录中。例如:
imports\asset_imports
。 - For Qt Quick UI projects (.qmlproject), specify the path to the directory that contains the module in the .qmlproject file of the application where you want to use the module as a value of the
importPaths
variable. For exampleimportPaths: [ "imports", "asset_imports" ]
. - 对于Qt Quick UI项目(.qmlproject),请指定中包含模块的目录的路径。要将模块用作ImportPath变量值的应用程序的qmlproject文件。例如ImportPath:[“imports”,“
asset_imports
”]。 - Create a
qmldir
file for your module and place it in the module directory. For more information, see Module Definition qmldir Files. - 为模块创建qmldir文件并将其放置在模块目录中。有关更多信息,请参阅模块定义qmldir文件。
- Create a
qmltypes
file, as instructed in Generating Type Description Files. - 按照生成类型描述文件中的说明,创建qmltypes文件。
- Create a directory named
designer
in your module directory. - 在模块目录中创建名为designer的目录。
-
Create a
.metainfo
file for your module and place it in thedesigner
directory. Use a metainfo file delivered with Qt, such asqtquickcontrols2.metainfo
, as an example. -
创建模块的metainfo文件,并将其放置在designer目录中。使用Qt附带的metainfo文件,例如qtquickcontrols2.metainfo。
- Import the module into the project, as instructed in Importing QML Modules.
- 按照导入QML模块中的说明,将模块导入到项目中。
Generating Type Description Files
生成类型描述文件
When registering QML types, make sure that the QML module has a plugins.qmltypes
file. Ideally, it should be located in the same directory as the qmldir
file. The qmltypes
file contains a description of the components exported by the module's plugins and is loaded by Qt Creator when the module is imported.
注册QML类型时,请确保QML模块具有plugins.qmltypes
文件。理想情况下,它应该与qmldir文件位于同一目录中。qmltypes文件包含模块插件导出的组件的描述,并在导入模块时由Qt Creator加载。
For more information, see Type Description Files.
有关详细信息,请参见类型描述文件。
Dumping Plugins Automatically
自动转储插件
If a module with plugins lacks the qmltypes
file, Qt Creator tries to generate a temporary file itself by running the qmldump
program in the background. However, this automatic dumping is a fallback mechanism with many points of failure and you cannot rely upon it.
如果带有插件的模块缺少qmltypes文件,Qt Creator会尝试在后台运行qmldump程序来生成临时文件。然而,这种自动转储是一种具有多个故障点的回退机制,您不能依赖它。
Importing QML Modules
导入QML模块
By default, Qt Creator will look in the QML import path of Qt for QML modules.
默认情况下,Qt Creator将在Qt的QML导入路径中查找QML模块。
If you use qmake and your application adds additional import paths that Qt Creator should use, specify them using QML_IMPORT_PATH
in the .pro
file of your application: QML_IMPORT_PATH += path/to/module
.
如果使用qmake,并且应用程序添加了Qt Creator应该使用的其他导入路径,请在应用程序的pro文件中,使用QML_IMPORT_PATH
指定这些路径:QML_IMPORT_PATH += path/to/module
。
If you use CMake, add the following command to the CMakeLists.txt file to set the QML import path:
如果使用CMake,请将以下命令添加到CMakeListstxt文件,以设置QML导入路径:
set(QML_IMPORT_PATH ${CMAKE_SOURCE_DIR}/qml ${CMAKE_BINARY_DIR}/imports CACHE STRING "" FORCE)
The import path affects all the targets built by the CMake project.
导入路径会影响CMake项目构建的所有目标。