effects: Do not take ownership of QuickEffect::delegate

QuickEffect::setDelegate is exposed QML API.

The lifespan of assigned objects is therefore managed by the QML engine,
and we should be watching for deletion not actually deleting it.
This commit is contained in:
David Edmundson 2024-03-18 10:20:29 +00:00
parent b2a48d09e7
commit e13a30f00f
2 changed files with 7 additions and 6 deletions

View file

@ -65,7 +65,7 @@ public:
}
bool isItemOnScreen(QQuickItem *item, Output *screen) const;
std::unique_ptr<QQmlComponent> delegate;
QPointer<QQmlComponent> delegate;
QUrl source;
std::map<Output *, std::unique_ptr<QQmlContext>> contexts;
std::map<Output *, std::unique_ptr<QQmlIncubator>> incubators;
@ -248,7 +248,7 @@ void QuickSceneEffect::setSource(const QUrl &url)
}
if (d->source != url) {
d->source = url;
d->delegate.reset();
d->delegate.clear();
}
}
@ -265,7 +265,7 @@ void QuickSceneEffect::setDelegate(QQmlComponent *delegate)
}
if (d->delegate.get() != delegate) {
d->source = QUrl();
d->delegate.reset(delegate);
d->delegate = delegate;
Q_EMIT delegateChanged();
}
}
@ -472,11 +472,12 @@ void QuickSceneEffect::startInternal()
return;
}
d->delegate = std::make_unique<QQmlComponent>(effects->qmlEngine());
d->delegate = new QQmlComponent(effects->qmlEngine(), this);
d->delegate->loadUrl(d->source);
if (d->delegate->isError()) {
qWarning().nospace() << "Failed to load " << d->source << ": " << d->delegate->errors();
d->delegate.reset();
d->delegate.clear();
return;
}
Q_EMIT delegateChanged();

View file

@ -158,7 +158,7 @@ OverviewEffect::OverviewEffect()
OverviewConfig::instance(effects->config());
reconfigure(ReconfigureAll);
auto delegate = new QQmlComponent(effects->qmlEngine());
auto delegate = new QQmlComponent(effects->qmlEngine(), this);
connect(delegate, &QQmlComponent::statusChanged, this, [delegate]() {
if (delegate->isError()) {
qWarning() << "Failed to load overview:" << delegate->errorString();