移植到 Android

Most Qt applications should be portable to Android with ease unless they depend on a specific hardware or software feature not supported by Android.

The porting approach differs depending on whether the existing application is QML or Widget based, your intended build tool, and if you are porting from a mobile or desktop application.

QML-based applications built with CMake are considered the best approach for mobile applications.

移植自 Qt 桌面应用程序

Most of your existing Qt code should work, but you must make some decisions around your UI scalability and layout for different orientations and screen sizes. Consider a responsive layouts approach to fully take advantage of Qt Qml Applications.

There are considerations that are not directly impacted by using Qt, such as app store guidelines, theme guidelines, and others that may impact the way you develop your application. These won't be discussed here.

添加资源

Most applications need resources. Here, we discuss how that impacts porting your application to Android.

Qt 资源系统

Like most UI applications, Qt applications also depend on resources such as images, icons, translation files, and so on. These resources must be made available on the device as they are required for the application to function correctly.

The most convenient option is to bundle the resources into a qrc file, which gets built into the application binary. This approach reduces the porting effort considerably and provides faster access to the resources. It is also a cross-platform approach, which makes porting to other platforms easier.

By default, all Qt applications can access the contents of a qrc file using the ":/" prefix or the URL scheme prefix, "qrc:". To know more about qrc files and how they are handled, see Qt 资源系统 .

使用资产方式添加资源

Qt for Android provides a special, virtual file system that is based on the Android 资产机制 . Files that are put under the directory " ANDROID_PACKAGE_SOURCE_DIR /assets/" are packaged as part of your application package.

The asset approach is the best option for better interoperability with the Android APIs. You can access all resources in the directory using the "assets:" prefix. Unlike qrc, this approach is Android-specific, not a cross-platform solution.

注意: 使用 Qt 资源系统 entails having qrc files bundled in the shared libraries which must be unpacked first and loaded by the linker, while the Android assets are compressed in the APK and can be directly used in your application. That means using the Android assets can take up less space, but it's not a cross-platform solution with Qt.

采用 CMake 的资产方式

If you are new to using CMake or CMake with Qt for the first time, see 构建采用 CMake . Here, the focus is on the steps applicable for adding resources.

You will likely want to modify a few things that are controlled by the Android Manifest file. See Qt Android 清单文件配置 for more information on that. So you likely have already set QT_ANDROID_PACKAGE_SOURCE_DIR 像这样:

set_property(TARGET target_name PROPERTY QT_ANDROID_PACKAGE_SOURCE_DIR
             "${CMAKE_CURRENT_SOURCE_DIR}/android")
					

Now, you can place your assets directly in that folder or have a custom step with CMake to copy them from the main project path to the assets path.

Then, you can access that image asset from C++ as follows:

QImage image("assets:/images/happy.png");
					

Qt for Android - 从源代码构建 在 Android 部署应用程序