aboutsummaryrefslogtreecommitdiffstats
path: root/src/quicktemplates2/qquicktooltip.cpp
diff options
context:
space:
mode:
authorJ-P Nurmi <jpnurmi@theqtcompany.com>2016-04-14 17:29:47 +0200
committerJ-P Nurmi <jpnurmi@theqtcompany.com>2016-04-15 09:24:53 +0000
commit0fe0283308199a6bf4a155627cddd524638c7063 (patch)
tree049241510be9a85024525b5053fd67df4aea6ff2 /src/quicktemplates2/qquicktooltip.cpp
parentb2e766c0cf4b5ae027f8d96202a9a9d29a7d84d4 (diff)
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 <mitch.curtis@theqtcompany.com>
Diffstat (limited to 'src/quicktemplates2/qquicktooltip.cpp')
-rw-r--r--src/quicktemplates2/qquicktooltip.cpp29
1 files changed, 17 insertions, 12 deletions
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 <QtCore/qbasictimer.h>
+#include <QtQml/qqmlinfo.h>
#include <QtQml/qqmlengine.h>
#include <QtQml/qqmlcontext.h>
#include <QtQml/qqmlcomponent.h>
@@ -323,10 +324,8 @@ void QQuickToolTip::setTimeout(int timeout)
QQuickToolTipAttached *QQuickToolTip::qmlAttachedProperties(QObject *object)
{
QQuickItem *item = qobject_cast<QQuickItem *>(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<QQuickToolTip *>(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<QQuickToolTip *>(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<QQuickItem *>(parent()));