Qt for OpenHarmony/user development guide: Difference between revisions

From Qt Wiki
Jump to navigation Jump to search
No edit summary
No edit summary
 
(2 intermediate revisions by the same user not shown)
Line 1: Line 1:
'''English'''[[Qt_for_OpenHarmony/user_development_guide_zh|中文]]


=先导知识=
'''English''' [[Qt_for_OpenHarmony/user_development_guide_zh|中文]]
鸿蒙应用生命周期相关文档:


[https://gitee.com/openharmony/docs/blob/master/zh-cn/application-dev/application-models/stage-model-development-overview.md 应用生命周期(Stage模型)]
= Prerequisite Knowledge =
OpenHarmony application lifecycle-related documentation:


[https://gitee.com/openharmony/docs/blob/master/zh-cn/application-dev/application-models/uiability-lifecycle.md UIAbility组件生命周期]
[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/reference/apis-ability-kit/js-apis-inner-application-uiAbilityContext.md UIAbilityContext API参考]
[https://gitee.com/openharmony/docs/blob/master/zh-cn/application-dev/application-models/uiability-lifecycle.md UIAbility Component Lifecycle]


=Qt for OHOS应用生命周期概述=
[https://gitee.com/openharmony/docs/blob/master/zh-cn/application-dev/reference/apis-ability-kit/js-apis-inner-application-uiAbilityContext.md UIAbilityContext API Reference]
在鸿蒙系统中,应用生命周期管理遵循Stage模型,主要包括UIAbility组件的生命周期管理。Qt for OHOS应用作为鸿蒙生态的一部分,同样需要适配这一生命周期模型。


Qt for OHOS应用的生命周期主要涉及以下几个方面:
= 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.


1. 应用启动与初始化
The lifecycle of Qt for OHOS applications mainly involves the following aspects:


2. 应用间交互(拉起其他应用/被其他应用拉起)
1. Application startup and initialization


3. 多实例管理
2. Inter-application interaction (launching other applications/being launched by other applications)


4. 应用退出
3. Multiple instance management


=应用启动与初始化=
4. Application exit
在Qt for OHOS中,应用启动流程是由系统自动管理的,开发者无需关心底层细节。实际流程如下:


1. 系统启动鸿蒙Stage模型下的UIAbility组件
= 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:


2. UIAbility组件通过NAPI方式调用启动Qt线程
1. The system starts the UIAbility component under the OpenHarmony Stage model


3. 系统自动加载Qt应用的共享库,并调用Qt应用的main函数
2. The UIAbility component calls to start the Qt thread through NAPI


4. Qt应用的主窗口被创建并显示在OHOS的WindowStage中
3. The system automatically loads the shared library of the Qt application and calls the main function of the Qt application


对开发者来说,应用的入口仍然是main函数,无需关注更多细节。
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.
在Qt for OHOS中,可以使用QtOhosExtras模块提供的API (startAbility)来拉起其他应用,替代传统Qt中的QProcess方案。


注意:下面的方法只能拉起带界面应用。
= 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.


==== 显式匹配(指定包名和Ability名) ====
=== 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";          // 目标应用的Ability名称
     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>


==== 隐式匹配(通过action和uri匹配) ====
==== 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";        // 数据URI
     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 ===
当Qt应用被其他应用拉起时,需要处理接收到的Want参数。
When a Qt application is launched by other applications, it needs to handle the received Want parameters.


==== 在模块的module.json里面配置如下: ====
==== Configure the following in the module's module.json5: ====
<syntaxhighlight lang="json">
<syntaxhighlight lang="json">
"abilities": [
"abilities": [
Line 106: Line 107:
         "actions": [
         "actions": [
           "action.system.home",
           "action.system.home",
           "ohos.want.action.viewData"  // 支持的action类型
           "ohos.want.action.viewData"  // Supported action types
         ],
         ],
         "uris": [{"type": "*/*"}]      // 支持的数据类型
         "uris": [{"type": "*/*"}]      // Supported data types
       }
       }
     ]
     ]
Line 115: Line 116:
</syntaxhighlight>
</syntaxhighlight>


==== 处理接收到的Want ====
==== 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()
{
{
     // 连接Want接收信号
     // 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)
{
{
     // 处理接收到的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:
     }
     }
      
      
     // 根据Want内容执行相应操作
     // 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 =
在OpenHarmony系统中,应用可以有多种实例模式:单实例、多实例单进程、多实例多进程。Qt for OHOS应用需要根据需求选择合适的模式。
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.


注意:这里的实例指的是UIAbility实例。
Note: Instances here refer to UIAbility instances.


=== 单实例模式 ===
=== Singleton Mode ===
当应用被再次拉起时,使用现有实例处理新的请求:<syntaxhighlight lang="c++">
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 ===
每次被拉起时创建新的UIAbility实例,但共享同一进程:<syntaxhighlight lang="c++">
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 ===
启动新的应用进程创建新的UIAbility实例处理请求:<syntaxhighlight lang="c++">
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++);
      
      
     // 使用QtOhosExtras启动新进程
     // Use QtOhosExtras to start new process
     QtOhosExtras::startAppProcess(processName, want);
     QtOhosExtras::startAppProcess(processName, want);
}
}
</syntaxhighlight>注意:多实例多进程模式需要在module.json5中添加配置:<syntaxhighlight lang="json">
</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 ===

Latest revision as of 12:11, 14 March 2025

English 中文

Prerequisite Knowledge

OpenHarmony application lifecycle-related documentation:

Application Lifecycle (Stage Model)

UIAbility Component Lifecycle

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.json5:

"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
  }
]

Application Exit

Pre-close