Qt for OpenHarmony/zh

From Qt Wiki
< Qt for OpenHarmony
Revision as of 14:56, 28 October 2024 by Rita Qian (talk | contribs) (update the translation for build lib)
Jump to navigation Jump to search


中文 English

OpenHarmony 介绍

OpenHarmony是开放原子基金会孵化并运营的开源项目,目标是构建智能终端设备操作系统的框架与平台,旨在面向全场景、全连接、全智能时代的到来,推动万物互联产业的蓬勃发展。

OpenHarmony 开源项目

华为 DevEco Studio 安装

如果您想开发Qt for OpenHarmony应用程序,除了使用Qt Creator之外,还需要依赖DevEco Studio。

https://developer.huawei.com/consumer/cn/deveco-studio/

一般情况下,不需要配置DevEco Studio使用的sdk版本,因为DevEco Studio默认自带了一个sdk。

但是如果包含的sdk不能满足你的需求,你可以通过下面的方法设置你想要使用的sdk版本:

对于 OpenHarmony 5,您需要安装 API 版本 为12 的SDK:

DevEco_Studio_Preferences

为了避免因开发环境导致的任何问题,请首先确认是否可以使用 DevEco Studio 来开发 Arkts 应用程序。

Gerrit 简介

Qt 源代码托管在 Gerrit 上,用于代码审查和版本控制。有关如何使用 Gerrit 的所有必要信息都可以在以下页面查看:

如何获取 Qt for OpenHarmony 源代码

  1. 访问并登录: https://codereview.qt-project.org
  2. 点击右上角的“设置”,打开设置页面。
  3. 点击“设置”->“HTTP 凭证”->“生成新密码”生成新密码。保存密码以便稍后使用“git clone”使用。
  4. 访问 https://codereview.qt-project.org/admin/repos/qt/tqtc-qt5, 并克隆git 仓库地址。
  5. cd tqtc-qt5
  6. 切换分支:git checkout tqtc/harmonyos-5.12.12 or git checkout tqtc/harmonyos-5.15.16
  7. 更新子模块:git submodule update --init --recursive
Codereview-settings.png


GENERATE-NEW-PASSWORD.png










Git-address.png

在 macOS 中为 Qt for OpenHarmony 构建 Qt 库

准备第三方依赖项

以下说明只是基于 macOS Sonoma 14.0进行准备, 并不代表最低依赖条件。

  • git 版本 2.39.3
  • python 3.12.0

系统环境变量

打开 ~/.zshrc 或者~/.bashrc ,并添加一下内容:

local HARMONY_OS_API_VER=12
local HOME_LIBRARY=${HOME}/Library
export OHOS_SDK_ROOT=${HOME_LIBRARY}/OpenHarmony/Sdk
export HOS_SDK_HOME=${HOME_LIBRARY}/OpenHarmony/Sdk
export OHOS_SDK_12=${HOS_SDK_HOME}/${HARMONY_OS_API_VER}
export NATIVE_OHOS_SDK=${OHOS_SDK_12}/native

构建 Qt 源代码

您可以使用以下脚本以 arm64-v8a 为目标构建 Qt。

请填写您本地源目录的路径。

#!/bin/sh

# Target architecure
ARCH="arm64-v8a"

# Source and build directories
QT5_ROOT_DIR=<ENTER PATH TO YOUR qt/qt5 DIRECORY>
QT5_BUILD_DIR="$QT5_ROOT_DIR/build_${ARCH}"

# Prepare build directory
mkdir "$QT5_BUILD_DIR"
cd "$QT5_BUILD_DIR"

../configure \
    -v \
    -xplatform ohos-clang \
    -prefix ${NATIVE_OHOS_SDK}/opt/Qt/5.12.12/ohos-${ARCH}-clang/ \
    -opensource \
    -confirm-license \
    -release \
    -no-use-gold-linker \
    -no-gcc-sysroot \
    -ohos-arch ${ARCH} \
    -opengles3 \
    -skip qt3d -skip qtactiveqt -skip qtandroidextras -skip qtcanvas3d -skip qtcharts \
    -skip qtconnectivity -skip qtdatavis3d -skip qtdoc -skip qtdocgallery -skip qtfeedback \
    -skip qtgamepad -skip qtgraphicaleffects -skip qtlocation -skip qtmacextras -skip qtnetworkauth \
    -skip qtpim -skip qtpurchasing -skip qtqa -skip qtremoteobjects -skip qtrepotools \
    -skip qtscript -skip qtscxml -skip qtsensors -skip qtserialbus -skip qtserialport \
    -skip qtspeech -skip qtsystems -skip qttools -skip qttranslations -skip qtvirtualkeyboard \
    -skip qtwayland -skip qtwebchannel -skip qtwebengine -skip qtwebglplugin -skip qtwebsockets \
    -skip qtwebview -skip qtwinextras -skip qtx11extras -skip qtxmlpatterns -skip doc \
    -no-dbus \
    -c++std c++14 \
    -make tests

然后,

make -j16
make install

在 macOS 中为 OpenHarmony 构建 Qt 应用程序

在 Qt Creator 中为 OpenHarmony 创建开发工具包

转到 PreferencesKitsQt Versions. 单击添加 Add….按钮

Macos-qtversion-add.jpg

配置 Qt 版本

选择编译Qt for OpenHarmony源代码生成的 qmake文件:

Macos-oh-qmake.jpg

配置 编译器

转到 Compilers 选项卡. 单击 Add 按钮,,然后选择 CustomC.

Oh-compile-c

设置填写:

Name: OHOS Clang

Compiler path: /path/to/OpenHarmony/Sdk/12/native/llvm/bin/clang

Make Path: /usr/bin/make

ABI: arm - linux - generic - elf - 64bit

Compilers-filled.jpg

对于 C++ 编译器,请按照上述步骤操作。不过,请确保将名称设置为 OHOS Clang++,并在编译器路径中选择

并使用 clang++。

CppCompilers-filled.jpg

配置套件

转到 Kits 选项卡。

单击 Add按钮。

Kits.jpg

在 Qt Creator 中设置所需的环境变量

转到 PreferencesEnvironmentSystem ,然后单击环境中系统选项的 Change… 按钮.

通过添加一行来添加新的环境变量:

NATIVE_OHOS_SDK=/path/to/OpenHarmony/Sdk/12/native

Change-env.jpg

在 Windows 中为 OpenHarmony 构建 Qt 库

准备构建环境

  • 安装 DevEco Studio 并从其 SDK 管理器中下载OpenHarmony/native 包,Qt for OpenHarmony/zh#华为 DevEco Studio 安装
  • 下载 MinGW 工具链

    例如 llvm-mingw-20230919-ucrt-x86_64.zip from Releases · mstorsjo/llvm-mingw 解压并将文件夹复制到 d:\

  • 安装Perl

    Strawberry Perl for Windows

  • 将MinGW 和Perl路径添加到 PATH 环境变量中,如下所示:

    Add environment variable.png

  • 另外在系统环境变量中添加 MINGW_ROOT 和 PERL_ROOT :

    Add root for environment.png

  • 打开命令提示符 (使用CMD工具) ,并按照一下方式设置环境变量:
    REM set NATIVE_OHOS_SDK 
    SET NATIVE_OHOS_SDK=D:\oh\DevEcoStudio\sdk\HarmonyOS-NEXT-DB6\openharmony\native
    
    REM set OHOS_SDK_SYSROOT 
    SET OHOS_SDK_SYSROOT=%NATIVE_OHOS_SDK%\sysroot
    
    REM set LLVM_INSTALL_DIR 
    SET LLVM_INSTALL_DIR=%NATIVE_OHOS_SDK%\llvm
    

构建

  • 使用相同的命令提示符找到你的 qt5 仓库目录并创建与之并行的build目录(不要设置build目录是 qt5 仓库目录的子目录)。然后转到 build 目录,设置一个包含跳过的部分子模块的变量并运行 configure命令:
    REM set QT_SKIPS 
    SET QT_SKIPS=-skip qt3d -skip qtactiveqt -skip qtandroidextras -skip qtcanvas3d -skip qtcharts -skip qtconnectivity -skip qtdatavis3d -skip qtdoc -skip qtdocgallery -skip qtfeedback -skip qtgamepad -skip qtgraphicaleffects -skip qtlocation -skip qtmacextras -skip qtnetworkauth -skip qtpim -skip qtpurchasing -skip qtqa -skip qtremoteobjects -skip qtrepotools -skip qtscript -skip qtscxml -skip qtsensors -skip qtserialbus -skip qtserialport -skip qtspeech -skip qtsystems -skip qttools -skip qttranslations -skip qtvirtualkeyboard -skip qtwayland -skip qtwebchannel -skip qtwebengine -skip qtwebglplugin -skip qtwebsockets -skip qtwebview -skip qtwinextras -skip qtx11extras -skip qtxmlpatterns -no-dbus -skip doc
    
    REM  configure command
    call ..\tqtc-qt5\configure.bat -v -xplatform ohos-clang -device-option CROSS_COMPILE=%LLVM_INSTALL_DIR%\bin -prefix c:\Qt\qt-5.12.12-ohos -opensource -confirm-license -debug -no-use-gold-linker %QT_SKIPS% -nomake tests -nomake examples -no-gcc-sysroot -opengles3 -c++std c++14 -ohos-arch arm64-v8a
    
  • 配置成功后,您可以使用 mingw32-make进行构建:
     REM build mingw32-make mingw32-make -j8
    
  • 构建成功后,您可以使用以下命令开始安装:
     mingw32-make install
    

在 Windows 中为 OpenHarmony 构建 Qt 应用程序

The following parts of the instruction, that you have installed DevEco studio, OpenHarmony toolchains,already have the device of OpenHarmonyOS.

Be aware that Qt5 build + multiple options, currently Arch is arm64-v8a.

准备 下载Qt Creator 作为 Qt 开发的IDE

Download Qt Creator,create a kit for OpenHarmony manually

在 Qt Creator 中为 OpenHarmony 创建开发工具包

Go to Edit →Preferences→Kits

配置 Qt 版本

Go to Qt Version tab. Click the button Add…

Add kits.png

Go to the folder in which Qt 5.12.12 for OHOS was installed (it should be installed in a path specified with PREFIX), then go to the bin folder, and select the qmake application and click Open. For example, I compiled the Qt source code with PREFIX=C:\Qt\qt-5.12.12-ohos, so qmake will be in C:\Qt\qt-5.12.12-ohos\bin\qmake. Set Name to Qt %{Qt:Version} for OHOS. At this point, there should be information, that there are no compilers that can produce code for this Qt version.

Add qt version.png

配置编译器

Go to the Compilers tab. Click on the button Add, then select Custom→C

Set Name to OHOS Clang.

Assuming you have followed the instruction to build Qt 5.12.12 for OHOS from sources, you should have llvm-mingw already installed. If not, please follow the MinGW toolchain installation stepQt for OpenHarmony#Preparing the Build Environment

Set the Make path to mingw32-make.exe executable shipped with your MinGW toolchain. It should be located in <path-to-MinGW-toolchain>\bin\mingw32-make.exe. Do not forget to substitute <path-to-MinGW-toolchain>with the actual path.

Change the ABI to arm - linux - generic - elf - 64bit

Add clang c.png

Follow the above steps for the c++ compiler. However, this time, make sure to set Name to OHOS Clang++ and in Compiler path select the clang++.exe instead.

Add clang c++.png

配置套件

Go to Qt kits tab. Click the button Add…

Set Name to OHOS Clang

Set Sysroot to D:\oh\DevEcoStudio\sdk\HarmonyOS-NEXT-DB6\openharmony\native\sysroot. In Compiler section, select previously configured compilers for C(OHOS Clang) and C++(OHOS Clang++).

In Qt version select previously configured Qt 5.12.12 for OHOS.

Set Qt mkspec to ohos-clang.

The complete Kit should look like this:

Add ohos clang.png

在 Qt Creator 中设置所需的环境变量

Go to Edit→Preferences→Environment→System and click the Change…button in Environment section.

Add a new environment variable by adding a line NATIVE_OHOS_SDK=D:\oh\DevEcoStudio\sdk\HarmonyOS-NEXT-DB6\openharmony\native

Add environment.png

获取模板 DevEco 项目

Contact us to obtain a template DevEco project for building final OpenHarmony Qt applications. This will be used to run the Qt application on the OpenHarmony device.

Put the template DevEco project into a known location. Let’s call it <deveco-project>

Go to folder<deveco-project>\entry and create a folder<deveco-project>\entry\libs\arm64-v8a

编译一个简单的 Qt 项目

To simplify this instruction, we will not create an application from scratch, instead we will use an existing example from widget.

Go to your Documents folder and create a new folder called qt_creator_projects. Then go to<qt5-root>\examples\widgets\widgets folder in explorer and copy the whole calculator folder into the qt_creator_projects folder

Then, in QtCreator, click on FileOpen File or Project, then go to the qt_creator_projects\calculator and select the calculator.profile.

In Configure Project tab select the OHOS Clang kit and click the Configure Project button.

Trigger the compilation by clicking on BuildBuild Project “calculator”, or use shortcut: Ctrl+B. The project should compile without errors.

You should be able to find the compiled libcalculator.so file inside the Documents\qt_creators_projects\build-calculator-OHOS_Clang-Debug folder.

在硬件上运行 Qt for OpenHamony 应用程序

构建最终的 OHOS 应用程序

Copy the libcalculator.so file from the previous step into your<deveco-project>\entry\libs\arm64-v8a folder

Then copy the needed library from <qt-install-path>\lib\ to <deveco-project>\entry\lib\arm64-v8a\

For example, for libQt5Widgets.so library it will be (Powershell):

Copy-Item "<qt-install-path>\lib\libQt5Widgets.so" -Destination "<deveco-project>\entry\libs\arm64-v8a\"

Similarly, you can find all dependencies of the OHOS qpa plugin, to make sure, you have all necessary libraries.

修改 ets 中的初始加载

Once all libraries are copied, we can modify the eTS source code,

Open the <deveco-project> in DevEco:

Open template app.png

Open file <deveco-project>\entry\src\main\ets\qabilitystage\QAbilitystage.ts. Change the qtApplicationName variable to the name of our library( libcalculator.so ):

Edit EntryAbility.ts.png

构建并运行应用程序

Make sure the device is connected. Once it is connected, it should be visible in DevEco Studio

Device .png

Then adding the signing configs,open file--project structure--signing configs,check all the items,and click the OK.

Tempsnip.png

Once the device is selected in DevEco Studio,you can build and run the application directly:

Run app.png

After BUILD SUCCESSFUL, you can see the the application is running on the target device.