Jump to content

Qt for HarmonyOS/qtohosextras doc/QOhosAppContext Class: Difference between revisions

From Qt Wiki
No edit summary
Drop manual breadcrumb, MediaWiki renders it automatically (QTFOROH-2307)
 
(2 intermediate revisions by 2 users not shown)
Line 1: Line 1:
< [[Qt_for_HarmonyOS/qtohosextras_doc|Qt for HarmonyOS / qtohosextras doc]]
= QOhosAppContext Class =


= QOhosAppContext Class =
class [[Qt_for_HarmonyOS/qtohosextras_doc/Qt_Ohos_Extras_C++_Classes|QtOhosExtras]]::QOhosAppContext


The QOhosAppContext class contains API to manage native application context. More...
The QOhosAppContext class contains API to manage native application context. [[#details|More...]]


{| class="wikitable"
{| class="wikitable"
|-
|-
! Header:
| Header:
| #include <QOhosAppContext>
| #include <[https://doc.qt.io/qt-5/qohosappcontext-sub-qtcore.html QOhosAppContext]>
|-
|-
! qmake:
| qmake:
| QT += ohosextras
| QT += ohosextras
|-
|-
! Since:
| Since:
| Qt 5.12.12
| Qt 5.12.12
|-
|-
! Inherits:
| Inherits:
| [[QObject_Class|QObject]]
| [https://doc.qt.io/qt-5/qobject.html QObject]
|}
|}


* [[List_of_All_Members_for_QOhosAppContext|List of all members, including inherited members]]
This class was introduced in Qt 5.12.12.
* [[Obsolete_Members_for_QOhosAppContext|Obsolete members]]
 
* [[Qt_for_HarmonyOS/qtohosextras_doc/List_of_All_Members_for_QOhosAppContext|List of all members, including inherited members]]


== Public Types ==
== Public Types ==


{| class="wikitable" style="width:100%"
{| class="wikitable"
|-
|-
| enum || '''[[#ColorThemeMode-enum|ColorThemeMode]]''' { LightTheme, DarkTheme, FollowSystemSetting }
| enum class
| '''[[Qt_for_HarmonyOS/qtohosextras_doc/QOhosAppContext_Class#ColorThemeMode-enum|ColorThemeMode]]''' { LightTheme, DarkTheme, FollowSystemSetting }
|}
|}


== Public Functions ==
== Public Functions ==


{| class="wikitable" style="width:100%"
{| class="wikitable"
|-
| virtual bool
| '''[[Qt_for_HarmonyOS/qtohosextras_doc/QOhosAppContext_Class#darkThemeActive|darkThemeActive]]'''() const = 0
|-
| virtual void
| '''[[Qt_for_HarmonyOS/qtohosextras_doc/QOhosAppContext_Class#enableContextMenuEventOnLongPress|enableContextMenuEventOnLongPress]]'''() = 0
|-
|-
| virtual bool || '''[[#darkThemeActive|darkThemeActive]]'''() const = 0
| virtual double
| '''[[Qt_for_HarmonyOS/qtohosextras_doc/QOhosAppContext_Class#fontSizeScale|fontSizeScale]]'''() const = 0
|-
|-
| virtual void || '''[[#enableContextMenuEventOnLongPress|enableContextMenuEventOnLongPress]]'''() = 0
| virtual QSharedPointer<QOhosBundleInfo>
| '''[[Qt_for_HarmonyOS/qtohosextras_doc/QOhosAppContext_Class#getBundleInfo|getBundleInfo]]'''() const = 0
|-
|-
| virtual double || '''[[#fontSizeScale|fontSizeScale]]'''() const = 0
| virtual bool
| '''[[Qt_for_HarmonyOS/qtohosextras_doc/QOhosAppContext_Class#hasSerialPortAccessRight|hasSerialPortAccessRight]]'''(const QString &''portName'') const = 0
|-
|-
| int || '''[[#getBundleInfo|getBundleInfo]]'''() const
| virtual bool
| '''[[Qt_for_HarmonyOS/qtohosextras_doc/QOhosAppContext_Class#isPermissionGranted|isPermissionGranted]]'''(AppPermissions::Permission ''permission'') const = 0
|-
|-
| virtual bool || '''[[#isPermissionGranted|isPermissionGranted]]'''(int ''permission'') const = 0
| virtual bool
| '''[[Qt_for_HarmonyOS/qtohosextras_doc/QOhosAppContext_Class#isPermissionGranted-1|isPermissionGranted]]'''(const QString &''permissionName'') const = 0
|-
|-
| virtual bool || '''[[#isPermissionGranted-1|isPermissionGranted]]'''(const QString &''permissionName'') const = 0
| virtual void
| '''[[Qt_for_HarmonyOS/qtohosextras_doc/QOhosAppContext_Class#requestPermissionFromUserIfNeeded|requestPermissionFromUserIfNeeded]]'''(AppPermissions::Permission ''permission'') = 0
|-
|-
| virtual void || '''[[#requestPermissionFromUserIfNeeded|requestPermissionFromUserIfNeeded]]'''(int ''permission'') = 0
| virtual void
| '''[[Qt_for_HarmonyOS/qtohosextras_doc/QOhosAppContext_Class#requestPermissionOnSettingIfNeeded|requestPermissionOnSettingIfNeeded]]'''(AppPermissions::Permission ''permission'') = 0
|-
|-
| virtual void || '''[[#requestPermissionOnSettingIfNeeded|requestPermissionOnSettingIfNeeded]]'''(int ''permission'') = 0
| virtual void
| '''[[Qt_for_HarmonyOS/qtohosextras_doc/QOhosAppContext_Class#requestSerialPortAccessRightIfNeeded|requestSerialPortAccessRightIfNeeded]]'''(const QString &''portName'') = 0
|-
|-
| virtual void || '''[[#restartApp|restartApp]]'''() = 0
| virtual void
| '''[[Qt_for_HarmonyOS/qtohosextras_doc/QOhosAppContext_Class#restartApp|restartApp]]'''() = 0
|-
|-
| virtual void || '''[[#restartApp-1|restartApp]]'''(const int &''want'') = 0
| virtual void
| '''[[Qt_for_HarmonyOS/qtohosextras_doc/QOhosAppContext_Class#restartApp-1|restartApp]]'''(const QOhosWant &''requestWant'') = 0
|-
|-
| virtual void || '''[[#setColorThemeMode|setColorThemeMode]]'''(QtOhosExtras::QOhosAppContext::ColorThemeMode ''mode'') = 0
| virtual void
| '''[[Qt_for_HarmonyOS/qtohosextras_doc/QOhosAppContext_Class#setColorThemeMode|setColorThemeMode]]'''(ColorThemeMode ''mode'') = 0
|}
|}


== Signals ==
== Signals ==


{| class="wikitable" style="width:100%"
{| class="wikitable"
|-
| void
| '''[[Qt_for_HarmonyOS/qtohosextras_doc/QOhosAppContext_Class#darkThemeActiveChanged|darkThemeActiveChanged]]'''(bool ''darkThemeActive'')
|-
|-
| void || '''[[#darkThemeActiveChanged|darkThemeActiveChanged]]'''(bool ''darkThemeActive'')
| void
| '''[[Qt_for_HarmonyOS/qtohosextras_doc/QOhosAppContext_Class#fontSizeScaleChanged|fontSizeScaleChanged]]'''(double ''fontSizeScale'')
|-
|-
| void || '''[[#fontSizeScaleChanged|fontSizeScaleChanged]]'''(double ''fontSizeScale'')
| void
| '''[[Qt_for_HarmonyOS/qtohosextras_doc/QOhosAppContext_Class#permissionRequestOnSettingResponseReceived|permissionRequestOnSettingResponseReceived]]'''(AppPermissions::Permission ''permission'', bool ''permissionGranted'')
|-
|-
| void || '''[[#permissionRequestOnSettingResponseReceived|permissionRequestOnSettingResponseReceived]]'''(int ''permission'', bool ''permissionGranted'')
| void
| '''[[Qt_for_HarmonyOS/qtohosextras_doc/QOhosAppContext_Class#permissionRequestResponseReceived|permissionRequestResponseReceived]]'''(AppPermissions::Permission ''permission'', bool ''permissionGranted'')
|-
|-
| void || '''[[#permissionRequestResponseReceived|permissionRequestResponseReceived]]'''(int ''permission'', bool ''permissionGranted'')
| void
| '''[[Qt_for_HarmonyOS/qtohosextras_doc/QOhosAppContext_Class#permissionRequestResponseWithResultReceived|permissionRequestResponseWithResultReceived]]'''(AppPermissions::Permission ''permission'', AppPermissions::PermissionResult ''result'')
|-
|-
| void || '''[[#permissionRequestResponseWithResultReceived|permissionRequestResponseWithResultReceived]]'''(int ''permission'', int ''result'')
| void
| '''[[Qt_for_HarmonyOS/qtohosextras_doc/QOhosAppContext_Class#serialPortAccessRightResponseReceived|serialPortAccessRightResponseReceived]]'''(const QString &''portName'', QSharedPointer<QObject> ''serialPortAccessRightContext'')
|}
|}


== Static Public Members ==
== Static Public Members ==


{| class="wikitable" style="width:100%"
{| class="wikitable"
|-
|-
| int || '''[[#getAppLaunchWant|getAppLaunchWant]]'''()
| QOhosWant
| '''[[Qt_for_HarmonyOS/qtohosextras_doc/QOhosAppContext_Class#getAppLaunchWant|getAppLaunchWant]]'''()
|-
|-
| int || '''[[#getAppLaunchWantInfo|getAppLaunchWantInfo]]'''()
| QSharedPointer<QOhosWantInfo>
| '''[[Qt_for_HarmonyOS/qtohosextras_doc/QOhosAppContext_Class#getAppLaunchWantInfo|getAppLaunchWantInfo]]'''()
|-
|-
| QtOhosExtras::QOhosAppContext * || '''[[#instance|instance]]'''()
| QOhosAppContext *
| '''[[Qt_for_HarmonyOS/qtohosextras_doc/QOhosAppContext_Class#instance|instance]]'''()
|-
|-
| bool || '''[[#isNoUiChildMode|isNoUiChildMode]]'''()
| bool
| '''[[Qt_for_HarmonyOS/qtohosextras_doc/QOhosAppContext_Class#isNoUiChildMode|isNoUiChildMode]]'''()
|-
|-
| void || '''[[#startNoUiChildProcess|startNoUiChildProcess]]'''(QString ''libraryName'', QStringList ''args'')
| void
|-
| '''[[Qt_for_HarmonyOS/qtohosextras_doc/QOhosAppContext_Class#startNoUiChildProcess|startNoUiChildProcess]]'''(QString ''libraryName'', QStringList ''args'')
| const QMetaObject || '''staticMetaObject'''
|}
 
== Protected Functions ==
 
{| class="wikitable" style="width:100%"
|-
| || '''[[#QOhosAppContext|QOhosAppContext]]'''()
|-
| || '''[[#QOhosAppContext-1|QOhosAppContext]]'''(const QtOhosExtras::QOhosAppContext &)
|-
| virtual || '''[[#dtor.QOhosAppContext|~QOhosAppContext]]'''() override
|-
| QtOhosExtras::QOhosAppContext & || '''[[#operator-eq|operator=]]'''(const QtOhosExtras::QOhosAppContext &)
|}
|}


== Detailed Description ==
== Detailed Description ==
The QOhosAppContext class contains API to manage native application context.


== Member Type Documentation ==
== Member Type Documentation ==


=== <span id="ColorThemeMode-enum"></span>enum QOhosAppContext::ColorThemeMode ===
=== <span id="ColorThemeMode-enum"></span>enum class QOhosAppContext::ColorThemeMode ===


Defines the color theme mode for the application.
Defines the color theme mode for the application.


{| class="wikitable"
{| class="wikitable"
! Constant !! Value !! Description
|-
|-
| QtOhosExtras::QOhosAppContext::LightTheme || 0 || Uses the light theme colors (default).
! Constant
! Value
! Description
|-
|-
| QtOhosExtras::QOhosAppContext::DarkTheme || 1 || Uses the dark theme colors.
| <code>QtOhosExtras::QOhosAppContext::ColorThemeMode::LightTheme</code>
| <code>0</code>
| Uses the light theme colors (default).
|-
|-
| QtOhosExtras::QOhosAppContext::FollowSystemSetting || 2 || Automatically updates the theme based on the system setting.
| <code>QtOhosExtras::QOhosAppContext::ColorThemeMode::DarkTheme</code>
| <code>1</code>
| Uses the dark theme colors.
|-
| <code>QtOhosExtras::QOhosAppContext::ColorThemeMode::FollowSystemSetting</code>
| <code>2</code>
| Automatically updates the theme based on the system setting. See [https://developer.huawei.com/consumer/en/doc/harmonyos-references-V5/js-apis-app-ability-environmentcallback-V5#environmentcallbackonconfigurationupdated Configuration Updated].
|}
|}
This enum was introduced or modified in Qt 5.12.12.


== Member Function Documentation ==
== Member Function Documentation ==


=== <span id="QOhosAppContext"></span>[protected] QOhosAppContext::QOhosAppContext() ===
=== <span id="darkThemeActiveChanged"></span><code>[signal] </code>void QOhosAppContext::darkThemeActiveChanged(bool ''darkThemeActive'') ===
Default constructs an instance of QOhosAppContext.


=== <span id="dtor.QOhosAppContext"></span>[virtual protected] QOhosAppContext::~QOhosAppContext() ===
Signal emitted when the application's color theme changes between dark and light modes. The ''darkThemeActive'' parameter is <code>true</code> when the dark theme became active.
Destroys the instance of QOhosAppContext.


=== <span id="darkThemeActive"></span>[pure virtual] bool QOhosAppContext::darkThemeActive() const ===
'''See also '''[[Qt_for_HarmonyOS/qtohosextras_doc/QOhosAppContext_Class#darkThemeActive|darkThemeActive]]() and [[Qt_for_HarmonyOS/qtohosextras_doc/QOhosAppContext_Class#setColorThemeMode|setColorThemeMode]]().
Returns whether the dark color theme is active.


=== <span id="enableContextMenuEventOnLongPress"></span>[pure virtual] void QOhosAppContext::enableContextMenuEventOnLongPress() ===
=== <span id="fontSizeScaleChanged"></span><code>[signal] </code>void QOhosAppContext::fontSizeScaleChanged(double ''fontSizeScale'') ===
Enable context menu triggering on long press touch for the entire application lifetime.


=== <span id="fontSizeScale"></span>[pure virtual] double QOhosAppContext::fontSizeScale() const ===
Signal emitted on system configuration update. ''fontSizeScale'' is the system current font size scale. See [https://developer.huawei.com/consumer/en/doc/harmonyos-references-V5/js-apis-app-ability-environmentcallback-V5#environmentcallbackonconfigurationupdated Configuration Updated].
Returns font size scale ratio. The value ranges from 0 to 3.2.


=== <span id="getBundleInfo"></span>int QOhosAppContext::getBundleInfo() const ===
'''See also '''[[Qt_for_HarmonyOS/qtohosextras_doc/QOhosAppContext_Class#fontSizeScale|fontSizeScale]]().
Returns [[QOhosBundleInfo_Class|QOhosBundleInfo]] object for the current application.


=== <span id="isPermissionGranted"></span>[pure virtual] bool QOhosAppContext::isPermissionGranted(int permission) const ===
=== <span id="permissionRequestOnSettingResponseReceived"></span><code>[signal] </code>void QOhosAppContext::permissionRequestOnSettingResponseReceived([[Qt_for_HarmonyOS/qtohosextras_doc/AppPermissions_Namespace#Permission-enum|AppPermissions::Permission]] ''permission'', bool ''permissionGranted'') ===
Returns true if a given ''permission'' is granted.
 
Signal emitted on [[Qt_for_HarmonyOS/qtohosextras_doc/QOhosAppContext_Class#requestPermissionOnSettingIfNeeded|requestPermissionOnSettingIfNeeded]]() request result. Provides ''permissionGranted'' state for a checked ''permission''.
 
'''See also '''[[Qt_for_HarmonyOS/qtohosextras_doc/QOhosAppContext_Class#requestPermissionOnSettingIfNeeded|requestPermissionOnSettingIfNeeded]]().
 
=== <span id="permissionRequestResponseReceived"></span><code>[signal] </code>void QOhosAppContext::permissionRequestResponseReceived([[Qt_for_HarmonyOS/qtohosextras_doc/AppPermissions_Namespace#Permission-enum|AppPermissions::Permission]] ''permission'', bool ''permissionGranted'') ===
 
Signal emitted on [[Qt_for_HarmonyOS/qtohosextras_doc/QOhosAppContext_Class#requestPermissionFromUserIfNeeded|requestPermissionFromUserIfNeeded]]() request result. Provides ''permissionGranted'' state for a checked ''permission''.
 
'''See also '''[[Qt_for_HarmonyOS/qtohosextras_doc/QOhosAppContext_Class#requestPermissionFromUserIfNeeded|requestPermissionFromUserIfNeeded]]().
 
=== <span id="permissionRequestResponseWithResultReceived"></span><code>[signal] </code>void QOhosAppContext::permissionRequestResponseWithResultReceived([[Qt_for_HarmonyOS/qtohosextras_doc/AppPermissions_Namespace#Permission-enum|AppPermissions::Permission]] ''permission'', [[Qt_for_HarmonyOS/qtohosextras_doc/PermissionResult_Class|AppPermissions::PermissionResult]] ''result'') ===
 
Signal emitted on [[Qt_for_HarmonyOS/qtohosextras_doc/QOhosAppContext_Class#requestPermissionFromUserIfNeeded|requestPermissionFromUserIfNeeded]]() request result. Provides ''result'' for a checked ''permission''.
 
'''See also '''[[Qt_for_HarmonyOS/qtohosextras_doc/QOhosAppContext_Class#requestPermissionFromUserIfNeeded|requestPermissionFromUserIfNeeded]]().
 
=== <span id="serialPortAccessRightResponseReceived"></span><code>[signal] </code>void QOhosAppContext::serialPortAccessRightResponseReceived(const [https://doc.qt.io/qt-5/qstring.html QString] &''portName'', [https://doc.qt.io/qt-5/qsharedpointer.html QSharedPointer]<[https://doc.qt.io/qt-5/qobject.html#QObject QObject]> ''serialPortAccessRightContext'') ===
 
Emitted when the result of [[Qt_for_HarmonyOS/qtohosextras_doc/QOhosAppContext_Class#requestSerialPortAccessRightIfNeeded|requestSerialPortAccessRightIfNeeded]](const [https://doc.qt.io/qt-5/qstring.html QString] &portName) becomes available.
 
The signal provides the ''portName'' corresponding to the response. Additionally, it includes a ''serialPortAccessRightContext'' that defines the access rights granted for the requested serial port. The caller must keep this context alive for as long as access to the serial port is required.
 
Destroying or releasing the context automatically cancels the access rights for the associated serial port.
 
If access was not granted, the context will be nullptr.
 
'''Note: '''The lifetime of the access permission is strictly tied to the lifetime of the provided context. Once the last reference to the context is destroyed, the permission request is revoked internally.
 
'''See also '''[[Qt_for_HarmonyOS/qtohosextras_doc/QOhosAppContext_Class#requestSerialPortAccessRightIfNeeded|requestSerialPortAccessRightIfNeeded]](const QString &portName).
 
=== <span id="darkThemeActive"></span><code>[pure virtual] </code>bool QOhosAppContext::darkThemeActive() const ===
 
Returns whether the dark color theme is active. By default, this function returns `false`, meaning the light theme is used.
 
'''See also '''[[Qt_for_HarmonyOS/qtohosextras_doc/QOhosAppContext_Class#darkThemeActiveChanged|darkThemeActiveChanged]]() and [[Qt_for_HarmonyOS/qtohosextras_doc/QOhosAppContext_Class#setColorThemeMode|setColorThemeMode]]().
 
=== <span id="enableContextMenuEventOnLongPress"></span><code>[pure virtual] </code>void QOhosAppContext::enableContextMenuEventOnLongPress() ===
 
Enable context menu triggering on long press touch for the entire application lifetime. Mimic Windows long touch behavior by generating a contextMenuEvent with the [https://doc.qt.io/qt-5/qcontextmenuevent.html#Reason-enum QContextMenuEvent::Other] reason.
 
The function should be called at the application start, before app.exec();.
 
=== <span id="fontSizeScale"></span><code>[pure virtual] </code>double QOhosAppContext::fontSizeScale() const ===
 
Returns font size scale ratio. The value ranges from 0 to 3.2. The default value is 1. See [https://developer.huawei.com/consumer/en/doc/harmonyos-references-V5/js-apis-app-ability-configuration-V5 Font Size Scale].
 
'''See also '''[[Qt_for_HarmonyOS/qtohosextras_doc/QOhosAppContext_Class#fontSizeScaleChanged|fontSizeScaleChanged]](double fontSizeScale).
 
=== <span id="getAppLaunchWant"></span><code>[static] </code>[[Qt_for_HarmonyOS/qtohosextras_doc/QOhosWant_Class|QOhosWant]] QOhosAppContext::getAppLaunchWant() ===
 
Returns the Want object that was used to launch initial instance of the application's QAbility. It is recommended to use newer [[Qt_for_HarmonyOS/qtohosextras_doc/QOhosAppContext_Class#getAppLaunchWantInfo|getAppLaunchWantInfo]]() method.
 
'''See also '''[[Qt_for_HarmonyOS/qtohosextras_doc/QOhosAppContext_Class#getAppLaunchWantInfo|getAppLaunchWantInfo]]().
 
=== <span id="getAppLaunchWantInfo"></span><code>[static] </code>[https://doc.qt.io/qt-5/qsharedpointer.html QSharedPointer]<[[Qt_for_HarmonyOS/qtohosextras_doc/QOhosWantInfo_Class|QOhosWantInfo]]> QOhosAppContext::getAppLaunchWantInfo() ===
 
Returns the Want object that was used to launch initial instance of the application's QAbility.
 
=== <span id="getBundleInfo"></span><code>[pure virtual] </code>[https://doc.qt.io/qt-5/qsharedpointer.html QSharedPointer]<[[Qt_for_HarmonyOS/qtohosextras_doc/QOhosBundleInfo_Class|QOhosBundleInfo]]> QOhosAppContext::getBundleInfo() const ===
 
Returns [[Qt_for_HarmonyOS/qtohosextras_doc/QOhosBundleInfo_Class|QOhosBundleInfo]] object for the current application. The obtained information does not contain information about the signature, HAP module, ability, ExtensionAbility, or permission.
 
=== <span id="hasSerialPortAccessRight"></span><code>[pure virtual] </code>bool QOhosAppContext::hasSerialPortAccessRight(const [https://doc.qt.io/qt-5/qstring.html QString] &''portName'') const ===
 
Checks whether the application currently has permission to access the serial port identified by ''portName''.
 
Returns <code>true</code> if access rights for the specified serial port are currently granted, otherwise returns <code>false</code>.
 
This function performs a synchronous check of the current permission state and does not trigger any permission request. To request access rights when they are not yet granted, use [[Qt_for_HarmonyOS/qtohosextras_doc/QOhosAppContext_Class#requestSerialPortAccessRightIfNeeded|requestSerialPortAccessRightIfNeeded]]().
 
For details about the underlying platform API, see [https://developer.huawei.com/consumer/en/doc/harmonyos-references/js-apis-serialmanager Serial Port Manager].
 
'''See also '''[[Qt_for_HarmonyOS/qtohosextras_doc/QOhosAppContext_Class#requestSerialPortAccessRightIfNeeded|requestSerialPortAccessRightIfNeeded]]().
 
=== <span id="instance"></span><code>[static] </code>QOhosAppContext *QOhosAppContext::instance() ===
 
Gets [[Qt_for_HarmonyOS/qtohosextras_doc/QOhosAppContext_Class|QOhosAppContext]] global instance.
 
=== <span id="isNoUiChildMode"></span><code>[static] </code>bool QOhosAppContext::isNoUiChildMode() ===
 
Returns true if the current process was started as a "No UI" child.
 
=== <span id="isPermissionGranted"></span><code>[pure virtual] </code>bool QOhosAppContext::isPermissionGranted([[Qt_for_HarmonyOS/qtohosextras_doc/AppPermissions_Namespace#Permission-enum|AppPermissions::Permission]] ''permission'') const ===
 
Returns true is a given ''permission'' is granted, false otherwise. See [https://developer.huawei.com/consumer/en/doc/harmonyos-references-V13/js-apis-abilityaccessctrl-V13#checkaccesstoken9 Check Access Token].
 
=== <span id="isPermissionGranted-1"></span><code>[pure virtual] </code>bool QOhosAppContext::isPermissionGranted(const [https://doc.qt.io/qt-5/qstring.html QString] &''permissionName'') const ===
 
Returns true if a permission with a given ''permissionName'' is granted, false otherwise. The ''permissionName'' is a full OHOS permission name, for example "ohos.permission.READ_PASTEBOARD". See [https://developer.huawei.com/consumer/en/doc/harmonyos-references/js-apis-abilityaccessctrl#checkaccesstoken9 Check Access Token].
 
=== <span id="requestPermissionFromUserIfNeeded"></span><code>[pure virtual] </code>void QOhosAppContext::requestPermissionFromUserIfNeeded([[Qt_for_HarmonyOS/qtohosextras_doc/AppPermissions_Namespace#Permission-enum|AppPermissions::Permission]] ''permission'') ===
 
Request a given application ''permission'' from the User. This is asynchronous call which result will be emitted by [[Qt_for_HarmonyOS/qtohosextras_doc/QOhosAppContext_Class#permissionRequestResponseReceived|permissionRequestResponseReceived]]() signal.
 
'''See also '''[[Qt_for_HarmonyOS/qtohosextras_doc/QOhosAppContext_Class#permissionRequestResponseReceived|permissionRequestResponseReceived]]().
 
=== <span id="requestPermissionOnSettingIfNeeded"></span><code>[pure virtual] </code>void QOhosAppContext::requestPermissionOnSettingIfNeeded([[Qt_for_HarmonyOS/qtohosextras_doc/AppPermissions_Namespace#Permission-enum|AppPermissions::Permission]] ''permission'') ===
 
Request a given application ''permission'' in a Settings dialog box. This is asynchronous call which result will be emitted by [[Qt_for_HarmonyOS/qtohosextras_doc/QOhosAppContext_Class#permissionRequestOnSettingResponseReceived|permissionRequestOnSettingResponseReceived]]() signal.
 
'''See also '''[[Qt_for_HarmonyOS/qtohosextras_doc/QOhosAppContext_Class#permissionRequestOnSettingResponseReceived|permissionRequestOnSettingResponseReceived]]().
 
=== <span id="requestSerialPortAccessRightIfNeeded"></span><code>[pure virtual] </code>void QOhosAppContext::requestSerialPortAccessRightIfNeeded(const [https://doc.qt.io/qt-5/qstring.html QString] &''portName'') ===
 
Requests permission for the application to access the serial port identified by ''portName''.
 
This function performs an asynchronous permission request. The result of the request is delivered via the [[Qt_for_HarmonyOS/qtohosextras_doc/QOhosAppContext_Class#serialPortAccessRightResponseReceived|serialPortAccessRightResponseReceived]]() signal.
 
The outcome of the request (granted or denied) is reported asynchronously through [[Qt_for_HarmonyOS/qtohosextras_doc/QOhosAppContext_Class#serialPortAccessRightResponseReceived|serialPortAccessRightResponseReceived]](). If access is granted, the signal provides a context object that must be kept alive for as long as the application requires access to the serial port.
 
If access is not granted, [[Qt_for_HarmonyOS/qtohosextras_doc/QOhosAppContext_Class#serialPortAccessRightResponseReceived|serialPortAccessRightResponseReceived]]() signal delivers nullptr. This may happen if:
 
* a provided ''portName'' cannot be mapped to a valid system serial port,
* an error occurred while requesting the access right,
* user denied the access right.
 
For details about the underlying platform API, see [https://developer.huawei.com/consumer/en/doc/harmonyos-references/js-apis-serialmanager Serial Port Manager].
 
'''See also '''[[Qt_for_HarmonyOS/qtohosextras_doc/QOhosAppContext_Class#serialPortAccessRightResponseReceived|serialPortAccessRightResponseReceived]]().
 
=== <span id="restartApp"></span><code>[pure virtual] </code>void QOhosAppContext::restartApp() ===


=== <span id="restartApp"></span>[pure virtual] void QOhosAppContext::restartApp() ===
Restarts the Application using the app launch want.
Restarts the Application using the app launch want.


=== <span id="setColorThemeMode"></span>[pure virtual] void QOhosAppContext::setColorThemeMode(QtOhosExtras::QOhosAppContext::ColorThemeMode mode) ===
The current application will be killed using SIGKILL and a new instance of the application will be launched with the application start want.
 
All abilities and sub-widnows created within this process will be closed.
 
The application will be killed ungracefully. This function won't return to the caller.
 
The caller must ensure, that the application has system focus when this function is called, otherwise the application will be killed but the new application won't be started. OHOS system treats some system dialogs (for example File Dialog) as separate from the application. If such dialog is open, the application loses the system focus.
 
If restartApp is called too frequently, the system call will be throttled to avoid errors.
 
Use this function, if you want to restart app with app launch want, instead of calling restartApp(const [[Qt_for_HarmonyOS/qtohosextras_doc/QOhosWant_Class|QOhosWant]] &requestWant) with [[Qt_for_HarmonyOS/qtohosextras_doc/QOhosAppContext_Class#getAppLaunchWant|QOhosAppContext::getAppLaunchWant]]() as a parameter.
 
=== <span id="restartApp-1"></span><code>[pure virtual] </code>void QOhosAppContext::restartApp(const [[Qt_for_HarmonyOS/qtohosextras_doc/QOhosWant_Class|QOhosWant]] &''requestWant'') ===
 
Restarts the Application using the ''requestWant''.
 
The current application will be killed using SIGKILL and a new instance of the application will be launched with the ''requestWant''.
 
All abilities and sub-widnows created within this process will be closed.
 
The application will be killed ungracefully. This function won't return to the caller.
 
The caller must ensure, that the application has system focus when this function is called, otherwise the application will be killed but the new application won't be started. OHOS system treats some system dialogs (for example File Dialog) as separate from the application. If such dialog is open, the application loses the system focus.
 
If restartApp is called too frequently, the system call will be throttled to avoid errors.
 
To call this function with the app launch want (see [[Qt_for_HarmonyOS/qtohosextras_doc/QOhosAppContext_Class#getAppLaunchWant|QOhosAppContext::getAppLaunchWant]]()), use restartApp().
 
<syntaxhighlight lang="cpp">
 
QtOhosExtras::QOhosWant requestWant = QtOhosExtras::QOhosAppContext::getAppLaunchWant();
requestWant.parameters["first_parameter"] = "first_parameter_value";
requestWant.parameters["second_parameter"] = "second_parameter_value";
QtOhosExtras::QOhosAppContext::restartApp(requestWant);
 
</syntaxhighlight>
 
=== <span id="setColorThemeMode"></span><code>[pure virtual] </code>void QOhosAppContext::setColorThemeMode([[Qt_for_HarmonyOS/qtohosextras_doc/QOhosAppContext_Class#ColorThemeMode-enum|ColorThemeMode]] ''mode'') ===
 
Sets the application's color theme to fixed value or enables automatic theme updates.
Sets the application's color theme to fixed value or enables automatic theme updates.


=== <span id="startNoUiChildProcess"></span>[static] void QOhosAppContext::startNoUiChildProcess(QString libraryName, QStringList args) ===
Calling this function with ''mode'' `[[Qt_for_HarmonyOS/qtohosextras_doc/QOhosAppContext_Class#ColorThemeMode-enum|FollowSystemSetting]]` allows the application to adjust its theme dynamically based on the system's theme configuration. The theme will be updated to match the system theme when `[[Qt_for_HarmonyOS/qtohosextras_doc/QOhosAppContext_Class#ColorThemeMode-enum|FollowSystemSetting]]` is set. See [https://developer.huawei.com/consumer/en/doc/harmonyos-references-V5/js-apis-app-ability-environmentcallback-V5#environmentcallbackonconfigurationupdated Configuration Updated]. If the color theme actually changes as a result of this call, the [[Qt_for_HarmonyOS/qtohosextras_doc/QOhosAppContext_Class#darkThemeActiveChanged|darkThemeActiveChanged]]() signal will be emitted.
Starts "No UI" child process for a given ''libraryName'' and ''args''.
 
'''See also '''[[Qt_for_HarmonyOS/qtohosextras_doc/QOhosAppContext_Class#darkThemeActive|darkThemeActive]]() and [[Qt_for_HarmonyOS/qtohosextras_doc/QOhosAppContext_Class#darkThemeActiveChanged|darkThemeActiveChanged]]().
 
=== <span id="startNoUiChildProcess"></span><code>[static] </code>void QOhosAppContext::startNoUiChildProcess([https://doc.qt.io/qt-5/qstring.html QString] ''libraryName'', [https://doc.qt.io/qt-5/qstringlist.html QStringList] ''args'') ===
 
Starts "No UI" child process for a given ''libraryName'' and ''args''. Arguments passed to the startNoUiChildProcess() function are forwarded to the child's main() function. See [https://developer.huawei.com/consumer/en/doc/harmonyos-references-V5/js-apis-app-ability-childprocessmanager-V5 Child Process Manager].
 
<syntaxhighlight lang="cpp">
 
QtOhosExtras::QOhosAppContext::startNoUiChildProcess(
    "libapp.so",
    QStringList{
        "first arg",
        "second arg",
    });
 
</syntaxhighlight>


[[Category:Qt for HarmonyOS]]
[[Category:Qt for HarmonyOS]]

Latest revision as of 06:14, 17 September 2026

QOhosAppContext Class

class QtOhosExtras::QOhosAppContext

The QOhosAppContext class contains API to manage native application context. More...

Header: #include <QOhosAppContext>
qmake: QT += ohosextras
Since: Qt 5.12.12
Inherits: QObject

This class was introduced in Qt 5.12.12.

Public Types

enum class ColorThemeMode { LightTheme, DarkTheme, FollowSystemSetting }

Public Functions

virtual bool darkThemeActive() const = 0
virtual void enableContextMenuEventOnLongPress() = 0
virtual double fontSizeScale() const = 0
virtual QSharedPointer<QOhosBundleInfo> getBundleInfo() const = 0
virtual bool hasSerialPortAccessRight(const QString &portName) const = 0
virtual bool isPermissionGranted(AppPermissions::Permission permission) const = 0
virtual bool isPermissionGranted(const QString &permissionName) const = 0
virtual void requestPermissionFromUserIfNeeded(AppPermissions::Permission permission) = 0
virtual void requestPermissionOnSettingIfNeeded(AppPermissions::Permission permission) = 0
virtual void requestSerialPortAccessRightIfNeeded(const QString &portName) = 0
virtual void restartApp() = 0
virtual void restartApp(const QOhosWant &requestWant) = 0
virtual void setColorThemeMode(ColorThemeMode mode) = 0

Signals

void darkThemeActiveChanged(bool darkThemeActive)
void fontSizeScaleChanged(double fontSizeScale)
void permissionRequestOnSettingResponseReceived(AppPermissions::Permission permission, bool permissionGranted)
void permissionRequestResponseReceived(AppPermissions::Permission permission, bool permissionGranted)
void permissionRequestResponseWithResultReceived(AppPermissions::Permission permission, AppPermissions::PermissionResult result)
void serialPortAccessRightResponseReceived(const QString &portName, QSharedPointer<QObject> serialPortAccessRightContext)

Static Public Members

QOhosWant getAppLaunchWant()
QSharedPointer<QOhosWantInfo> getAppLaunchWantInfo()
QOhosAppContext * instance()
bool isNoUiChildMode()
void startNoUiChildProcess(QString libraryName, QStringList args)

Detailed Description

Member Type Documentation

enum class QOhosAppContext::ColorThemeMode

Defines the color theme mode for the application.

Constant Value Description
QtOhosExtras::QOhosAppContext::ColorThemeMode::LightTheme 0 Uses the light theme colors (default).
QtOhosExtras::QOhosAppContext::ColorThemeMode::DarkTheme 1 Uses the dark theme colors.
QtOhosExtras::QOhosAppContext::ColorThemeMode::FollowSystemSetting 2 Automatically updates the theme based on the system setting. See Configuration Updated.

This enum was introduced or modified in Qt 5.12.12.

Member Function Documentation

[signal] void QOhosAppContext::darkThemeActiveChanged(bool darkThemeActive)

Signal emitted when the application's color theme changes between dark and light modes. The darkThemeActive parameter is true when the dark theme became active.

See also darkThemeActive() and setColorThemeMode().

[signal] void QOhosAppContext::fontSizeScaleChanged(double fontSizeScale)

Signal emitted on system configuration update. fontSizeScale is the system current font size scale. See Configuration Updated.

See also fontSizeScale().

[signal] void QOhosAppContext::permissionRequestOnSettingResponseReceived(AppPermissions::Permission permission, bool permissionGranted)

Signal emitted on requestPermissionOnSettingIfNeeded() request result. Provides permissionGranted state for a checked permission.

See also requestPermissionOnSettingIfNeeded().

[signal] void QOhosAppContext::permissionRequestResponseReceived(AppPermissions::Permission permission, bool permissionGranted)

Signal emitted on requestPermissionFromUserIfNeeded() request result. Provides permissionGranted state for a checked permission.

See also requestPermissionFromUserIfNeeded().

[signal] void QOhosAppContext::permissionRequestResponseWithResultReceived(AppPermissions::Permission permission, AppPermissions::PermissionResult result)

Signal emitted on requestPermissionFromUserIfNeeded() request result. Provides result for a checked permission.

See also requestPermissionFromUserIfNeeded().

[signal] void QOhosAppContext::serialPortAccessRightResponseReceived(const QString &portName, QSharedPointer<QObject> serialPortAccessRightContext)

Emitted when the result of requestSerialPortAccessRightIfNeeded(const QString &portName) becomes available.

The signal provides the portName corresponding to the response. Additionally, it includes a serialPortAccessRightContext that defines the access rights granted for the requested serial port. The caller must keep this context alive for as long as access to the serial port is required.

Destroying or releasing the context automatically cancels the access rights for the associated serial port.

If access was not granted, the context will be nullptr.

Note: The lifetime of the access permission is strictly tied to the lifetime of the provided context. Once the last reference to the context is destroyed, the permission request is revoked internally.

See also requestSerialPortAccessRightIfNeeded(const QString &portName).

[pure virtual] bool QOhosAppContext::darkThemeActive() const

Returns whether the dark color theme is active. By default, this function returns `false`, meaning the light theme is used.

See also darkThemeActiveChanged() and setColorThemeMode().

[pure virtual] void QOhosAppContext::enableContextMenuEventOnLongPress()

Enable context menu triggering on long press touch for the entire application lifetime. Mimic Windows long touch behavior by generating a contextMenuEvent with the QContextMenuEvent::Other reason.

The function should be called at the application start, before app.exec();.

[pure virtual] double QOhosAppContext::fontSizeScale() const

Returns font size scale ratio. The value ranges from 0 to 3.2. The default value is 1. See Font Size Scale.

See also fontSizeScaleChanged(double fontSizeScale).

[static] QOhosWant QOhosAppContext::getAppLaunchWant()

Returns the Want object that was used to launch initial instance of the application's QAbility. It is recommended to use newer getAppLaunchWantInfo() method.

See also getAppLaunchWantInfo().

[static] QSharedPointer<QOhosWantInfo> QOhosAppContext::getAppLaunchWantInfo()

Returns the Want object that was used to launch initial instance of the application's QAbility.

[pure virtual] QSharedPointer<QOhosBundleInfo> QOhosAppContext::getBundleInfo() const

Returns QOhosBundleInfo object for the current application. The obtained information does not contain information about the signature, HAP module, ability, ExtensionAbility, or permission.

[pure virtual] bool QOhosAppContext::hasSerialPortAccessRight(const QString &portName) const

Checks whether the application currently has permission to access the serial port identified by portName.

Returns true if access rights for the specified serial port are currently granted, otherwise returns false.

This function performs a synchronous check of the current permission state and does not trigger any permission request. To request access rights when they are not yet granted, use requestSerialPortAccessRightIfNeeded().

For details about the underlying platform API, see Serial Port Manager.

See also requestSerialPortAccessRightIfNeeded().

[static] QOhosAppContext *QOhosAppContext::instance()

Gets QOhosAppContext global instance.

[static] bool QOhosAppContext::isNoUiChildMode()

Returns true if the current process was started as a "No UI" child.

[pure virtual] bool QOhosAppContext::isPermissionGranted(AppPermissions::Permission permission) const

Returns true is a given permission is granted, false otherwise. See Check Access Token.

[pure virtual] bool QOhosAppContext::isPermissionGranted(const QString &permissionName) const

Returns true if a permission with a given permissionName is granted, false otherwise. The permissionName is a full OHOS permission name, for example "ohos.permission.READ_PASTEBOARD". See Check Access Token.

[pure virtual] void QOhosAppContext::requestPermissionFromUserIfNeeded(AppPermissions::Permission permission)

Request a given application permission from the User. This is asynchronous call which result will be emitted by permissionRequestResponseReceived() signal.

See also permissionRequestResponseReceived().

[pure virtual] void QOhosAppContext::requestPermissionOnSettingIfNeeded(AppPermissions::Permission permission)

Request a given application permission in a Settings dialog box. This is asynchronous call which result will be emitted by permissionRequestOnSettingResponseReceived() signal.

See also permissionRequestOnSettingResponseReceived().

[pure virtual] void QOhosAppContext::requestSerialPortAccessRightIfNeeded(const QString &portName)

Requests permission for the application to access the serial port identified by portName.

This function performs an asynchronous permission request. The result of the request is delivered via the serialPortAccessRightResponseReceived() signal.

The outcome of the request (granted or denied) is reported asynchronously through serialPortAccessRightResponseReceived(). If access is granted, the signal provides a context object that must be kept alive for as long as the application requires access to the serial port.

If access is not granted, serialPortAccessRightResponseReceived() signal delivers nullptr. This may happen if:

  • a provided portName cannot be mapped to a valid system serial port,
  • an error occurred while requesting the access right,
  • user denied the access right.

For details about the underlying platform API, see Serial Port Manager.

See also serialPortAccessRightResponseReceived().

[pure virtual] void QOhosAppContext::restartApp()

Restarts the Application using the app launch want.

The current application will be killed using SIGKILL and a new instance of the application will be launched with the application start want.

All abilities and sub-widnows created within this process will be closed.

The application will be killed ungracefully. This function won't return to the caller.

The caller must ensure, that the application has system focus when this function is called, otherwise the application will be killed but the new application won't be started. OHOS system treats some system dialogs (for example File Dialog) as separate from the application. If such dialog is open, the application loses the system focus.

If restartApp is called too frequently, the system call will be throttled to avoid errors.

Use this function, if you want to restart app with app launch want, instead of calling restartApp(const QOhosWant &requestWant) with QOhosAppContext::getAppLaunchWant() as a parameter.

[pure virtual] void QOhosAppContext::restartApp(const QOhosWant &requestWant)

Restarts the Application using the requestWant.

The current application will be killed using SIGKILL and a new instance of the application will be launched with the requestWant.

All abilities and sub-widnows created within this process will be closed.

The application will be killed ungracefully. This function won't return to the caller.

The caller must ensure, that the application has system focus when this function is called, otherwise the application will be killed but the new application won't be started. OHOS system treats some system dialogs (for example File Dialog) as separate from the application. If such dialog is open, the application loses the system focus.

If restartApp is called too frequently, the system call will be throttled to avoid errors.

To call this function with the app launch want (see QOhosAppContext::getAppLaunchWant()), use restartApp().

 QtOhosExtras::QOhosWant requestWant = QtOhosExtras::QOhosAppContext::getAppLaunchWant();
 requestWant.parameters["first_parameter"] = "first_parameter_value";
 requestWant.parameters["second_parameter"] = "second_parameter_value";
 QtOhosExtras::QOhosAppContext::restartApp(requestWant);

[pure virtual] void QOhosAppContext::setColorThemeMode(ColorThemeMode mode)

Sets the application's color theme to fixed value or enables automatic theme updates.

Calling this function with mode `FollowSystemSetting` allows the application to adjust its theme dynamically based on the system's theme configuration. The theme will be updated to match the system theme when `FollowSystemSetting` is set. See Configuration Updated. If the color theme actually changes as a result of this call, the darkThemeActiveChanged() signal will be emitted.

See also darkThemeActive() and darkThemeActiveChanged().

[static] void QOhosAppContext::startNoUiChildProcess(QString libraryName, QStringList args)

Starts "No UI" child process for a given libraryName and args. Arguments passed to the startNoUiChildProcess() function are forwarded to the child's main() function. See Child Process Manager.

 QtOhosExtras::QOhosAppContext::startNoUiChildProcess(
     "libapp.so",
     QStringList{
         "first arg",
         "second arg",
     });