Qt for Android - Building from Source Qt for Android Examples
Most Qt applications should be portable to Android with ease, unless they depend on a specific hardware or software feature not supported by Android. If your application is not using any such feature, deployment is the only step that might demands some changes to your application.
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 the Qt Resource System.
The other approach is to deploy the resources into the package's assets
directory. It is the best option if you want to achieve 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.
To use the "assets:" approach, for example, add the following lines into the .pro
file:
android { assets.files = images/happy.png assets.path = /assets/images/ INSTALLS += assets }
Then you can access that image asset from C++ as follows:
QImage image("assets:/images/happy.png");
The following step-by-step instructions guide you to port an existing Qt Quick application to Android using the qrc approach:
main.qml
file from resources.qrc
, you can use
the following C++ code:
QQuickView viewer; viewer.setSource(QUrl("qrc:/qml/main.qml")); viewer.show();
Note: QML documents can refer to the contents in qrc files using the relative path to the document. Such references do not require the "qrc:
" or ":/
" prefix.
Note: Qt Quick Controls will use the Material Style if the target device is running Android 3.0 (API 11) or later. The application should function normally on devices with Android versions earlier than v3.0, but without native style for controls.
Qt for Android - Building from Source Qt for Android Examples
As part of the free Business evaluation, we offer a free welcome call for companies, to talk about your requirements, and how the Felgo SDK & Services can help you. Just sign up and schedule your call.
Sign up now to start your free Business evaluation: