Qt Quick in Android as a View
Android中的Qt Quick作为视图
May 02, 2024 by Tinja Paavoseppä | Comments
2024年5月2日 Tinja Paavoseppä|评论
This blog post is the first one in a series where we are going to have a look at one exciting feature coming as a Technology Preview in 6.7.0: the ability to use QML and Qt Quick in your otherwise non-Qt Android apps as an Android View! We've also created an Android Studio plugin to help you keep the workflow simple. In this first post, we'll have an overview of the feature, what it entails, why we are adding it, and where you could benefit from it. In the following posts, we will show how to use it along with some code examples and take a closer look at the tooling, testing, and some possible use cases, such as 3D.
这篇博文是系列文章中的第一篇,我们将在6.7.0中看到一个令人兴奋的技术预览功能:在非Qt Android应用程序中使用QML和Qt Quick作为Android视图的能力!我们还创建了一个Android Studio插件,帮助简化工作流程。在第一篇文章中,我们将概述该功能,它需要什么,我们为什么要添加它,以及可以从中受益的地方。在接下来的文章中,我将展示如何使用它以及一些代码示例,并深入了解工具、测试和一些可能的用例,如3D。
While it has been possible to deploy a Qt app on Android since Qt 5, that has required you to make your app with Qt first. While that is great if you want the app to be cross-platform, or don't want to delve too deep into Android-specific APIs, there can be some use cases where you cannot go "all in" with Qt. You may be only interested in developing an app for Android and other platforms are not a priority for you, or you may have an existing Android app. Maybe you expect to have to use a lot of Android APIs and aren't thrilled by using JNI to bridge the Android-specific Java or Kotlin code with Qt C++ and QML.
虽然从Qt 5开始就可以在Android上部署Qt应用程序,但这需要首先使用Qt制作应用程序。虽然如果想让应用程序跨平台,或者不想太深入地研究Android特定的API,这是很好的,但在某些用例中,可能无法“全面”使用Qt。可能只对开发Android应用程序感兴趣,而其他平台对你来说不是优先事项,或者可能有一个现有的Android应用程序。也许预计必须使用大量的Android API,但对使用JNI将特定于Android的Java或Kotlin代码与Qt C++和QML桥接并不感到兴奋。
Even when you want to keep your app an Android app first and foremost, maybe you would like to add things like 3D, animations, and custom-styled elements to your UI – and having Qt Quick makes this a breeze. So, in Qt 6.7.0, we introduced a much-requested feature: you can now use QML to add Qt Quick into your Android app as a View. You can use Java or Kotlin to write your app in Android Studio, have it be just like any other Android app, and just add Qt Quick to where you think it would shine! In case you already have existing QML code from another project and think it would fit nicely in your Android app, too, you can easily add it there.
即使首先想让你的应用程序成为安卓应用程序,也许你也想在你的UI中添加3D、动画和自定义元素,而Qt Quick让这一切变得轻而易举。因此,在Qt 6.7.0中,我们引入了一个备受要求的功能:现在可以使用QML将Qt Quick作为视图添加到Android应用程序中。可以使用Java或Kotlin在Android Studio中编写应用程序,让它像任何其他Android应用程序一样,只需将Qt Quick添加到你认为它会发光的地方!如果已经有来自另一个项目的现有QML代码,并且认为它也很适合Android应用程序,可以很容易地将其添加到那里。
QtQuick3D Morphing Example embedded into an Android app, with native Android Sliders replacing the Qt Quick ones.
QtQuick3D变形示例嵌入到Android应用程序中,原生Android Slider取代了Qt Quick Slider。
Embedding QWindows into native UIs has been possible on many platforms, but doing it from Java brings an added layer of complexity. So, we have added a new Java class, QtQuickView, that takes care of the necessary steps for you. It creates a foreign window representing itself and a QQuickView with a given QML source, which it will then parent to itself. It will also load the Qt libraries and set up the Qt runtime. All this happens under the hood, so you don't have to worry about making the JNI calls or monitoring the lifecycles of these windows. All you need to do is add the QtQuickView into your view hierarchy!
在许多平台上,将QWindows嵌入到本机UI中是可能的,但从Java中嵌入会增加一层复杂性。因此,我们添加了一个新的Java类QtQuickView,它处理必要的步骤。它创建了一个表示自身的外部窗口和一个具有给定QML源的QQuickView,然后将其作为自身的父窗口。它还将加载Qt库并设置Qt运行时。所有这些都发生在后台,所以不必担心进行JNI调用或监视这些窗口的生命周期。您所需要做的就是将QtQuickView添加到视图层次结构中!
To be able to use this class, you will first need to add Qt to your Android project. We have created an Android Studio plugin to help you do this; check out the documentation to see how to get and use it! The plugin allows you to create and import a Qt Quick Project and integrate it into the Android project's build chain. This plugin saves you the trouble of using Qt Creator to build the Qt project and manually copying the build artifacts, such as libraries, to the Android Project.
为了能够使用这个类,首先需要将Qt添加到Android项目中。我们已经创建了一个Android Studio插件来帮助做到这一点;查看文档,了解如何获取和使用它!该插件允许创建和导入Qt Quick项目,并将其集成到Android项目的构建链中。这个插件省去了使用Qt Creator构建Qt项目和手动将构建工件(如库)复制到Android项目的麻烦。
This feature is still in its early phases, but we hope you are excited to give it a go and share your feedback and ideas on how to develop it further with us! In the next post, coming soon, we will go through all the steps of adding your Qt content to an Android Studio project, so stay tuned!
这项功能仍处于早期阶段,但我们希望您能尝试一下,并与我们分享如何进一步开发它的反馈和想法!在即将发布的下一篇文章中,我们将完成将您的Qt内容添加到Android Studio项目的所有步骤,请继续关注!