From 0fe0283308199a6bf4a155627cddd524638c7063 Mon Sep 17 00:00:00 2001 From: J-P Nurmi Date: Thu, 14 Apr 2016 17:29:47 +0200 Subject: ToolTip: use QQmlInfo and don't crash with non-Items Before: ToolTip must be attached to an Item QObject(0x239c3e0) The program has unexpectedly finished. After: qrc:/main.qml:8:5: QML QtObject: ToolTip must be attached to an Item Change-Id: I9a2bf317a13eb7408fc5851087457bbfbeca9f3f Reviewed-by: Mitch Curtis --- src/quicktemplates2/qquicktooltip.cpp | 29 +++++++++++++++++------------ 1 file changed, 17 insertions(+), 12 deletions(-) (limited to 'src/quicktemplates2/qquicktooltip.cpp') diff --git a/src/quicktemplates2/qquicktooltip.cpp b/src/quicktemplates2/qquicktooltip.cpp index 81fde54b..a12b934c 100644 --- a/src/quicktemplates2/qquicktooltip.cpp +++ b/src/quicktemplates2/qquicktooltip.cpp @@ -38,6 +38,7 @@ #include "qquickpopup_p_p.h" #include +#include #include #include #include @@ -323,10 +324,8 @@ void QQuickToolTip::setTimeout(int timeout) QQuickToolTipAttached *QQuickToolTip::qmlAttachedProperties(QObject *object) { QQuickItem *item = qobject_cast(object); - if (!item) { - qWarning() << "ToolTip must be attached to an Item" << object; - return nullptr; - } + if (!item) + qmlInfo(object) << "ToolTip must be attached to an Item"; return new QQuickToolTipAttached(item); } @@ -402,13 +401,15 @@ QQuickToolTip *QQuickToolTipAttachedPrivate::instance(bool create) const if (!tip && create) { // TODO: a cleaner way to create the instance? QQml(Meta)Type? QQmlContext *context = qmlContext(parent); - QQmlComponent component(context->engine()); - component.setData("import Qt.labs.controls 1.0; ToolTip { }", QUrl()); - - QObject *object = component.create(context); - tip = qobject_cast(object); - if (!tip) - delete object; + if (context) { + QQmlComponent component(context->engine()); + component.setData("import Qt.labs.controls 1.0; ToolTip { }", QUrl()); + + QObject *object = component.create(context); + tip = qobject_cast(object); + if (!tip) + delete object; + } } return tip; } @@ -439,7 +440,8 @@ void QQuickToolTipAttached::setText(const QString &text) d->text = text; emit textChanged(); - d->instance(true)->setText(text); + if (QQuickToolTip *tip = d->instance(true)) + tip->setText(text); } /*! @@ -533,6 +535,9 @@ void QQuickToolTipAttached::show(const QString &text, int ms) { Q_D(QQuickToolTipAttached); QQuickToolTip *tip = d->instance(true); + if (!tip) + return; + tip->resetWidth(); tip->resetHeight(); tip->setParentItem(qobject_cast(parent())); -- cgit v1.2.3