Qt 6.x
The Qt SDK
Loading...
Searching...
No Matches
qquickview.cpp
Go to the documentation of this file.
1// Copyright (C) 2016 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
4#include "qquickview.h"
5#include "qquickview_p.h"
6
7#include "qquickwindow_p.h"
8#include "qquickitem_p.h"
10
11#include <QtQml/qqmlengine.h>
12#include <private/qqmlengine_p.h>
13#include <private/qv4qobjectwrapper_p.h>
14#include <QtCore/qbasictimer.h>
15
16#include <memory>
17
19
21{
22 Q_Q(QQuickView);
23
24 engine = e;
25
26 if (engine.isNull())
27 engine = new QQmlEngine(q);
28
30
32 engine.data()->setIncubationController(q->incubationController());
33
34 {
35 // The content item has CppOwnership policy (set in QQuickWindow). Ensure the presence of a JS
36 // wrapper so that the garbage collector can see the policy.
39 }
40}
41
43 : component(nullptr), resizeMode(QQuickView::SizeViewToRootObject), initialSize(0,0)
44{
45}
46
48{
49}
50
52{
53 Q_Q(QQuickView);
54 if (!engine) {
55 qWarning() << "QQuickView: invalid qml engine.";
56 return;
57 }
58
59 if (root)
60 delete root;
61 if (component) {
62 delete component;
63 component = nullptr;
64 }
65 if (!source.isEmpty()) {
67 if (!component->isLoading()) {
68 q->continueExecute();
69 } else {
71 q, SLOT(continueExecute()));
72 }
73 }
74}
75
77 const QRectF &oldGeometry)
78{
79 Q_Q(QQuickView);
80 if (resizeItem == root && resizeMode == QQuickView::SizeViewToRootObject) {
81 // wait for both width and height to be changed
83 }
84 QQuickItemChangeListener::itemGeometryChanged(resizeItem, change, oldGeometry);
85}
86
126{
127 d_func()->init();
128}
129
137{
139}
140
152{
154 d_func()->init(engine);
155}
156
161 : QQuickWindow(*(new QQuickViewPrivate), control)
162{
163 d_func()->init();
165}
166
171{
172 // Ensure that the component is destroyed before the engine; the engine may
173 // be a child of the QQuickViewPrivate, and will be destroyed by its dtor
174 Q_D(QQuickView);
175 delete d->root;
176}
177
199{
200 Q_D(QQuickView);
201 d->source = url;
202 d->execute();
203}
204
218void QQuickView::setInitialProperties(const QVariantMap &initialProperties)
219{
220 Q_D(QQuickView);
221 d->initialProperties = initialProperties;
222}
223
230{
231 Q_D(QQuickView);
232 d->source = url;
233 d->component = component;
234
235 if (d->component && d->component->isError()) {
236 const QList<QQmlError> errorList = d->component->errors();
237 for (const QQmlError &error : errorList) {
238 QMessageLogger(error.url().toString().toLatin1().constData(), error.line(), nullptr).warning()
239 << error;
240 }
242 return;
243 }
244
245 if (!d->setRootObject(item))
246 delete item;
248}
249
256{
257 Q_D(const QQuickView);
258 return d->source;
259}
260
266{
267 Q_D(const QQuickView);
268 return d->engine ? const_cast<QQmlEngine *>(d->engine.data()) : nullptr;
269}
270
279{
280 Q_D(const QQuickView);
281 return d->engine ? d->engine.data()->rootContext() : nullptr;
282}
283
309{
310 Q_D(const QQuickView);
311 if (!d->engine)
312 return QQuickView::Error;
313
314 if (!d->component)
315 return QQuickView::Null;
316
317 if (d->component->status() == QQmlComponent::Ready && !d->root)
318 return QQuickView::Error;
319
320 return QQuickView::Status(d->component->status());
321}
322
328{
329 Q_D(const QQuickView);
330 QList<QQmlError> errs;
331
332 if (d->component)
333 errs = d->component->errors();
334
335 if (!d->engine) {
337 error.setDescription(QLatin1String("QQuickView: invalid qml engine."));
338 errs << error;
339 } else if (d->component && d->component->status() == QQmlComponent::Ready && !d->root) {
341 error.setDescription(QLatin1String("QQuickView: invalid root object."));
342 errs << error;
343 }
344
345 return errs;
346}
347
362{
363 Q_D(QQuickView);
364 if (d->resizeMode == mode)
365 return;
366
367 if (d->root) {
368 if (d->resizeMode == SizeViewToRootObject) {
370 p->removeItemChangeListener(d, QQuickItemPrivate::Geometry);
371 }
372 }
373
374 d->resizeMode = mode;
375 if (d->root) {
376 d->initResize();
377 }
378}
379
381{
382 if (root) {
385 p->addItemChangeListener(this, QQuickItemPrivate::Geometry);
386 }
387 }
388 updateSize();
389}
390
392{
393 Q_Q(QQuickView);
394 if (!root)
395 return;
396
398 QSize newSize = QSize(root->width(), root->height());
399 if (newSize.isValid() && newSize != q->size()) {
400 q->resize(newSize);
401 }
403 bool needToUpdateWidth = !qFuzzyCompare(q->width(), root->width());
404 bool needToUpdateHeight = !qFuzzyCompare(q->height(), root->height());
405
406 if (needToUpdateWidth && needToUpdateHeight)
407 root->setSize(QSizeF(q->width(), q->height()));
408 else if (needToUpdateWidth)
409 root->setWidth(q->width());
410 else if (needToUpdateHeight)
411 root->setHeight(q->height());
412 }
413}
414
416{
418 int widthCandidate = -1;
419 int heightCandidate = -1;
420 if (root) {
421 widthCandidate = root->width();
422 heightCandidate = root->height();
423 }
424 if (widthCandidate > 0) {
425 rootObjectSize.setWidth(widthCandidate);
426 }
427 if (heightCandidate > 0) {
428 rootObjectSize.setHeight(heightCandidate);
429 }
430 return rootObjectSize;
431}
432
434{
435 Q_D(const QQuickView);
436 return d->resizeMode;
437}
438
442void QQuickView::continueExecute()
443{
444 Q_D(QQuickView);
445 disconnect(d->component, SIGNAL(statusChanged(QQmlComponent::Status)), this, SLOT(continueExecute()));
446
447 if (d->component->isError()) {
448 const QList<QQmlError> errorList = d->component->errors();
449 for (const QQmlError &error : errorList) {
450 QMessageLogger(error.url().toString().toLatin1().constData(), error.line(), nullptr).warning()
451 << error;
452 }
454 return;
455 }
456
457 std::unique_ptr<QObject> obj(d->initialProperties.empty()
458 ? d->component->create()
459 : d->component->createWithInitialProperties(d->initialProperties));
460
461 if (d->component->isError()) {
462 const QList<QQmlError> errorList = d->component->errors();
463 for (const QQmlError &error : errorList) {
464 QMessageLogger(error.url().toString().toLatin1().constData(), error.line(), nullptr).warning()
465 << error;
466 }
468 return;
469 }
470
471 if (d->setRootObject(obj.get()))
472 Q_UNUSED(obj.release());
474}
475
476
486{
487 Q_Q(QQuickView);
488 if (root == obj)
489 return true;
490
491 delete root;
492 if (obj == nullptr)
493 return true;
494
496 root = sgItem;
498 sgItem->setParentItem(q->QQuickWindow::contentItem());
499 QQml_setParent_noEvent(sgItem, q->QQuickWindow::contentItem());
501 if ((resizeMode == QQuickView::SizeViewToRootObject || q->width() <= 1 || q->height() <= 1) &&
502 initialSize != q->size()) {
503 q->resize(initialSize);
504 }
505 initResize();
506 return true;
507 }
508
510 qWarning() << "QQuickView does not support using a window as a root item." << Qt::endl
511 << Qt::endl
512 << "If you wish to create your root window from QML, consider using QQmlApplicationEngine instead." << Qt::endl;
513 return false;
514 }
515
516 qWarning() << "QQuickView only supports loading of root objects that derive from QQuickItem." << Qt::endl
517 << Qt::endl
518 << "Ensure your QML code is written for QtQuick 2, and uses a root that is or" << Qt::endl
519 << "inherits from QtQuick's Item (not a Timer, QtObject, etc)." << Qt::endl;
520 return false;
521}
522
529{
530 Q_D(QQuickView);
531 if (!e || e->timerId() == d->resizetimer.timerId()) {
532 d->updateSize();
533 d->resizetimer.stop();
534 }
535}
536
542{
543 Q_D(const QQuickView);
544 QSize rootObjectSize = d->rootObjectSize();
545 if (rootObjectSize.isEmpty()) {
546 return size();
547 } else {
548 return rootObjectSize;
549 }
550}
551
560{
561 Q_D(const QQuickView);
562 return d->initialSize;
563}
564
569{
570 Q_D(const QQuickView);
571 return d->root;
572}
573
580{
581 Q_D(QQuickView);
582 if (d->resizeMode == SizeRootObjectToView)
583 d->updateSize();
584
586}
587
590{
592}
593
596{
598}
599
602{
604}
605
608{
610}
611
614{
616}
617
618
620
621#include "moc_qquickview.cpp"
void start(int msec, QObject *obj)
\obsolete Use chrono overload instead.
QV4::ExecutionEngine * handle() const
Definition qjsengine.h:292
The QKeyEvent class describes a key event.
Definition qevent.h:423
Definition qlist.h:74
\inmodule QtCore
Definition qlogging.h:68
void void Q_DECL_COLD_FUNCTION void warning(const char *msg,...) const Q_ATTRIBUTE_FORMAT_PRINTF(2
Logs a warning message specified with format msg.
Definition qlogging.cpp:648
\inmodule QtGui
Definition qevent.h:195
\inmodule QtCore
Definition qobject.h:90
static QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *member, Qt::ConnectionType=Qt::AutoConnection)
\threadsafe
Definition qobject.cpp:2823
T * data() const
Definition qpointer.h:56
bool isNull() const
Returns true if the referenced object has been destroyed or if there is no referenced object; otherwi...
Definition qpointer.h:67
The QQmlComponent class encapsulates a QML component definition.
bool isLoading() const
Returns true if status() == QQmlComponent::Loading.
The QQmlContext class defines a context within a QML engine.
Definition qqmlcontext.h:25
The QQmlEngine class provides an environment for instantiating QML components.
Definition qqmlengine.h:57
QQmlIncubationController * incubationController() const
Returns the currently set incubation controller, or 0 if no controller has been set.
static void setContextForObject(QObject *, QQmlContext *)
Sets the QQmlContext for the object to context.
void setIncubationController(QQmlIncubationController *)
Sets the engine's incubation controller.
QQmlContext * rootContext() const
Returns the engine's root context.
The QQmlError class encapsulates a QML error.
Definition qqmlerror.h:18
virtual void itemGeometryChanged(QQuickItem *, QQuickGeometryChange, const QRectF &)
static QQuickItemPrivate * get(QQuickItem *item)
The QQuickItem class provides the most basic of all visual items in \l {Qt Quick}.
Definition qquickitem.h:64
void setSize(const QSizeF &size)
void setFlag(Flag flag, bool enabled=true)
Enables the specified flag for this item if enabled is true; if enabled is false, the flag is disable...
void setHeight(qreal)
qreal width
This property holds the width of this item.
Definition qquickitem.h:76
qreal height
This property holds the height of this item.
Definition qquickitem.h:77
void setWidth(qreal)
The QQuickRenderControl class provides a mechanism for rendering the Qt Quick scenegraph onto an offs...
QSize rootObjectSize() const
bool setRootObject(QObject *)
QBasicTimer resizetimer
QQuickView::ResizeMode resizeMode
QPointer< QQuickItem > root
void init(QQmlEngine *e=nullptr)
QPointer< QQmlEngine > engine
QQmlComponent * component
void itemGeometryChanged(QQuickItem *item, QQuickGeometryChange change, const QRectF &) override
The QQuickView class provides a window for displaying a Qt Quick user interface.
Definition qquickview.h:20
QQuickView(QWindow *parent=nullptr)
Constructs a QQuickView with the given parent.
void setContent(const QUrl &url, QQmlComponent *component, QObject *item)
~QQuickView() override
Destroys the QQuickView.
QUrl source
The URL of the source of the QML component.
Definition qquickview.h:24
ResizeMode resizeMode
whether the view should resize the window contents
Definition qquickview.h:22
void mousePressEvent(QMouseEvent *) override
\reimp
QList< QQmlError > errors() const
Return the list of errors that occurred during the last compile or create operation.
void keyReleaseEvent(QKeyEvent *) override
\reimp
void keyPressEvent(QKeyEvent *) override
\reimp
QSize initialSize() const
Returns the initial size of the root object.
void setResizeMode(ResizeMode)
void timerEvent(QTimerEvent *) override
Status status
The component's current \l{QQuickView::Status} {status}.
Definition qquickview.h:23
QQmlEngine * engine() const
Returns a pointer to the QQmlEngine used for instantiating QML Components.
QQmlContext * rootContext() const
This function returns the root of the context hierarchy.
ResizeMode
This enum specifies how to resize the view.
Definition qquickview.h:39
@ SizeViewToRootObject
Definition qquickview.h:39
@ SizeRootObjectToView
Definition qquickview.h:39
void resizeEvent(QResizeEvent *) override
Status
Specifies the loading status of the QQuickView.
Definition qquickview.h:44
void mouseMoveEvent(QMouseEvent *) override
\reimp
void setSource(const QUrl &)
Sets the source to the url, loads the QML component and instantiates it.
void setInitialProperties(const QVariantMap &initialProperties)
Sets the initial properties initialProperties with which the QML component gets initialized after cal...
QQuickItem * rootObject() const
Returns the view's root \l {QQuickItem} {item}.
void statusChanged(QQuickView::Status)
This signal is emitted when the component's current status changes.
QSize sizeHint() const
void mouseReleaseEvent(QMouseEvent *) override
\reimp
QQuickRootItem * contentItem
\qmltype Window \instantiates QQuickWindow \inqmlmodule QtQuick
void mousePressEvent(QMouseEvent *) override
\reimp
void resizeEvent(QResizeEvent *) override
\reimp
void mouseReleaseEvent(QMouseEvent *) override
\reimp
void mouseMoveEvent(QMouseEvent *) override
\reimp
void keyReleaseEvent(QKeyEvent *) override
\reimp
void keyPressEvent(QKeyEvent *) override
\reimp
\inmodule QtCore\reentrant
Definition qrect.h:483
The QResizeEvent class contains event parameters for resize events.
Definition qevent.h:547
\inmodule QtCore
Definition qsize.h:207
\inmodule QtCore
Definition qsize.h:25
constexpr void setWidth(int w) noexcept
Sets the width to the given width.
Definition qsize.h:135
constexpr bool isEmpty() const noexcept
Returns true if either of the width and height is less than or equal to 0; otherwise returns false.
Definition qsize.h:123
constexpr void setHeight(int h) noexcept
Sets the height to the given height.
Definition qsize.h:138
constexpr bool isValid() const noexcept
Returns true if both the width and height is equal to or greater than 0; otherwise returns false.
Definition qsize.h:126
\inmodule QtCore
Definition qcoreevent.h:359
\inmodule QtCore
Definition qurl.h:94
\inmodule QtGui
Definition qwindow.h:63
QSize size() const override
Returns the size of the window excluding any window frame.
Definition qwindow.h:210
void statusChanged(QDeclarativeComponent::Status status)
[1]
Definition qlogging.cpp:9
double e
Combined button and popup list for selecting options.
QTextStream & endl(QTextStream &stream)
Writes '\n' to the stream and flushes the stream.
DBusConnection const char DBusError * error
bool qFuzzyCompare(qfloat16 p1, qfloat16 p2) noexcept
Definition qfloat16.h:287
#define qWarning
Definition qlogging.h:162
#define SLOT(a)
Definition qobjectdefs.h:51
#define SIGNAL(a)
Definition qobjectdefs.h:52
GLenum mode
GLsizei GLsizei GLchar * source
GLhandleARB obj
[2]
GLdouble GLdouble GLdouble GLdouble q
Definition qopenglext.h:259
GLfloat GLfloat p
[1]
static qreal component(const QPointF &point, unsigned int i)
void QQml_setParent_noEvent(QObject *object, QObject *parent)
Makes the object a child of parent.
QQuickItem * qobject_cast< QQuickItem * >(QObject *o)
Definition qquickitem.h:483
#define Q_ASSERT(cond)
Definition qrandom.cpp:47
QLatin1StringView QLatin1String
Definition qstringfwd.h:31
#define emit
#define Q_UNUSED(x)
QWindow * qobject_cast< QWindow * >(QObject *o)
Definition qwindow.h:367
QUrl url("example.com")
[constructor-url-reference]
QObject::connect nullptr
myObject disconnect()
[26]
QGraphicsItem * item
QJSEngine engine
[0]
static ReturnedValue wrap(ExecutionEngine *engine, QObject *object)
IUIAutomationTreeWalker __RPC__deref_out_opt IUIAutomationElement ** parent