Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
Loading...
Searching...
No Matches
QtQuickView.qdoc
Go to the documentation of this file.
1// Copyright (C) 2024 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GFDL-1.3-no-invariants-only
3
4/*!
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.
9 \techpreview
10 \since 6.7
11
12 The QtQuickView class lets you easily add QML content to your Android app as
13 a \l {Android: View}{View}.
14
15 \target QtQuickView
16 \table
17 \row
18 \li Class:
19 \li QtQuickView
20 \row
21 \li Package Name:
22 \li org.qtproject.qt.android
23 \row
24 \li Extends:
25 \li org.qtproject.qt.android.QtView
26
27 – org.qtproject.qt.android.QtLayout
28
29 –– android.view.ViewGroup
30 \endtable
31
32 \section1 Detailed description
33
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
41 APIs in Android apps.
42
43 A typical use of the class:
44
45 \code
46 @Override
47 protected void onCreate(Bundle savedInstanceState) {
48 super.onCreate(savedInstanceState);
49 setContentView(R.layout.activity_main);
50 ...
51
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);
55 });
56
57 // Add QML to your layout
58 layout.addView(qmlView, params);
59 ...
60 }
61 \endcode
62
63 For a more detailed example, see \l {QML in Android Studio Projects}.
64
65 \section1 Constructors
66
67 \section2 public QtQuickView(Context parent, String qmlUri, String appName)
68
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
72 by \e qmlUri.
73
74 \section3 Parameters
75
76 \list
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.
81 \endlist
82
83 \section3 Throws
84
85 Throws a \l {Android: InvalidParameterException}{InvalidParameterException} if
86 a parameter is invalid.
87
88 \section2 public QtQuickView(Context context, String qmlUri, String appName, String[] qmlImportPaths)
89
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.
95
96 \section3 Parameters
97
98 \list
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
104 be passed to.
105 \endlist
106
107 \section3 Throws
108
109 Throws a \l {Android: InvalidParameterException}{InvalidParameterException} if
110 a parameter is invalid.
111
112 \section1 Interfaces
113
114 \section2 public interface SignalListener<T>
115 \target SignalListener
116
117 Invoked on the Android UI thread when the signal has been emitted.
118
119 \section3 Parameters
120
121 \list
122 \li \b signalName: literal signal name
123 \li \b value: the value delivered by the signal or null if the signal is
124 without a parameter.
125 \endlist
126
127 \section2 public interface StatusChangeListener
128 \target StatusChangeListener
129
130 Invoked on the Android UI thread when the QML component status has changed.
131
132 \section3 Parameters
133
134 \list
135 \li \b status: The current status.
136 \endlist
137
138 \section1 Fields
139
140 \section2 Status values
141 \target Status values
142
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}.
145
146 \section1 Methods
147
148 \section2 public void setProperty(String propertyName, Object value)
149 \target setProperty()
150
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
155 they do not exist.
156
157 \section3 Parameters
158 \list
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
161 \endlist
162
163 \section2 public <T extends Object> T getProperty(String propertyName)
164 \target getProperty()
165
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,
169 bool, and string.
170
171 \section3 Parameters
172 \list
173 \li \b propertyName: the name of the existing root object property.
174 \endlist
175
176 \section3 Returns
177
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.
180
181 \section3 Throws
182
183 Throws a \l {Android: ClassCastException}{ClassCastException} if type casting fails.
184
185 \section2 public <T> int addSignalListener(String signalName, Class<T> argType, SignalListener<T> listener)
186 \target addSignalListener()
187
188 Associates a \l {SignalListener} with a signal of the QML root object.
189
190 \section3 Parameters
191 \list
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.
195 \endlist
196
197 \section3 Returns
198
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.
202
203 \section2 public boolean removeSignalListener(int signalListenerId)
204
205 Stops a \l {SignalListener} with a given id obtained from \l addSignalListener()
206 call, from listening to a signal.
207
208 \section3 Parameters
209 \list
210 \li \b signalListenerId: the connection ID.
211 \endlist
212
213 \section3 Returns
214 \e True if the connection ID is valid and has been successfully removed,
215 otherwise returns false.
216
217 \section2 public int getStatus()
218 \target getStatus()
219
220 Gets the \l {Status values}{status} of the QML component.
221
222 \section3 Returns
223
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.
229
230 \section2 public void setStatusChangeListener(StatusChangeListener listener)
231
232 Sets a \l {StatusChangeListener} to listen to status changes.
233
234 \section3 Parameters
235
236 \list
237 \li \b listener: an instance of a \l {StatusChangeListener} interface.
238 \endlist
239*/