QT: qtpy 抽象的QT API 库 与 Qt for MCU + PyQt6 to Android

https://www.riverbankcomputing.com/static/Docs/PyQt6/
https://www.qt.io/blog/taking-qt-for-python-to-android
https://github.com/shyamnathp/python-for-android/tree/pyside_support

一个非常好的 PyQT 实例 应用App 是 Anaconda 推出的 anaconda_navigator(兼容 Windows / Linux / MacOS),
安装完 Anaconda 之后,就可以查看到 Anaconda Navigator 的 PyQt 源码。

QT 的版本在不断的更新,为保证 QT5 / QT6 / pyqt6 / pyside6 多个库的 API 持续稳定,
因此出现 qtpy 这个抽象层库,类似 Python 的 six 库保障各版本的API统一。
pip install qtpy
PyQT6 和 PySide6 的:

import os
os.environ['QT_API'] = 'pyqt6' # OR: 'pyside6'
from qtpy import QtGui, QtWidgets, QtCore
print(QtWidgets.QWidget)

PySide is! - What's compatible with asyncio?
from PySide6.QtAsyncio import QAsyncioEventLoopPolicy
and
asyncio.set_event_loop_policy(QAsyncioEventLoopPolicy())
asyncio lets you replace its event loop with a custom implementation, and such a custom implementation is exactly what we have been doing.
This allows you to use the asyncio API and run programs that use asyncio or frameworks based on asyncio,
this puts the entire Qt API at asyncio's disposal, enabling you to mix and match code from asyncio and Qt, leveraging asyncio's async/await syntax.
Today, with the first release of the 6.6 cycle, we offer a technical preview of this event loop implementation,
letting you create futures, tasks, and handles, and manage the event loop lifecycle,
with wide coverage of the full event loop API to be built up over the upcoming minor releases.
Using Qt's event loop is as simple as adding these two lines to your code:

https://www.qt.io/blog/qt-for-mcus-2.5.2-lts-released
Qt for MCUs 2.5.2 LTS (Long-Term Support) has been released and is available for download. As a patch release, Qt for MCUs 2.5.2 LTS provides bug fixes and other improvements, and maintains source compatibility with Qt for MCUs 2.5.x. It does not add any new functionality.
For more information about fixed bugs, please check the Qt for MCUs 2.5.2 changelog.
As with other Qt products, Qt for MCUs 2.5.2 LTS can be added to an existing installation by using the maintenance tool or via a clean installation using Qt Online Installer.
More patch releases for Qt for MCUs 2.5 LTS are planned until December 2024, which is the end of the standard support period for that LTS version.

Taking Qt for Python to Android
April 19, 2023 by Shyamnath Premnadh | Comments

Deploying Python applications to Android is a big talking point these days among the Python community, and a frequently asked question by people developing their first GUI applications. There are already a couple of Python frameworks that offer the ability to deploy your applications to Android devices, and Qt for Android has existed for a decade now. With QML and Qt Quick, one can use Qt to develop native Android applications. It was high time we bridged the gap and brought PySide applications to Android.

With the new 6.5 release, we give you a new CLI tool, pyside6-android-deploy, that provides an easy interface to deploy your PySide6 application 🎉.

Technical Details

Currently, this tool is only available on Linux-based environments, and only a subset of the Qt modules are supported (see Considerations).

This tool uses a custom fork of the python-for-android project with a custom Qt bootstrap and recipes for PySide6 and shiboken6. The reason for using python-for-android instead of androiddeployqt is that python-for-android already provides a way to package the Python interpreter, which is by default not pre-installed on Android platforms. It can also be easily extended to include new Python packages, even those with a C/C++ backend. python-for-android already supports many popular Python packages like numpy, pandas, etc.

The entire deployment process is a 3-step process, where the first two steps need to be done only once per Android platform. The first two steps are setting up and cross-compiling Qt for Python wheels required for pyside6-android-deploy to work.

Steps to deploy your PySide application to Android

As mentioned above, Steps 1 and 2 need to be done only once for a specific Android architecture. Each PySide6 application that you deploy will use the same Qt for Python wheels.

Step 1: Setting up prerequisites

The Android dependencies for PySide6 are the same dependencies as those for Qt for Android. This step involves downloading the JDK, Android NDK, and Android SDK. You may skip this step if you already have them downloaded and set up. The recommended NDK version for PySide6 version 6.5 is r25. You can either use Qt Creator to install all the dependencies or install the dependencies manually, as shown below.

  1. Install JDK 11 or above. See instructions here.
  2. Download the latest version of Android Command Line Tools Only for Linux. This will download a .zip file.
  3. You can use the following script to help you download and setup Android SDK and NDK(r25c) into your current working directory.
#!/bin/bash 
shopt -s extglob 
if [ -z "$1" ] 
  then 
    echo "Supply path to  commandlinetools-linux-*_latest.zip" 
    exit 1 
fi 
android_sdk=$(pwd)/android_sdk 
mkdir -p $android_sdk 
unzip $1 -d $android_sdk 
latest=$android_sdk/cmdline-tools/latest 
mkdir -p $latest 
mv $android_sdk/cmdline-tools/!(latest) $latest 
$latest/bin/sdkmanager "ndk;25.2.9519653" "build-tools;33.0.2" "platforms;android-31" "platform-tools" 
cp -avr $android_sdk/cmdline-tools/latest $android_sdk/tools

Simply run the script by giving it execution permission (chmod +x) and passing the path to the downloaded .zip file as a cli argument.

  1. Add the Android SDK and NDK paths into the following environment variables:
export ANDROID_SDK_ROOT=“/android_sdk” 
export ANDROID_NDK_ROOT=“/android_sdk/ndk/25.2.9519653” 

Step 2: Cross-compile Qt for Python wheels for Android

Python-for-android requires shiboken6 and PySide6 to be cross-compiled during the deployment process to produce Qt for Python binaries that are compatible with the specific Android platform. Cross-compiling Qt for Python every time for each of your applications can be a cumbersome, time-consuming task 😫. Hence, it is better to use cross-compilation once to create Qt for Python wheels for an Android platform, which can be reused for each of the applications that you deploy. We have made creating these wheels easier for you with a Python script which is available in the Qt for Python source repository.

Make sure to install all the project requirements up to and including “Getting the source”, then install the cross-compilation requirements like this:

pip install –r tools/cross_compile_android/requirements.txt 

And run the following script:

python tools/cross_compile_android/main.py --plat-name=aarch64 --ndk-path=$ANDROID_NDK_ROOT --qt-install-path=/opt/Qt/6.5.0 --sdk-path=$ANDROID_SDK_ROOT

In the above command, --plat-name refers to one of aarch64, armv7a, i686, x86_64 depending on the target architecture. --qt-install-path refers to the path where Qt 6.5.0 or later is installed. Use --help to see all the other available options.

After the successful completion of this command, you will have the Qt for Python wheels in pyside-setup/dist folder.

Note: We recommend using venv or virtuanenv

Step 3: Deploy your application - pyside6-android-deploy

For this tutorial, we take a simple QtQuick example Scene Graph Painted Item example. You can download this example directly from the link. One main requirement of pyside6-android-deploy tool is that the main Python entry point file should be named main.py. Change the name of painteditem.py to main.py and adjust correspondingly in painteditem.pyproject. The requirement of having the main Python entry point named main.py comes from python-for-android. Having the .pyproject file is not a strict requirement, but it enables pyside6-android-deploy to find the files associated with the project more easily and not include any unnecessary files.

Run the following command:

pyside6-android-deploy --wheel-pyside=pyside-setup/dist/PySide6-6.5.0a1-6.5.0-cp37-abi3-android_aarch64.whl --wheel-shiboken=pyside-setup/dist/shiboken6-6.5.0a1-6.5.0-cp37-abi3-android_aarch64.whl --name=painteditem

In the above command --name refers to the application’s name, as shown in your Android device. Use --help to see the other available options.

At the end of this command, you will have a .apk file created within the application directory, which you can install on your aarch64 based Android device. Hurray, you have successfully taken your Qt for Python application to Android🎉.

Considerations🤔

  1. Presently, the tool pyside6-android-deploy only works from Linux.
    This limitation comes from our cross-compilation infrastructure that we are actively looking to improve and allow macOS and Windows users to also deploy Android applications.

  2. Qt modules supported:
    QtCore,QtGui,QtWidgets,QtNetwork,QtOpenGL,QtQml, QtQuick, QtQuickControls2.

  3. The main Python entry point of the application should be named main.py
    This requirement comes from python-for-android.

What can you expect in the future?🤖

  1. More tutorials and examples
    You can expect all the Qt for Android examples to also work and be available for PySide6 Android deployment.

  2. Simplifying the current deployment process

  3. There are some manual steps that we are aiming to remove, to allow the deployment of applications to be more straightforward.

4.Additional Qt modules compatibility

There are many examples and real use-case that our Qt for Android offering has, and we want to aim to be compatible with them. That’s why we see value in adding support for QtConcurrent, QtPositioning, QtLocation, QtBluetooth, QtSensors, etc.

Thank you for reading this tutorial😀. Have fun trying out PySide6 Android deployment. Feel free to drop us a message or open a suggestion/bug in JIRA. Your feedback fuels us in making Qt for Python better. Also, feel open to connecting to the Qt for Python community through our community platforms.

Stay tuned for further updates on Android deployment 👍.

posted @   abaelhe  阅读(234)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· winform 绘制太阳,地球,月球 运作规律
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
点击右上角即可分享
微信分享提示