Qt for HarmonyOS/qtohosextras doc/Request Permission Example: Difference between revisions
Fix example page conversion (QTFOROH-2307) |
Add code snippets to example pages (QTFOROH-2307) |
||
| Line 8: | Line 8: | ||
A launcher button opens a new main window with one row per supported permission. In each row, one button requests the permission through the QOhosAbilityContext::requestPermissionFromUserIfNeeded() enum overload and the other through the string overload with the full OHOS permission name, for example <code>ohos.permission.READ_PASTEBOARD</code>. The per-window context is obtained with QOhosAbilityContext::getInstanceForMainWindow() after the window has been shown. Results arrive through the permissionRequestResponseReceived() and namedPermissionRequestResponseReceived() signals and are displayed with the <code>permissionGranted</code> and <code>dialogShown</code> flags of AppPermissions::PermissionResult. | A launcher button opens a new main window with one row per supported permission. In each row, one button requests the permission through the QOhosAbilityContext::requestPermissionFromUserIfNeeded() enum overload and the other through the string overload with the full OHOS permission name, for example <code>ohos.permission.READ_PASTEBOARD</code>. The per-window context is obtained with QOhosAbilityContext::getInstanceForMainWindow() after the window has been shown. Results arrive through the permissionRequestResponseReceived() and namedPermissionRequestResponseReceived() signals and are displayed with the <code>permissionGranted</code> and <code>dialogShown</code> flags of AppPermissions::PermissionResult. | ||
Requesting a permission through the enum overload: | |||
<syntaxhighlight lang="cpp"> | |||
[window, supportedPermission]() { | |||
auto abilityContext = tryGetAbilityContext(window); | |||
if (abilityContext != nullptr) { | |||
abilityContext->requestPermissionFromUserIfNeeded( | |||
supportedPermission.permission); | |||
} | |||
})); | |||
</syntaxhighlight> | |||
Requesting a permission by its full OHOS name: | |||
<syntaxhighlight lang="cpp"> | |||
[window, supportedPermission]() { | |||
auto abilityContext = tryGetAbilityContext(window); | |||
if (abilityContext != nullptr) { | |||
abilityContext->requestPermissionFromUserIfNeeded( | |||
supportedPermission.namedPermission); | |||
} | |||
})); | |||
</syntaxhighlight> | |||
Receiving the asynchronous response for enum based requests: | |||
<syntaxhighlight lang="cpp"> | |||
auto abilityContext = QtOhosExtras::QOhosAbilityContext::getInstanceForMainWindow(newWindow->windowHandle()); | |||
QObject::connect( | |||
abilityContext.data(), | |||
&QtOhosExtras::QOhosAbilityContext::permissionRequestResponseReceived, | |||
newWindow, | |||
[newWindow]( | |||
QtOhosExtras::AppPermissions::Permission permission, | |||
QtOhosExtras::AppPermissions::PermissionResult result) { | |||
showAppPermissionResponseDialog( | |||
newWindow, QLatin1String("Enum based"), | |||
readableNameForPermission(permission), | |||
result); | |||
}); | |||
</syntaxhighlight> | |||
'''Note: '''getInstanceForMainWindow() requires a valid [https://doc.qt.io/qt-5/qwindow.html QWindow], so the window must be shown before the context is requested. | '''Note: '''getInstanceForMainWindow() requires a valid [https://doc.qt.io/qt-5/qwindow.html QWindow], so the window must be shown before the context is requested. | ||
Revision as of 10:15, 16 September 2026
< Qt for HarmonyOS / qtohosextras doc
Request Permission Example
Demonstrates requesting application permissions from the user, per main window, by enum and by name.
A launcher button opens a new main window with one row per supported permission. In each row, one button requests the permission through the QOhosAbilityContext::requestPermissionFromUserIfNeeded() enum overload and the other through the string overload with the full OHOS permission name, for example ohos.permission.READ_PASTEBOARD. The per-window context is obtained with QOhosAbilityContext::getInstanceForMainWindow() after the window has been shown. Results arrive through the permissionRequestResponseReceived() and namedPermissionRequestResponseReceived() signals and are displayed with the permissionGranted and dialogShown flags of AppPermissions::PermissionResult.
Requesting a permission through the enum overload:
[window, supportedPermission]() {
auto abilityContext = tryGetAbilityContext(window);
if (abilityContext != nullptr) {
abilityContext->requestPermissionFromUserIfNeeded(
supportedPermission.permission);
}
}));
Requesting a permission by its full OHOS name:
[window, supportedPermission]() {
auto abilityContext = tryGetAbilityContext(window);
if (abilityContext != nullptr) {
abilityContext->requestPermissionFromUserIfNeeded(
supportedPermission.namedPermission);
}
}));
Receiving the asynchronous response for enum based requests:
auto abilityContext = QtOhosExtras::QOhosAbilityContext::getInstanceForMainWindow(newWindow->windowHandle());
QObject::connect(
abilityContext.data(),
&QtOhosExtras::QOhosAbilityContext::permissionRequestResponseReceived,
newWindow,
[newWindow](
QtOhosExtras::AppPermissions::Permission permission,
QtOhosExtras::AppPermissions::PermissionResult result) {
showAppPermissionResponseDialog(
newWindow, QLatin1String("Enum based"),
readableNameForPermission(permission),
result);
});
Note: getInstanceForMainWindow() requires a valid QWindow, so the window must be shown before the context is requested.