Qt for OpenHarmony/user development guide: Difference between revisions
No edit summary |
No edit summary |
||
Line 1: | Line 1: | ||
'''English''' [[Qt_for_OpenHarmony/user_development_guide_zh|中文]] | '''English''' [[Qt_for_OpenHarmony/user_development_guide_zh|中文]] | ||
= | = Prerequisite Knowledge = | ||
OpenHarmony application lifecycle-related documentation: | |||
[https://gitee.com/openharmony/docs/blob/master/zh-cn/application-dev/application-models/stage-model-development-overview.md | [https://gitee.com/openharmony/docs/blob/master/zh-cn/application-dev/application-models/stage-model-development-overview.md Application Lifecycle (Stage Model)] | ||
[https://gitee.com/openharmony/docs/blob/master/zh-cn/application-dev/application-models/uiability-lifecycle.md | [https://gitee.com/openharmony/docs/blob/master/zh-cn/application-dev/application-models/uiability-lifecycle.md UIAbility Component Lifecycle] | ||
[https://gitee.com/openharmony/docs/blob/master/zh-cn/application-dev/reference/apis-ability-kit/js-apis-inner-application-uiAbilityContext.md UIAbilityContext | [https://gitee.com/openharmony/docs/blob/master/zh-cn/application-dev/reference/apis-ability-kit/js-apis-inner-application-uiAbilityContext.md UIAbilityContext API Reference] | ||
=Qt for | = Qt for OHOS Application Lifecycle Overview = | ||
In the OpenHarmony system, application lifecycle management follows the Stage model, primarily involving the lifecycle management of UIAbility components. Qt for OHOS applications, as part of the OpenHarmony ecosystem, must adapt to this lifecycle model. | |||
Qt for | The lifecycle of Qt for OHOS applications mainly involves the following aspects: | ||
1. | 1. Application startup and initialization | ||
2. | 2. Inter-application interaction (launching other applications/being launched by other applications) | ||
3. | 3. Multiple instance management | ||
4. | 4. Application exit | ||
= | = Application Startup and Initialization = | ||
In Qt for OHOS, the application startup process is automatically managed by the system, and developers do not need to concern themselves with the underlying details. The actual process is as follows: | |||
1. | 1. The system starts the UIAbility component under the OpenHarmony Stage model | ||
2. | 2. The UIAbility component calls to start the Qt thread through NAPI | ||
3. | 3. The system automatically loads the shared library of the Qt application and calls the main function of the Qt application | ||
4. | 4. The main window of the Qt application is created and displayed in the OHOS WindowStage | ||
For developers, the entry point of the application remains the main function, with no need to focus on additional details. | |||
= | = Inter-application Interaction (Launching Other Applications/Being Launched by Other Applications) = | ||
In Qt for OHOS, you can use the API (startAbility) provided by the QtOhosExtras module to launch other applications, replacing the traditional QProcess solution in Qt. | |||
Note: The following methods can only launch applications with a user interface. | |||
=== | === Launching Other Applications === | ||
==== | ==== Explicit Matching (Specifying Bundle Name and Ability Name) ==== | ||
<syntaxhighlight lang="c++"> | <syntaxhighlight lang="c++"> | ||
#include <QtOhosExtras/qohoswant.h> | #include <QtOhosExtras/qohoswant.h> | ||
Line 51: | Line 52: | ||
{ | { | ||
QtOhosExtras::QOhosWant want; | QtOhosExtras::QOhosWant want; | ||
want.bundleName = "com.example.targetapp"; // | want.bundleName = "com.example.targetapp"; // Target application bundle name | ||
want.abilityName = "MainAbility"; // | want.abilityName = "MainAbility"; // Target application Ability name | ||
// | // Optional: Pass parameters | ||
QJsonObject parameters; | QJsonObject parameters; | ||
parameters.insert("key", "value"); | parameters.insert("key", "value"); | ||
want.parameters = parameters; | want.parameters = parameters; | ||
// | // Launch the target application | ||
QtOhosExtras::startAbility(want); | QtOhosExtras::startAbility(want); | ||
} | } | ||
</syntaxhighlight> | </syntaxhighlight> | ||
==== | ==== Implicit Matching (Matching through Action and URI) ==== | ||
<syntaxhighlight lang="c++"> | <syntaxhighlight lang="c++"> | ||
void MainWindow::startAppByAction() | void MainWindow::startAppByAction() | ||
{ | { | ||
QtOhosExtras::QOhosWant want; | QtOhosExtras::QOhosWant want; | ||
want.action = "ohos.want.action.viewData"; // | want.action = "ohos.want.action.viewData"; // Action type | ||
want.type = "text/plain"; // | want.type = "text/plain"; // Data type | ||
want.uri = "content://example/data"; // | want.uri = "content://example/data"; // Data URI | ||
// | // Optional: Pass parameters | ||
QJsonObject parameters; | QJsonObject parameters; | ||
parameters.insert("key", "value"); | parameters.insert("key", "value"); | ||
want.parameters = parameters; | want.parameters = parameters; | ||
// | // Launch the matching application | ||
QtOhosExtras::startAbility(want); | QtOhosExtras::startAbility(want); | ||
} | } | ||
</syntaxhighlight> | </syntaxhighlight> | ||
=== | === Being Launched by Other Applications === | ||
When a Qt application is launched by other applications, it needs to handle the received Want parameters. | |||
==== | ==== Configure the following in the module's module.json: ==== | ||
<syntaxhighlight lang="json"> | <syntaxhighlight lang="json"> | ||
"abilities": [ | "abilities": [ | ||
Line 106: | Line 107: | ||
"actions": [ | "actions": [ | ||
"action.system.home", | "action.system.home", | ||
"ohos.want.action.viewData" // | "ohos.want.action.viewData" // Supported action types | ||
], | ], | ||
"uris": [{"type": "*/*"}] // | "uris": [{"type": "*/*"}] // Supported data types | ||
} | } | ||
] | ] | ||
Line 115: | Line 116: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
==== | ==== Handling Received Want ==== | ||
<syntaxhighlight lang="c++"> | <syntaxhighlight lang="c++"> | ||
#include <QtOhosExtras/QtOhosExtras> | #include <QtOhosExtras/QtOhosExtras> | ||
Line 121: | Line 122: | ||
void MainWindow::init() | void MainWindow::init() | ||
{ | { | ||
// | // Connect Want reception signal | ||
QObject::connect( | QObject::connect( | ||
QtOhosExtras::QOhosUiAbilityContext::instance(), | QtOhosExtras::QOhosUiAbilityContext::instance(), | ||
Line 132: | Line 133: | ||
void MainWindow::onNewWantReceived(QtOhosExtras::QOhosWant want) | void MainWindow::onNewWantReceived(QtOhosExtras::QOhosWant want) | ||
{ | { | ||
// | // Handle received Want parameters | ||
qDebug() << "Received want with action:" << want.action; | qDebug() << "Received want with action:" << want.action; | ||
qDebug() << "URI:" << want.uri; | qDebug() << "URI:" << want.uri; | ||
qDebug() << "Type:" << want.type; | qDebug() << "Type:" << want.type; | ||
// | // Process parameters | ||
if (want.parameters.contains("key")) { | if (want.parameters.contains("key")) { | ||
QString value = want.parameters.value("key").toString(); | QString value = want.parameters.value("key").toString(); | ||
Line 143: | Line 144: | ||
} | } | ||
// | // Execute corresponding operations based on Want content | ||
if (want.action == "ohos.want.action.viewData") { | if (want.action == "ohos.want.action.viewData") { | ||
openDataForViewing(want.uri); | openDataForViewing(want.uri); | ||
Line 150: | Line 151: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
= | = Multiple Instance Management = | ||
In the OpenHarmony system, applications can have multiple instance modes: singleton, multi-instance single-process, and multi-instance multi-process. Qt for OHOS applications need to choose the appropriate mode based on requirements. | |||
Note: Instances here refer to UIAbility instances. | |||
=== | === Singleton Mode === | ||
When the application is launched again, it uses the existing instance to handle new requests: | |||
<syntaxhighlight lang="c++"> | |||
void MainWindow::onNewWantReceived(QtOhosExtras::QOhosWant want) | void MainWindow::onNewWantReceived(QtOhosExtras::QOhosWant want) | ||
{ | { | ||
// | // Use existing window to handle new requests | ||
processNewRequest(want); | processNewRequest(want); | ||
this->setText(want.uri); // | this->setText(want.uri); // Singleton single-process: original window handles | ||
} | } | ||
</syntaxhighlight> | </syntaxhighlight> | ||
=== | === Multi-instance Single-process Mode === | ||
Creates a new UIAbility instance each time it is launched, but shares the same process: | |||
<syntaxhighlight lang="c++"> | |||
void MainWindow::onNewWantReceived(QtOhosExtras::QOhosWant want) | void MainWindow::onNewWantReceived(QtOhosExtras::QOhosWant want) | ||
{ | { | ||
// | // Create new window instance | ||
MainWindow *newWindow = new MainWindow(); | MainWindow *newWindow = new MainWindow(); | ||
newWindow->processRequest(want); | newWindow->processRequest(want); | ||
Line 177: | Line 180: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
=== | === Multi-instance Multi-process Mode === | ||
Starts a new application process to create a new UIAbility instance to handle requests: | |||
<syntaxhighlight lang="c++"> | |||
void MainWindow::onNewWantReceived(QtOhosExtras::QOhosWant want) | void MainWindow::onNewWantReceived(QtOhosExtras::QOhosWant want) | ||
{ | { | ||
// | // Start new process | ||
static int processCounter = 0; | static int processCounter = 0; | ||
QString processName = "Process" + QString::number(processCounter++); | QString processName = "Process" + QString::number(processCounter++); | ||
// | // Use QtOhosExtras to start new process | ||
QtOhosExtras::startAppProcess(processName, want); | QtOhosExtras::startAppProcess(processName, want); | ||
} | } | ||
</syntaxhighlight> | </syntaxhighlight> | ||
Note: Multi-instance multi-process mode requires adding configuration in module.json5: | |||
<syntaxhighlight lang="json"> | |||
"abilities": [ | "abilities": [ | ||
{ | { | ||
"name": "QAbility", | "name": "QAbility", | ||
// | // Other configurations... | ||
"isolationProcess": true | "isolationProcess": true | ||
} | } | ||
Line 198: | Line 205: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
= | = Application Exit = | ||
=== | === Pre-close === |
Revision as of 10:23, 14 March 2025
English 中文
Prerequisite Knowledge
OpenHarmony application lifecycle-related documentation:
Application Lifecycle (Stage Model)
UIAbilityContext API Reference
Qt for OHOS Application Lifecycle Overview
In the OpenHarmony system, application lifecycle management follows the Stage model, primarily involving the lifecycle management of UIAbility components. Qt for OHOS applications, as part of the OpenHarmony ecosystem, must adapt to this lifecycle model.
The lifecycle of Qt for OHOS applications mainly involves the following aspects:
1. Application startup and initialization
2. Inter-application interaction (launching other applications/being launched by other applications)
3. Multiple instance management
4. Application exit
Application Startup and Initialization
In Qt for OHOS, the application startup process is automatically managed by the system, and developers do not need to concern themselves with the underlying details. The actual process is as follows:
1. The system starts the UIAbility component under the OpenHarmony Stage model
2. The UIAbility component calls to start the Qt thread through NAPI
3. The system automatically loads the shared library of the Qt application and calls the main function of the Qt application
4. The main window of the Qt application is created and displayed in the OHOS WindowStage
For developers, the entry point of the application remains the main function, with no need to focus on additional details.
Inter-application Interaction (Launching Other Applications/Being Launched by Other Applications)
In Qt for OHOS, you can use the API (startAbility) provided by the QtOhosExtras module to launch other applications, replacing the traditional QProcess solution in Qt.
Note: The following methods can only launch applications with a user interface.
Launching Other Applications
Explicit Matching (Specifying Bundle Name and Ability Name)
#include <QtOhosExtras/qohoswant.h>
#include <QtOhosExtras/qohosuiabilitycontext.h>
void MainWindow::startSpecificApp()
{
QtOhosExtras::QOhosWant want;
want.bundleName = "com.example.targetapp"; // Target application bundle name
want.abilityName = "MainAbility"; // Target application Ability name
// Optional: Pass parameters
QJsonObject parameters;
parameters.insert("key", "value");
want.parameters = parameters;
// Launch the target application
QtOhosExtras::startAbility(want);
}
Implicit Matching (Matching through Action and URI)
void MainWindow::startAppByAction()
{
QtOhosExtras::QOhosWant want;
want.action = "ohos.want.action.viewData"; // Action type
want.type = "text/plain"; // Data type
want.uri = "content://example/data"; // Data URI
// Optional: Pass parameters
QJsonObject parameters;
parameters.insert("key", "value");
want.parameters = parameters;
// Launch the matching application
QtOhosExtras::startAbility(want);
}
Being Launched by Other Applications
When a Qt application is launched by other applications, it needs to handle the received Want parameters.
Configure the following in the module's module.json:
"abilities": [
{
"name": "QAbility",
"srcEntry": "./ets/qability/QAbility.ets",
"launchType": "specified",
"description": "$string:QAbility_desc",
"icon": "$media:icon",
"label": "$string:QAbility_label",
"startWindowIcon": "$media:icon",
"startWindowBackground": "$color:start_window_background",
"exported": true,
"skills": [
{
"entities": [
"entity.system.home"
],
"actions": [
"action.system.home",
"ohos.want.action.viewData" // Supported action types
],
"uris": [{"type": "*/*"}] // Supported data types
}
]
}
]
Handling Received Want
#include <QtOhosExtras/QtOhosExtras>
void MainWindow::init()
{
// Connect Want reception signal
QObject::connect(
QtOhosExtras::QOhosUiAbilityContext::instance(),
&QtOhosExtras::QOhosUiAbilityContext::newWantReceived,
this,
&MainWindow::onNewWantReceived
);
}
void MainWindow::onNewWantReceived(QtOhosExtras::QOhosWant want)
{
// Handle received Want parameters
qDebug() << "Received want with action:" << want.action;
qDebug() << "URI:" << want.uri;
qDebug() << "Type:" << want.type;
// Process parameters
if (want.parameters.contains("key")) {
QString value = want.parameters.value("key").toString();
processParameter(value);
}
// Execute corresponding operations based on Want content
if (want.action == "ohos.want.action.viewData") {
openDataForViewing(want.uri);
}
}
Multiple Instance Management
In the OpenHarmony system, applications can have multiple instance modes: singleton, multi-instance single-process, and multi-instance multi-process. Qt for OHOS applications need to choose the appropriate mode based on requirements.
Note: Instances here refer to UIAbility instances.
Singleton Mode
When the application is launched again, it uses the existing instance to handle new requests:
void MainWindow::onNewWantReceived(QtOhosExtras::QOhosWant want)
{
// Use existing window to handle new requests
processNewRequest(want);
this->setText(want.uri); // Singleton single-process: original window handles
}
Multi-instance Single-process Mode
Creates a new UIAbility instance each time it is launched, but shares the same process:
void MainWindow::onNewWantReceived(QtOhosExtras::QOhosWant want)
{
// Create new window instance
MainWindow *newWindow = new MainWindow();
newWindow->processRequest(want);
newWindow->show();
}
Multi-instance Multi-process Mode
Starts a new application process to create a new UIAbility instance to handle requests:
void MainWindow::onNewWantReceived(QtOhosExtras::QOhosWant want)
{
// Start new process
static int processCounter = 0;
QString processName = "Process" + QString::number(processCounter++);
// Use QtOhosExtras to start new process
QtOhosExtras::startAppProcess(processName, want);
}
Note: Multi-instance multi-process mode requires adding configuration in module.json5:
"abilities": [
{
"name": "QAbility",
// Other configurations...
"isolationProcess": true
}
]