Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
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 \section1 Usage
23
24 To check and request a specific permission in your application,
25 include an instance of the appropriate permission type, and set
26 any of its properties if needed:
27
28 \qml
29 CalendarPermission {
30 id: calendarPermission
31 accessMode: CalendarPermission.ReadWrite
32 }
33 \endqml
34
35 The type can be used to check the current state of the
36 permissions, for example to drive a state based UI:
37
38 \qml
39 states: [
40 State {
41 name: "waitingForPermission"
42 when: calendarPermission.status == Qt.PermissionStatus.Undetermined
43 PropertyChanges { target: permissionRequestItem; visible: true }
44 },
45 State {
46 name: "permissionDenied"
47 when: calendarPermission.status == Qt.PermissionStatus.Denied
48 PropertyChanges { target: permissionDeniedItem; visible: true }
49 }
50 ]
51 \endqml
52
53 In the example above, two permission specific items will be overlaid if the
54 permission status is not granted. The request UI could perhaps look like:
55
56 \qml
57 Rectangle {
58 id: permissionRequestItem
59 anchors.fill: parent
60 visible: false
61
62 Text {
63 anchors.centerIn: parent
64 text: qsTr("We need your permission to access the calendar."
65 + "Please tap this screen to request permission.")
66
67 }
68
69 MouseArea {
70 anchors.fill: parent
71 onClicked: calendarPermission.request()
72 }
73 }
74 \endqml
75
76 With a corresponding denied UI:
77
78 \qml
79 Rectangle {
80 id: permissionDeniedItem
81 anchors.fill: parent
82 color: "red"
83 visible: false
84 Text {
85 anchors.centerIn: parent
86 text: qsTr("We need your permission to access the calendar,"
87 + "but permission was not granted. Please resolve.")
88 }
89 }
90 \endqml
91
92 \section2 Changing permission properties
93
94 The properties of a permission can be changed, even after
95 a request has been initiated by calling `request()`. This
96 will update the status, if it changes a result of the new
97 property values, but will not result in an automatic request
98 using the new set of properties.
99
100 For example, if upgrading an already granted calendar permission
101 for access mode \c Qt.CalendarPermission.ReadOnly to
102 \c Qt.CalendarPermission.ReadWrite, the platform will
103 respond in one of three ways:
104
105 \list
106 \li By implicitly granting the extended permission, for example
107 because the platform doesn't distinguish between the two
108 access modes, which will result in no state change.
109 \li By moving the status back to \ Undetermined, so that
110 the user can be consulted again for access to the now
111 extended permission.
112 \li By moving the status to \c Denied, for example if permissions
113 can not be upgraded once initially requested.
114 \endlist
115
116 All these states should then move the application's UI into
117 the appropriate state, where the user is informed of the
118 new state, with the possibility of requesting the new
119 permission if possible, or reverting to a less extensive
120 permission.
121
122 \section2 Interaction between permission items
123
124 Although the permission state is ultimately tied to the
125 underlying application, each permission item reports its own
126 status independently of all other items, and needs to be
127 independently requested if needed.
128
129 For example, requesting calendar access for one item will
130 not update the status of another CalendarPermission item,
131 even if these have the exact same properties.
132*/
133
134/*!
135 \include qmlpermissions.qdocinc {type-docs} {CalendarPermission} {QCalendarPermission} {calendar}
136 \since 6.6
137*/
138/*!
139 \include qmlpermissions.qdocinc {status-docs} {CalendarPermission}
140*/
141/*!
142 \include qmlpermissions.qdocinc {request-docs} {CalendarPermission}
143*/
144/*!
145 \include qmlpermissions.qdocinc {type-docs} {ContactsPermission} {QContactsPermission} {contacts}
146 \since 6.6
147*/
148/*!
149 \include qmlpermissions.qdocinc {status-docs} {ContactsPermission}
150*/
151/*!
152 \include qmlpermissions.qdocinc {request-docs} {ContactsPermission}
153*/
154/*!
155 \include qmlpermissions.qdocinc {type-docs} {CameraPermission} {QCameraPermission} {camera}
156 \since 6.6
157*/
158/*!
159 \include qmlpermissions.qdocinc {status-docs} {CameraPermission}
160*/
161/*!
162 \include qmlpermissions.qdocinc {request-docs} {CameraPermission}
163*/
164/*!
165 \include qmlpermissions.qdocinc {type-docs} {MicrophonePermission} {QMicrophonePermission} {microphone}
166 \since 6.6
167*/
168/*!
169 \include qmlpermissions.qdocinc {status-docs} {MicrophonePermission}
170*/
171/*!
172 \include qmlpermissions.qdocinc {request-docs} {MicrophonePermission}
173*/
174/*!
175 \include qmlpermissions.qdocinc {type-docs} {BluetoothPermission} {QBluetoothPermission} {Bluetooth peripherals}
176 \since 6.6
177*/
178/*!
179 \include qmlpermissions.qdocinc {status-docs} {BluetoothPermission}
180*/
181/*!
182 \include qmlpermissions.qdocinc {request-docs} {BluetoothPermission}
183*/
184/*!
185 \include qmlpermissions.qdocinc {type-docs} {LocationPermission} {QLocationPermission} {location}
186 \since 6.6
187*/
188/*!
189 \include qmlpermissions.qdocinc {status-docs} {LocationPermission}
190*/
191/*!
192 \include qmlpermissions.qdocinc {request-docs} {LocationPermission}
193*/