QQuickToolTip: fix destruction of the shared tooltip

Make the QML engine the parent of the shared tooltip instance,
and make sure the instances are per engine. This ensures that
the shared tooltip gets properly destructed when the associated
QML engine is destructed. The same technique is used for the
global styles in QQuickStyleAttached.

Change-Id: I08dcb4f9bc6ddafb7449afe43362e560c0952e88
Reviewed-by: Mitch Curtis <mitch.curtis@qt.io>
This commit is contained in:
J-P Nurmi 2016-08-18 16:12:27 +02:00
parent 7c07c60ff1
commit 93b68f9a12
1 changed files with 12 additions and 1 deletions

View File

@ -325,7 +325,13 @@ public:
QQuickToolTip *QQuickToolTipAttachedPrivate::instance(bool create) const
{
static QPointer<QQuickToolTip> tip;
QQmlEngine *engine = qmlEngine(parent);
if (!engine)
return nullptr;
static const char *name = "_q_QQuickToolTip";
QQuickToolTip *tip = engine->property(name).value<QQuickToolTip *>();
if (!tip && create) {
// TODO: a cleaner way to create the instance? QQml(Meta)Type?
QQmlContext *context = qmlContext(parent);
@ -334,9 +340,14 @@ QQuickToolTip *QQuickToolTipAttachedPrivate::instance(bool create) const
component.setData("import QtQuick.Controls 2.0; ToolTip { }", QUrl());
QObject *object = component.create(context);
if (object)
object->setParent(engine);
tip = qobject_cast<QQuickToolTip *>(object);
if (!tip)
delete object;
else
engine->setProperty(name, QVariant::fromValue(object));
}
}
return tip;