Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
Loading...
Searching...
No Matches
qquicktooltip.cpp
Go to the documentation of this file.
1// Copyright (C) 2017 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
4#include "qquicktooltip_p.h"
5#include "qquickpopup_p_p.h"
7#include "qquickcontrol_p_p.h"
8
9#include <QtCore/qbasictimer.h>
10#include <QtQml/qqmlinfo.h>
11#include <QtQml/qqmlengine.h>
12#include <QtQml/qqmlcontext.h>
13#include <QtQml/qqmlcomponent.h>
14#include <QtQuick/qquickwindow.h>
15
17
22
93{
94 Q_DECLARE_PUBLIC(QQuickToolTip)
95
96public:
97 void startDelay();
98 void stopDelay();
99
100 void startTimeout();
101 void stopTimeout();
102
103 void opened() override;
104
106
107 int delay = 0;
108 int timeout = -1;
112};
113
115{
116 Q_Q(QQuickToolTip);
117 if (delay > 0)
119}
120
125
132
137
143
145 : QQuickPopup(*(new QQuickToolTipPrivate), parent)
146{
147 Q_D(QQuickToolTip);
148 d->allowVerticalFlip = true;
149 d->allowHorizontalFlip = true;
150 d->popupItem->setHoverEnabled(false); // QTBUG-63644
151}
152
159{
160 Q_D(const QQuickToolTip);
161 return d->text;
162}
163
165{
166 Q_D(QQuickToolTip);
167 if (d->text == text)
168 return;
169
170 d->text = text;
173}
174
185{
186 Q_D(const QQuickToolTip);
187 return d->delay;
188}
189
191{
192 Q_D(QQuickToolTip);
193 if (d->delay == delay)
194 return;
195
196 d->delay = delay;
198}
199
210{
211 Q_D(const QQuickToolTip);
212 return d->timeout;
213}
214
216{
217 Q_D(QQuickToolTip);
218 if (d->timeout == timeout)
219 return;
220
221 d->timeout = timeout;
222
223 if (timeout <= 0)
224 d->stopTimeout();
225 else if (isOpened())
226 d->startTimeout();
227
229}
230
232{
233 Q_D(QQuickToolTip);
234 if (visible) {
235 if (!d->visible) {
236 // We are being made visible, and we weren't before.
237 if (d->delay > 0) {
238 d->startDelay();
239 return;
240 }
241 }
242 } else {
243 d->stopDelay();
244 }
246}
247
249{
251 if (!item)
252 qmlWarning(object) << "ToolTip must be attached to an Item";
253
254 return new QQuickToolTipAttached(object);
255}
256
264void QQuickToolTip::show(const QString &text, int ms)
265{
266 if (ms >= 0)
267 setTimeout(ms);
268 setText(text);
269 open();
270}
271
278void QQuickToolTip::hide()
279{
280 close();
281}
282
287
289{
290 Q_D(QQuickToolTip);
292 if (change == QQuickItem::ItemVisibleHasChanged) {
293 if (!data.boolValue)
294 d->stopTimeout();
295
296 QQuickToolTipAttached *attached = qobject_cast<QQuickToolTipAttached *>(qmlAttachedPropertiesObject<QQuickToolTip>(d->parentItem, false));
297 if (attached)
298 emit attached->visibleChanged();
299 }
300}
301
303{
304 Q_D(QQuickToolTip);
305 if (event->timerId() == d->timeoutTimer.timerId()) {
306 d->stopTimeout();
308 return;
309 }
310 if (event->timerId() == d->delayTimer.timerId()) {
311 d->stopDelay();
313 return;
314 }
316}
317
318#if QT_CONFIG(accessibility)
319QAccessible::Role QQuickToolTip::accessibleRole() const
320{
321 return QAccessible::ToolTip;
322}
323
324void QQuickToolTip::accessibilityActiveChanged(bool active)
325{
326 Q_D(QQuickToolTip);
327 QQuickPopup::accessibilityActiveChanged(active);
328
329 if (active)
331}
332#endif
333
335{
336 Q_DECLARE_PUBLIC(QQuickToolTipAttached)
337
338public:
339 QQuickToolTip *instance(bool create) const;
340
341 int delay = 0;
342 int timeout = -1;
344};
345
347{
349 if (!engine)
350 return nullptr;
351
352 static const char *name = "_q_QQuickToolTip";
353
355 if (!tip && create) {
356 // TODO: a cleaner way to create the instance? QQml(Meta)Type?
358 component.setData("import QtQuick.Controls; ToolTip { }", QUrl());
359
360 QObject *object = component.create();
361 if (object)
362 object->setParent(engine);
363
364 tip = qobject_cast<QQuickToolTip *>(object);
365 if (!tip)
366 delete object;
367 else
369 }
370 return tip;
371}
372
377
387{
388 Q_D(const QQuickToolTipAttached);
389 return d->text;
390}
391
393{
395 if (d->text == text)
396 return;
397
398 d->text = text;
400
401 if (isVisible())
402 d->instance(true)->setText(text);
403}
404
414{
415 Q_D(const QQuickToolTipAttached);
416 return d->delay;
417}
418
420{
422 if (d->delay == delay)
423 return;
424
425 d->delay = delay;
427
428 if (isVisible())
429 d->instance(true)->setDelay(delay);
430}
431
441{
442 Q_D(const QQuickToolTipAttached);
443 return d->timeout;
444}
445
447{
449 if (d->timeout == timeout)
450 return;
451
452 d->timeout = timeout;
454
455 if (isVisible())
456 d->instance(true)->setTimeout(timeout);
457}
458
468{
469 Q_D(const QQuickToolTipAttached);
470 QQuickToolTip *tip = d->instance(false);
471 if (!tip)
472 return false;
473
474 return tip->isVisible() && tip->parentItem() == parent();
475}
476
478{
480 if (visible)
481 show(d->text);
482 else
483 hide();
484}
485
495{
496 Q_D(const QQuickToolTipAttached);
497 return d->instance(true);
498}
499
509{
511 QQuickToolTip *tip = d->instance(true);
512 if (!tip)
513 return;
514
515 tip->resetWidth();
516 tip->resetHeight();
517 tip->setParentItem(qobject_cast<QQuickItem *>(parent()));
518 tip->setDelay(d->delay);
519 tip->setTimeout(ms >= 0 ? ms : d->timeout);
520 tip->show(text);
521}
522
531{
533 QQuickToolTip *tip = d->instance(false);
534 if (!tip)
535 return;
536 // check the parent item to prevent unexpectedly closing tooltip by new created invisible tooltip
537 if (parent() == tip->parentItem())
538 tip->close();
539}
540
542
543#include "moc_qquicktooltip_p.cpp"
\inmodule QtCore
Definition qbasictimer.h:18
void start(int msec, QObject *obj)
\obsolete Use chrono overload instead.
void stop()
Stops the timer.
\reentrant
Definition qfont.h:22
QObject * parent
Definition qobject.h:73
\inmodule QtCore
Definition qobject.h:103
QObject * parent() const
Returns a pointer to the parent object.
Definition qobject.h:346
virtual void timerEvent(QTimerEvent *event)
This event handler can be reimplemented in a subclass to receive timer events for the object.
Definition qobject.cpp:1470
QVariant property(const char *name) const
Returns the value of the object's name property.
Definition qobject.cpp:4323
bool setProperty(const char *name, const QVariant &value)
Sets the value of the object's name property to value.
The QPalette class contains color groups for each widget state.
Definition qpalette.h:19
The QQmlComponent class encapsulates a QML component definition.
The QQmlEngine class provides an environment for instantiating QML components.
Definition qqmlengine.h:57
The QQuickItem class provides the most basic of all visual items in \l {Qt Quick}.
Definition qquickitem.h:63
ItemChange
Used in conjunction with QQuickItem::itemChange() to notify the item about certain types of changes.
Definition qquickitem.h:144
@ ItemVisibleHasChanged
Definition qquickitem.h:148
virtual void opened()
void close()
\qmlmethod void QtQuick.Controls::Popup::close()
virtual void itemChange(QQuickItem::ItemChange change, const QQuickItem::ItemChangeData &data)
void open()
\qmlmethod void QtQuick.Controls::Popup::open()
void maybeSetAccessibleName(const QString &name)
virtual void setVisible(bool visible)
static QPalette palette(Scope scope)
static QFont font(Scope scope)
QQuickToolTip * instance(bool create) const
void hide()
\qmlattachedmethod void QtQuick.Controls::ToolTip::hide()
void setTimeout(int timeout)
void show(const QString &text, int ms=-1)
\qmlattachedmethod void QtQuick.Controls::ToolTip::show(string text, int timeout = -1)
void setText(const QString &text)
QQuickToolTip * toolTip
bool isVisible() const
\qmlattachedproperty bool QtQuick.Controls::ToolTip::visible
void setDelay(int delay)
QQuickToolTipAttached(QObject *parent=nullptr)
void setVisible(bool visible)
Provides tool tips for any control.
QPalette defaultPalette() const override
void opened() override
void timerEvent(QTimerEvent *event) override
This event handler can be reimplemented in a subclass to receive timer events for the object.
void setDelay(int delay)
QQuickToolTip(QQuickItem *parent=nullptr)
void delayChanged()
void itemChange(QQuickItem::ItemChange change, const QQuickItem::ItemChangeData &data) override
void setText(const QString &text)
void setTimeout(int timeout)
void setVisible(bool visible) override
static QQuickToolTipAttached * qmlAttachedProperties(QObject *object)
QFont defaultFont() const override
void timeoutChanged()
void textChanged()
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:129
\inmodule QtCore
Definition qcoreevent.h:366
\inmodule QtCore
Definition qurl.h:94
T value() const &
Definition qvariant.h:516
static auto fromValue(T &&value) noexcept(std::is_nothrow_copy_constructible_v< T > &&Private::CanUseInternalSpace< T >) -> std::enable_if_t< std::conjunction_v< std::is_copy_constructible< T >, std::is_destructible< T > >, QVariant >
Definition qvariant.h:536
QString text
Combined button and popup list for selecting options.
GLuint object
[3]
GLbitfield GLuint64 timeout
[4]
GLint GLsizei GLsizei GLenum GLenum GLsizei void * data
GLuint name
struct _cl_event * event
GLdouble GLdouble GLdouble GLdouble q
Definition qopenglext.h:259
static qreal component(const QPointF &point, unsigned int i)
QQmlEngine * qmlEngine(const QObject *obj)
Definition qqml.cpp:80
Q_QML_EXPORT QQmlInfo qmlWarning(const QObject *me)
QQuickItem * qobject_cast< QQuickItem * >(QObject *o)
Definition qquickitem.h:492
#define emit
view show()
[18] //! [19]
QGraphicsItem * item
view create()
QJSEngine engine
[0]
\inmodule QtQuick
Definition qquickitem.h:159