Create QmlEngine lazy in case one is not needed
QQuickWidget may be used with just a root item, and won't need a QmlEngine in that case. So if one isn't given to the constructor, only create one when one is needed for evaluating source. Change-Id: I96cfe5e2473d5d53fc2d52d4646d36c43f4ccb8a Reviewed-by: Laszlo Agocs <laszlo.agocs@qt.io>
This commit is contained in:
parent
b90f810ffa
commit
c9d4c8ed97
|
@ -96,10 +96,7 @@ void QQuickWidgetPrivate::init(QQmlEngine* e)
|
|||
|
||||
engine = e;
|
||||
|
||||
if (engine.isNull())
|
||||
engine = new QQmlEngine(q);
|
||||
|
||||
if (!engine.data()->incubationController())
|
||||
if (!engine.isNull() && !engine.data()->incubationController())
|
||||
engine.data()->setIncubationController(offscreenWindow->incubationController());
|
||||
|
||||
#ifndef QT_NO_DRAGANDDROP
|
||||
|
@ -112,6 +109,16 @@ void QQuickWidgetPrivate::init(QQmlEngine* e)
|
|||
QObject::connect(renderControl, SIGNAL(sceneChanged()), q, SLOT(triggerUpdate()));
|
||||
}
|
||||
|
||||
void QQuickWidgetPrivate::ensureEngine()
|
||||
{
|
||||
Q_Q(QQuickWidget);
|
||||
if (!engine.isNull())
|
||||
return;
|
||||
|
||||
engine = new QQmlEngine(q);
|
||||
engine.data()->setIncubationController(offscreenWindow->incubationController());
|
||||
}
|
||||
|
||||
void QQuickWidgetPrivate::invalidateRenderControl()
|
||||
{
|
||||
if (!context) // this is not an error, could be called before creating the context, or multiple times
|
||||
|
@ -167,10 +174,7 @@ QQuickWidgetPrivate::~QQuickWidgetPrivate()
|
|||
void QQuickWidgetPrivate::execute()
|
||||
{
|
||||
Q_Q(QQuickWidget);
|
||||
if (!engine) {
|
||||
qWarning() << "QQuickWidget: invalid qml engine.";
|
||||
return;
|
||||
}
|
||||
ensureEngine();
|
||||
|
||||
if (root) {
|
||||
delete root;
|
||||
|
@ -404,7 +408,6 @@ QQuickWidget::QQuickWidget(QQmlEngine* engine, QWidget *parent)
|
|||
{
|
||||
setMouseTracking(true);
|
||||
setFocusPolicy(Qt::StrongFocus);
|
||||
Q_ASSERT(engine);
|
||||
d_func()->init(engine);
|
||||
}
|
||||
|
||||
|
@ -548,7 +551,7 @@ QQmlContext* QQuickWidget::rootContext() const
|
|||
QQuickWidget::Status QQuickWidget::status() const
|
||||
{
|
||||
Q_D(const QQuickWidget);
|
||||
if (!d->engine)
|
||||
if (!d->engine && !d->source.isEmpty())
|
||||
return QQuickWidget::Error;
|
||||
|
||||
if (!d->component)
|
||||
|
@ -574,11 +577,12 @@ QList<QQmlError> QQuickWidget::errors() const
|
|||
if (d->component)
|
||||
errs = d->component->errors();
|
||||
|
||||
if (!d->engine) {
|
||||
if (!d->engine && !d->source.isEmpty()) {
|
||||
QQmlError error;
|
||||
error.setDescription(QLatin1String("QQuickWidget: invalid qml engine."));
|
||||
errs << error;
|
||||
} else if (d->component && d->component->status() == QQmlComponent::Ready && !d->root) {
|
||||
}
|
||||
if (d->component && d->component->status() == QQmlComponent::Ready && !d->root) {
|
||||
QQmlError error;
|
||||
error.setDescription(QLatin1String("QQuickWidget: invalid root object."));
|
||||
errs << error;
|
||||
|
|
|
@ -105,6 +105,7 @@ public:
|
|||
QImage grabFramebuffer() Q_DECL_OVERRIDE;
|
||||
|
||||
void init(QQmlEngine* e = 0);
|
||||
void ensureEngine();
|
||||
void handleWindowChange();
|
||||
void invalidateRenderControl();
|
||||
|
||||
|
|
|
@ -55,6 +55,7 @@ private slots:
|
|||
void readback();
|
||||
void renderingSignals();
|
||||
void grabBeforeShow();
|
||||
void nullEngine();
|
||||
};
|
||||
|
||||
|
||||
|
@ -301,6 +302,14 @@ void tst_qquickwidget::grabBeforeShow()
|
|||
QVERIFY(!widget.grab().isNull());
|
||||
}
|
||||
|
||||
void tst_qquickwidget::nullEngine()
|
||||
{
|
||||
QQuickWidget widget;
|
||||
QVERIFY(widget.engine() == Q_NULLPTR);
|
||||
QVERIFY(widget.errors().isEmpty());
|
||||
QCOMPARE(widget.status(), QQuickWidget::Null);
|
||||
}
|
||||
|
||||
QTEST_MAIN(tst_qquickwidget)
|
||||
|
||||
#include "tst_qquickwidget.moc"
|
||||
|
|
Loading…
Reference in New Issue