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(const T &type)
Qt::PermissionStatus status() const
QMetaType type() const
std::optional<T> value() const

详细说明

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

qApp->checkPermission(QCameraPermission{});

请求权限时,给定的函数将传递一个 QPermission 的实例,该实例可用于检查请求的结果:

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

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

QLocationPermissionlocationPermission; 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

访问麦克风,用于监听或录制声音

另请参阅 应用程序权限

成员函数文档

template <typename T, QPermission::if_permission<T> = true> QPermission::QPermission(const T &type)

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

您无需明确构建该类型,因为在检查或请求权限时会自动使用该类型。

约束

只有当Ttyped permission 类之一时,才会参与重载解析:

QBluetoothPermission

访问蓝牙外设

QCalendarPermission

访问用户日历

QCameraPermission

访问用于拍照或录像的摄像头

QContactsPermission

访问用户的联系人

QLocationPermission

访问用户位置

QMicrophonePermission

访问麦克风,用于监听或录制声音

Qt::PermissionStatus QPermission::status() const

返回权限的状态。

QMetaType QPermission::type() const

返回权限类型。

template <typename T, QPermission::if_permission<T> = true> std::optional<T> QPermission::value() const

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

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

限制条件

只有当Ttyped permission 类之一时,才会参与重载解析:

QBluetoothPermission

访问蓝牙外设

QCalendarPermission

访问用户日历

QCameraPermission

访问拍照或录像的摄像头

QContactsPermission

访问用户的联系人

QLocationPermission

访问用户位置

QMicrophonePermission

访问麦克风,用于监听或录制声音

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