将QML模块与插件一起使用

本文介绍了如何在QML中创建和使用QML模块,包括创建自定义组件,配置qmldir和qmltypes文件,以及利用C++插件将组件暴露给QML应用程序。同时,讨论了QtCreator如何自动处理缺少qmltypes文件的情况,以及如何通过设置QML_IMPORT_PATH导入模块。
摘要由CSDN通过智能技术生成

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 example importPaths: [ "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 the designer directory. Use a metainfo file delivered with Qt, such as qtquickcontrols2.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项目构建的所有目标。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值