Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
QtQuickView.java
Go to the documentation of this file.
1// Copyright (C) 2024 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
4package org.qtproject.qt.android;
5
6import android.content.Context;
7import android.util.Log;
8
9import java.lang.IllegalArgumentException;
10import java.lang.ref.WeakReference;
11import java.security.InvalidParameterException;
12
29public class QtQuickView extends QtView {
30 private final static String TAG = "QtQuickView";
31
32 private String m_qmlUri;
33 private String[] m_qmlImportPaths = null;
34 private QtQmlStatusChangeListener m_statusChangeListener = null;
35 private QtQmlStatus m_lastStatus = QtQmlStatus.NULL;
36 private boolean m_hasQueuedStatus = false;
37 private WeakReference<QtQuickViewContent> m_loadedComponent;
38
39 native void createQuickView(String qmlUri, int width, int height, long parentWindowReference,
40 long viewReference, String[] qmlImportPaths);
41 native void setRootObjectProperty(long windowReference, String propertyName, Object value);
42 native Object getRootObjectProperty(long windowReference, String propertyName);
43 native int addRootObjectSignalListener(long windowReference, String signalName,
44 Class<?> argType, Object listener);
45 native boolean removeRootObjectSignalListener(long windowReference, int signalListenerId);
46
59 public QtQuickView(Context context, String qmlUri, String appName)
60 throws InvalidParameterException {
61 this(context, qmlUri, appName, null);
62 }
63
79 public QtQuickView(Context context, String qmlUri, String appName, String[] qmlImportPaths)
80 throws InvalidParameterException
81 {
82 super(context, appName);
83 if (qmlUri == null || qmlUri.isEmpty()) {
84 throw new InvalidParameterException(
85 "QtQuickView: argument 'qmlUri' may not be empty or null");
86 }
87 m_qmlUri = qmlUri;
88 m_qmlImportPaths = qmlImportPaths;
89 }
90
98 {
99 super(context);
100 }
101
114 // TODO: QTBUG-125620 -- Refresh/reset import paths when loading a new component
115 public <T extends QtQuickViewContent> void loadContent(T qmlContent, String[] qmlImportPaths)
116 throws InvalidParameterException
117 {
118 String libName = qmlContent.getLibraryName();
119 String qmlUri = qmlContent.getFilePath();
120
121 if (libName == null || libName.isEmpty()) {
122 throw new InvalidParameterException(
123 "QtQuickViewContent: return value of getLibraryName() may not be empty or null");
124 }
125
126 if (qmlUri == null || qmlUri.isEmpty()) {
127 throw new InvalidParameterException(
128 "QtQuickViewContent: return value of getFilePath() may not be empty or null");
129 }
130
131 m_qmlUri = qmlUri;
132 m_qmlImportPaths = qmlImportPaths;
133
134 if (m_loadedComponent != null)
135 m_loadedComponent.clear();
136
137 m_loadedComponent = new WeakReference<>(qmlContent);
138 qmlContent.detachView();
139 qmlContent.attachView(this);
140 // The first QQuickView creation happen after first libs loading
141 // and windowReference() returns a reference to native QQuickView
142 // instance, after that. We don't load library again if the view
143 // exists.
144 if (windowReference() == 0) {
145 loadQtLibraries(libName);
146 } else {
147 createQuickView(m_qmlUri, getWidth(), getHeight(), 0, windowReference(),
148 m_qmlImportPaths);
149 }
150 }
151
160 public <T extends QtQuickViewContent> void loadContent(T qmlContent)
161 throws InvalidParameterException
162 {
163 loadContent(qmlContent, null);
164 }
165
166 @Override
167 protected void createWindow(long parentWindowReference) {
168 createQuickView(m_qmlUri, getWidth(), getHeight(), parentWindowReference, windowReference(),
169 m_qmlImportPaths);
170 }
171
187 public void setProperty(String propertyName, Object value)
188 {
189 setRootObjectProperty(windowReference(), propertyName, value);
190 }
191
207 // getRootObjectProperty always returns a primitive type or an Object
208 // so it is safe to suppress the unchecked warning
209 @SuppressWarnings("unchecked")
210 public <T> T getProperty(String propertyName)
211 {
212 return (T)getRootObjectProperty(windowReference(), propertyName);
213 }
214
225 public <T> int connectSignalListener(String signalName, Class<T> argType,
226 QtSignalListener<T> listener)
227 {
228 int signalListenerId =
229 addRootObjectSignalListener(windowReference(), signalName, argType, listener);
230 if (signalListenerId < 0) {
231 Log.w(TAG, "The signal " + signalName + " does not exist in the root object "
232 + "or the arguments do not match with the listener.");
233 }
234 return signalListenerId;
235 }
236
246 public boolean disconnectSignalListener(int signalListenerId)
247 {
248 return removeRootObjectSignalListener(windowReference(), signalListenerId);
249 }
250
265 {
266 return m_lastStatus;
267 }
268
275 {
276 m_statusChangeListener = listener;
277
278 if (m_hasQueuedStatus)
279 sendStatusChanged(m_lastStatus);
280 m_hasQueuedStatus = false;
281 }
282
283 private void handleStatusChange(int status)
284 {
285 try {
286 m_lastStatus = QtQmlStatus.fromInt(status);
287 } catch (IllegalArgumentException e) {
288 m_lastStatus = QtQmlStatus.NULL;
289 e.printStackTrace();
290 }
291
292 if (m_statusChangeListener == null)
293 m_hasQueuedStatus = true;
294 else
295 sendStatusChanged(m_lastStatus);
296 }
297
298 private void sendStatusChanged(QtQmlStatus status)
299 {
300 QtNative.runAction(() -> {
301 if (m_statusChangeListener != null) {
302 QtQuickViewContent content = m_loadedComponent != null ?
303 m_loadedComponent.get() : null;
304 if (content == null)
305 m_statusChangeListener.onStatusChanged(status);
306 else
307 m_statusChangeListener.onStatusChanged(status, content);
308 }
309 });
310 }
311}
Definition main.cpp:8
void setProperty(String propertyName, Object value)
QtQuickView(Context context, String qmlUri, String appName)
QtQuickView(Context context, String qmlUri, String appName, String[] qmlImportPaths)
void setStatusChangeListener(QtQmlStatusChangeListener listener)
void createWindow(long parentWindowReference)
boolean disconnectSignalListener(int signalListenerId)
QPainter Context
static QtQmlStatus fromInt(int value)
static const QString context()
Definition java.cpp:398
EGLOutputLayerEXT EGLint EGLAttrib value
[5]
#define TAG(x)
GLint GLsizei width