当今设备和 OS (操作系统) 的许多特征可能对隐私、安全性、及性能有显著影响,若滥用。因此,要求用户明确同意的平台越来越普遍,在访问这些特征之前。
The Qt QML 核心 module exposes the Qt C++ 应用程序权限 functionality to QML via a set of permission types that can be used to check or request permission in a cross platform manner.
| Access to the user's Bluetooth peripherals | |
| Access to the user's calendar | |
| 访问用户摄像机 | |
| Access to the user's contacts | |
| Access to the user's location | |
| Access to the user's microphone |
To check and request a specific permission in your application, include an instance of the appropriate permission type, and set any of its properties if needed:
CalendarPermission { id: calendarPermission accessMode: CalendarPermission.ReadWrite }
The type can be used to check the current state of the permissions, for example to drive a state based UI:
states: [
State {
name: "waitingForPermission"
when: calendarPermission.status == Qt.PermissionStatus.Undetermined
PropertyChanges { target: permissionRequestItem; visible: true }
},
State {
name: "permissionDenied"
when: calendarPermission.status == Qt.PermissionStatus.Denied
PropertyChanges { target: permissionDeniedItem; visible: true }
}
]
In the example above, two permission specific items will be overlaid if the permission status is not granted. The request UI could perhaps look like:
Rectangle { id: permissionRequestItem anchors.fill: parent visible: false Text { anchors.centerIn: parent text: qsTr("We need your permission to access the calendar." + "Please tap this screen to request permission.") } MouseArea { anchors.fill: parent onClicked: calendarPermission.request() } }
With a corresponding denied UI:
Rectangle { id: permissionDeniedItem anchors.fill: parent color: "red" visible: false Text { anchors.centerIn: parent text: qsTr("We need your permission to access the calendar," + "but permission was not granted. Please resolve.") } }
The properties of a permission can be changed, even after a request has been initiated by calling `request()`. This will update the status, if it changes a result of the new property values, but will not result in an automatic request using the new set of properties.
For example, if upgrading an already granted calendar permission for access mode
Qt.CalendarPermission.ReadOnly
to
Qt.CalendarPermission.ReadWrite
, the platform will respond in one of three ways:
Denied
, for example if permissions can not be upgraded once initially requested.
All these states should then move the application's UI into the appropriate state, where the user is informed of the new state, with the possibility of requesting the new permission if possible, or reverting to a less extensive permission.
Although the permission state is ultimately tied to the underlying application, each permission item reports its own status independently of all other items, and needs to be independently requested if needed.
For example, requesting calendar access for one item will not update the status of another CalendarPermission item, even if these have the exact same properties.