本页面

qt_add_harmonyos_permission

向目标可执行文件添加一个 HarmonyOS 权限。

该命令定义在Qt6 包的Core 组件中,可按如下方式加载:

find_package(Qt6 REQUIRED COMPONENTS Core)

该命令于 Qt 6.12 版本中引入。

注意:该 命令目前处于技术预览阶段,未来版本中可能会发生变化。

注意: 仅当目标平台为 HarmonyOS 时才应调用此 命令。

语法概述

qt_add_harmonyos_permission(target
    NAME <permission-name>
    [REASON <reason>]
    [USED_SCENE_ABILITIES <ability> [<ability>...]]
    [USED_SCENE_WHEN inuse | always]
)

如果禁用了无版本号的命令,请改用 `qt6_add_harmonyos_permission() `。它支持与该命令相同的参数集。

描述

该命令向target 可执行文件添加一个HarmonyOS权限。在构建HAP包时,该权限会被写入生成的module.json5 清单文件中的requestPermissions 数组中。使用此命令可在应用程序链接的Qt模块提供的默认权限基础上,请求额外的权限。

有关可用权限名称及其要求的列表,请参阅 HarmonyOS 应用程序权限文档。

参数

NAME 是权限标识符,例如ohos.permission.CAMERA 。这是唯一必需的参数。

REASON 是系统提示用户请求user_grant 权限时显示的消息。它接受字面量字符串或$string:<name> 资源引用,该引用将解析为应用程序资源文件中的本地化字符串。指定REASON 会导致为该权限生成一个usedScene 代码块。

USED_SCENE_ABILITIES 是使用该权限的能力列表。如果在生成usedScene 代码块时省略此参数,则默认值为QAbility ,即托管Qt应用程序的唯一能力。

USED_SCENE_WHEN 用于设置访问该权限的时机。有效值为inuse (仅当应用程序处于前台时使用该权限)和always (该权限在后台时同样被使用)。如果在发出usedScene 代码块时省略此参数,则默认为inuse 。

USED_SCENE_ABILITIES 和USED_SCENE_WHEN 关键字直接映射到生成的module.json5 文件中权限的usedScene 对象的abilities 和when 字段。

示例

qt_add_harmonyos_permission(myapp
    NAME ohos.permission.CAMERA
    REASON "$string:camera_permission_reason"
    USED_SCENE_WHEN inuse
)
qt_add_harmonyos_permission(myapp
    NAME ohos.permission.INTERNET
)

另请参阅 Qt for HarmonyOS CMake API。

© 2026 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.