1// Copyright (C) 2024 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GFDL-1.3-no-invariants-only
5 \page qtquickview-android-class.html
6 \title Qt Quick View Android Class
7 \ingroup qt_android_classes
8 \brief Allows you to add QML content to your Android app as a View.
12 The QtQuickView class lets you easily add QML content to your Android app as
13 a \l {Android: View}{View}.
22 \li org.qtproject.qt.android
25 \li org.qtproject.qt.android.QtView
27 – org.qtproject.qt.android.QtLayout
29 –– android.view.ViewGroup
32 \section1 Detailed description
34 The QtQuickView class lets you easily add QML content to your Android app as
35 a \l {Android: View}{View}. \c QtQuickView instantiates a \l QQuickView with
36 a given QML component source (a local or network file) and embeds it to itself.
37 You can add it to your Android app's layout as with any other View. \c QtQuickView
38 is a good choice when you want to extend your non-Qt Android app with QML content but
39 do not want to make the entire app using the Qt framework. It brings the power
40 of Qt Quick into your Android app, making it possible to use various Qt Quick
43 A typical use of the class:
47 protected void onCreate(Bundle savedInstanceState) {
48 super.onCreate(savedInstanceState);
49 setContentView(R.layout.activity_main);
52 QtQuickView qmlView = new QtQuickView(this, "qrc:/qt/qml/target/main.qml", "target");
53 qmlView.setStatusChangeListener(status -> {
54 Log.i(TAG, "QML loading status changed to " + status);
57 // Add QML to your layout
58 layout.addView(qmlView, params);
63 For a more detailed example, see \l {QML in Android Studio Projects}.
65 \section1 Constructors
67 \section2 public QtQuickView(Context parent, String qmlUri, String appName)
69 Creates a QtQuickView to load and render a QML component. Instantiating a
70 QtQuickView will load the Qt libraries, including the app library specified
71 by \e appName. Then, it creates a QQuickView that loads the QML source specified
77 \li \b context: the parent Context.
78 \li \b qmlUri: the URI of the main QML file.
79 \li \b appName: the name of the Qt app library to load and start.
80 This corresponds to the target name set in the Qt app's CMakeLists.txt.
85 Throws a \l {Android: InvalidParameterException}{InvalidParameterException} if
86 a parameter is invalid.
88 \section2 public QtQuickView(Context context, String qmlUri, String appName, String[] qmlImportPaths)
90 Creates a QtQuickView to load and view a QML component. Instantiating a
91 QtQuickView will load the Qt libraries, including the app library specified
92 by \e appName. Then, it creates a QQuickView that loads the QML source specified
93 by \e qmlUri. This overload accepts an array of strings \e qmlImportPaths in the
94 case where the QML application should load QML modules from custom paths.
99 \li \b context: the parent Context.
100 \li \b qmlUri: the URI of the main QML file.
101 \li \b appName: the name of the Qt app library to load and start.
102 This corresponds to the target name set in the Qt app's CMakeLists.txt.
103 \li \b qmlImportPaths: an array of strings for additional import paths to
109 Throws a \l {Android: InvalidParameterException}{InvalidParameterException} if
110 a parameter is invalid.
114 \section2 public interface SignalListener<T>
115 \target SignalListener
117 Invoked on the Android UI thread when the signal has been emitted.
122 \li \b signalName: literal signal name
123 \li \b value: the value delivered by the signal or null if the signal is
127 \section2 public interface StatusChangeListener
128 \target StatusChangeListener
130 Invoked on the Android UI thread when the QML component status has changed.
135 \li \b status: The current status.
140 \section2 Status values
141 \target Status values
143 The status can be \e STATUS_NULL, \e STATUS_READY, \e STATUS_LOADING or
144 \e STATUS_ERROR. For more information, see \l {QQuickView::Status}.
148 \section2 public void setProperty(String propertyName, Object value)
149 \target setProperty()
151 Sets the value of an existing property on the QML root object. The supported
152 types are \c Integer, \c Double, \c Float, \c Boolean, and \c String. These
153 types get converted to their corresponding QML types int, double/float, bool,
154 and string. This function does not add properties to the QML root object if
159 \li \b propertyName: the name of the existing root object property to set its value
160 \li \b value: the value of the property
163 \section2 public <T extends Object> T getProperty(String propertyName)
164 \target getProperty()
166 Gets the value of an existing property of the QML root object. The supported
167 return types are \e Integer, \e Double, \e Float, \e Boolean, and \e String.
168 These types get converted from their corresponding QML types int, double/float,
173 \li \b propertyName: the name of the existing root object property.
178 If the property does not exist or the status of the QML component is
179 anything other than \l {Status values}{STATUS_READY}, this function will return null.
183 Throws a \l {Android: ClassCastException}{ClassCastException} if type casting fails.
185 \section2 public <T> int addSignalListener(String signalName, Class<T> argType, SignalListener<T> listener)
186 \target addSignalListener()
188 Associates a \l {SignalListener} with a signal of the QML root object.
192 \li \b signalName: the name of the root object signal.
193 \li \b argType: the Class type of the signal argument.
194 \li \b listener: an instance of the SignalListener interface.
199 A \c {Connection ID} between signal and listener or the existing connection
200 ID if there is an existing connection between the same signal and listener.
201 Returns a negative value if the signal does not exist on the QML root object.
203 \section2 public boolean removeSignalListener(int signalListenerId)
205 Stops a \l {SignalListener} with a given id obtained from \l addSignalListener()
206 call, from listening to a signal.
210 \li \b signalListenerId: the connection ID.
214 \e True if the connection ID is valid and has been successfully removed,
215 otherwise returns false.
217 \section2 public int getStatus()
220 Gets the \l {Status values}{status} of the QML component.
224 \e STATUS_READY when the QML is ready. Invoking methods that operate on the QML
225 root object, such as \l {setProperty()}, \l {getProperty()}, and
226 \l {addSignalListener()}, would succeed \b only if the current status is
227 \c STATUS_READY. It can also return other \l {Status values}{status} values
228 representing the status of the underlying QQuickView instance.
230 \section2 public void setStatusChangeListener(StatusChangeListener listener)
232 Sets a \l {StatusChangeListener} to listen to status changes.
237 \li \b listener: an instance of a \l {StatusChangeListener} interface.