Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qmlpermissions.qdoc
Go to the documentation of this file.
1// Copyright (C) 2023 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GFDL-1.3-no-invariants-only
3
4/*!
5 \page qml-application-permissions.html
6 \title QML Application Permissions
7 \brief Managing application permissions via QML
8
9 Many features of today's devices and operating systems can have
10 significant privacy, security, and performance implications if
11 misused. It's therefore increasingly common for platforms to
12 require explicit consent from the user before accessing these
13 features.
14
15 The \l{Qt Qml Core} module exposes the Qt C++
16 \l [QtCore] {Application Permissions} functionality to QML via a set of
17 permission types that can be used to check or request permission in a cross
18 platform manner.
19
20 \annotatedlist qml-permissions
21
22 \note The available permission types cover core functionality of Qt modules
23 like Qt Multimedia and Qt Positioning, but do not encompass all platform-specific
24 permissions. Custom permission types are not currently supported.
25
26 \section1 Usage
27
28 To check and request a specific permission in your application,
29 include an instance of the appropriate permission type, and set
30 any of its properties if needed:
31
32 \qml
33 CalendarPermission {
34 id: calendarPermission
35 accessMode: CalendarPermission.ReadWrite
36 }
37 \endqml
38
39 The type can be used to check the current state of the
40 permissions, for example to drive a state based UI:
41
42 \qml
43 states: [
44 State {
45 name: "waitingForPermission"
46 when: calendarPermission.status == Qt.PermissionStatus.Undetermined
47 PropertyChanges { target: permissionRequestItem; visible: true }
48 },
49 State {
50 name: "permissionDenied"
51 when: calendarPermission.status == Qt.PermissionStatus.Denied
52 PropertyChanges { target: permissionDeniedItem; visible: true }
53 }
54 ]
55 \endqml
56
57 In the example above, two permission specific items will be overlaid if the
58 permission status is not granted. The request UI could perhaps look like:
59
60 \qml
61 Rectangle {
62 id: permissionRequestItem
63 anchors.fill: parent
64 visible: false
65
66 Text {
67 anchors.centerIn: parent
68 text: qsTr("We need your permission to access the calendar."
69 + "Please tap this screen to request permission.")
70
71 }
72
73 MouseArea {
74 anchors.fill: parent
75 onClicked: calendarPermission.request()
76 }
77 }
78 \endqml
79
80 With a corresponding denied UI:
81
82 \qml
83 Rectangle {
84 id: permissionDeniedItem
85 anchors.fill: parent
86 color: "red"
87 visible: false
88 Text {
89 anchors.centerIn: parent
90 text: qsTr("We need your permission to access the calendar,"
91 + "but permission was not granted. Please resolve.")
92 }
93 }
94 \endqml
95
96 \section2 Changing permission properties
97
98 The properties of a permission can be changed, even after
99 a request has been initiated by calling `request()`. This
100 will update the status, if it changes a result of the new
101 property values, but will not result in an automatic request
102 using the new set of properties.
103
104 For example, if upgrading an already granted calendar permission
105 for access mode \c Qt.CalendarPermission.ReadOnly to
106 \c Qt.CalendarPermission.ReadWrite, the platform will
107 respond in one of three ways:
108
109 \list
110 \li By implicitly granting the extended permission, for example
111 because the platform doesn't distinguish between the two
112 access modes, which will result in no state change.
113 \li By moving the status back to \ Undetermined, so that
114 the user can be consulted again for access to the now
115 extended permission.
116 \li By moving the status to \c Denied, for example if permissions
117 can not be upgraded once initially requested.
118 \endlist
119
120 All these states should then move the application's UI into
121 the appropriate state, where the user is informed of the
122 new state, with the possibility of requesting the new
123 permission if possible, or reverting to a less extensive
124 permission.
125
126 \section2 Interaction between permission items
127
128 Although the permission state is ultimately tied to the
129 underlying application, each permission item reports its own
130 status independently of all other items, and needs to be
131 independently requested if needed.
132
133 For example, requesting calendar access for one item will
134 not update the status of another CalendarPermission item,
135 even if these have the exact same properties.
136*/
137
138/*!
139 \include qmlpermissions.qdocinc {type-docs} {CalendarPermission} {QCalendarPermission} {calendar}
140 \since 6.6
141*/
142/*!
143 \include qmlpermissions.qdocinc {status-docs} {CalendarPermission}
144*/
145/*!
146 \include qmlpermissions.qdocinc {request-docs} {CalendarPermission}
147*/
148/*!
149 \include qmlpermissions.qdocinc {type-docs} {ContactsPermission} {QContactsPermission} {contacts}
150 \since 6.6
151*/
152/*!
153 \include qmlpermissions.qdocinc {status-docs} {ContactsPermission}
154*/
155/*!
156 \include qmlpermissions.qdocinc {request-docs} {ContactsPermission}
157*/
158/*!
159 \include qmlpermissions.qdocinc {type-docs} {CameraPermission} {QCameraPermission} {camera}
160 \since 6.6
161*/
162/*!
163 \include qmlpermissions.qdocinc {status-docs} {CameraPermission}
164*/
165/*!
166 \include qmlpermissions.qdocinc {request-docs} {CameraPermission}
167*/
168/*!
169 \include qmlpermissions.qdocinc {type-docs} {MicrophonePermission} {QMicrophonePermission} {microphone}
170 \since 6.6
171*/
172/*!
173 \include qmlpermissions.qdocinc {status-docs} {MicrophonePermission}
174*/
175/*!
176 \include qmlpermissions.qdocinc {request-docs} {MicrophonePermission}
177*/
178/*!
179 \include qmlpermissions.qdocinc {type-docs} {BluetoothPermission} {QBluetoothPermission} {Bluetooth peripherals}
180 \since 6.6
181*/
182/*!
183 \include qmlpermissions.qdocinc {status-docs} {BluetoothPermission}
184*/
185/*!
186 \include qmlpermissions.qdocinc {request-docs} {BluetoothPermission}
187*/
188/*!
189 \include qmlpermissions.qdocinc {type-docs} {LocationPermission} {QLocationPermission} {location}
190 \since 6.6
191*/
192/*!
193 \include qmlpermissions.qdocinc {status-docs} {LocationPermission}
194*/
195/*!
196 \include qmlpermissions.qdocinc {request-docs} {LocationPermission}
197*/