本页内容

QPermission Class

类型化权限的不透明封装。更多...

头文件: #include <QPermissions>
CMake: find_package(Qt6 REQUIRED COMPONENTS Core)
target_link_libraries(mytarget PRIVATE Qt6::Core)
qmake: QT += core
自: Qt 6.5

公共函数

QPermission()
QPermission(const T &type)
Qt::PermissionStatus status() const
QMetaType type() const
std::optional<T> value() const

详细说明

QPermission 类是typed permission 的不透明封装,用于检查或请求权限。您无需显式构造此类,因为在检查或请求权限时,系统会自动使用该类型:

qApp->checkPermission(QCameraPermission{});

在请求权限时,给定的 functor 将接收一个 QPermission 实例,可用于检查请求的结果:

qApp->requestPermission(QCameraPermission{}, [](const QPermission &permission) {
    if (permission.status() == Qt::PermissionStatus::Granted)
        takePhoto();
});

要检查原始类型化权限的属性,请使用value()函数:

QLocationPermission locationPermission;
locationPermission.setAccuracy(QLocationPermission::Precise);
qApp->requestPermission(locationPermission, this, &LocationWidget::permissionUpdated);
void LocationWidget::permissionUpdated(const QPermission &permission)
{
    if (permission.status() != Qt::PermissionStatus::Granted)
        return;
    auto locationPermission = permission.value<QLocationPermission>();
    if (!locationPermission || locationPermission->accuracy() != QLocationPermission::Precise)
        return;
    updatePreciseLocation();
}

类型化权限

提供以下权限:

QBluetoothPermission

访问蓝牙外设

QCalendarPermission

访问用户的日历

QCameraPermission

访问相机以拍摄照片或视频

QContactsPermission

访问用户的联系人

QLocationPermission

访问用户的位置

QMicrophonePermission

访问麦克风以监听或录制声音

另请参阅 “应用程序权限”。

成员函数文档

[explicit noexcept default] QPermission::QPermission()

默认构造一个QPermission 的实例。

template <typename T> requires if_permission<T> QPermission::QPermission(const T &type)

根据给定的typed permission type 构建一个权限。

您无需显式创建此类型,因为在检查或请求权限时,系统会自动使用该类型。

约束

仅当 `T ` 是以下 `typed permission ` 类之一时,才参与重载解析:

QBluetoothPermission

访问蓝牙外设

QCalendarPermission

访问用户的日历

QCameraPermission

访问相机以拍摄照片或视频

QContactsPermission

访问用户的联系人

QLocationPermission

访问用户的定位信息

QMicrophonePermission

访问麦克风以监听或录制声音

Qt::PermissionStatus QPermission::status() const

返回权限的状态。

QMetaType QPermission::type() const

返回该权限的类型。

template <typename T> requires if_permission<T> std::optional<T> QPermission::value() const

返回类型为T 的typed permission ,如果该QPermission 对象不包含此类权限,则返回std::nullopt 。

使用type() 来动态选择要请求的类型化权限。

约束

仅当 `T ` 属于以下 `typed permission ` 类之一时,才参与重载解析:

QBluetoothPermission

访问蓝牙外设

QCalendarPermission

访问用户的日历

QCameraPermission

访问相机以拍摄照片或录制视频

QContactsPermission

访问用户的联系人

QLocationPermission

访问用户的位置

QMicrophonePermission

访问麦克风以监听或录制声音

© 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.