11#include <QtCore/qbasictimer.h>
12#include <QtQml/qqmlinfo.h>
13#include <QtQml/qqmlengine.h>
14#include <QtQml/qqmlcontext.h>
15#include <QtQml/qqmlcomponent.h>
16#include <QtQuick/qquickwindow.h>
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
98#ifdef QT_BUILD_INTERNAL
99Q_CONSTINIT Q_AUTOTEST_EXPORT
107#ifdef QT_BUILD_INTERNAL
108Q_CONSTINIT Q_AUTOTEST_EXPORT
116 Q_DECLARE_PUBLIC(QQuickToolTip)
142 if (qt_quicktooltipattachedprivate_delay == -1)
143 qt_quicktooltipattachedprivate_delay = qGuiApp->styleHints()->toolTipWakeUpDelay();
150 delayTimer.start(delay, q);
162 timeoutTimer.start(timeout, q);
172 QQuickPopupPrivate::opened();
181QQuickToolTip::QQuickToolTip(QQuickItem *parent)
182 : QQuickPopup(*(
new QQuickToolTipPrivate), parent)
185 d->allowVerticalFlip =
true;
186 d->allowHorizontalFlip =
true;
187 d->popupItem->setHoverEnabled(
false);
191
192
193
194
195QString QQuickToolTip::text()
const
197 Q_D(
const QQuickToolTip);
201void QQuickToolTip::setText(
const QString &text)
208 maybeSetAccessibleName(text);
213
214
215
216
217
218
219
220
221int QQuickToolTip::delay()
const
223 Q_D(
const QQuickToolTip);
227void QQuickToolTip::setDelay(
int delay)
230 if (d->delay == delay)
238
239
240
241
242
243
244
245
246int QQuickToolTip::timeout()
const
248 Q_D(
const QQuickToolTip);
252void QQuickToolTip::setTimeout(
int timeout)
255 if (d->timeout == timeout)
258 d->timeout = timeout;
265 emit timeoutChanged();
268void QQuickToolTip::setVisible(
bool visible)
282 QQuickPopup::setVisible(visible);
285QQuickToolTipAttached *QQuickToolTip::qmlAttachedProperties(QObject *object)
287 return new QQuickToolTipAttached(object);
291
292
293
294
295
296
297void QQuickToolTip::show(
const QString &text,
int ms)
306
307
308
309
310
311void QQuickToolTip::hide()
316QFont QQuickToolTip::defaultFont()
const
318 return QQuickTheme::font(QQuickTheme::ToolTip);
321void QQuickToolTip::itemChange(QQuickItem::ItemChange change,
const QQuickItem::ItemChangeData &data)
324 QQuickPopup::itemChange(change, data);
325 if (change == QQuickItem::ItemVisibleHasChanged) {
329 QQuickToolTipAttached *attached = qobject_cast<QQuickToolTipAttached *>(qmlAttachedPropertiesObject<QQuickToolTip>(d->parentItem,
false));
331 emit attached->visibleChanged();
335void QQuickToolTip::timerEvent(QTimerEvent *event)
338 if (event->timerId() == d->timeoutTimer.timerId()) {
340 QQuickPopup::setVisible(
false);
343 if (event->timerId() == d->delayTimer.timerId()) {
345 QQuickPopup::setVisible(
true);
348 QQuickPopup::timerEvent(event);
351#if QT_CONFIG(accessibility)
352QAccessible::Role QQuickToolTip::accessibleRole()
const
354 return QAccessible::ToolTip;
357void QQuickToolTip::accessibilityActiveChanged(
bool active)
360 QQuickPopup::accessibilityActiveChanged(active);
363 maybeSetAccessibleName(d->text);
367QQuickToolTip *QQuickToolTipAttachedPrivate::instance(
bool create)
const
369 QQmlEngine *engine = qmlEngine(parent);
375 static const char *name =
"_q_shared_QQuickToolTip";
377 QQuickToolTip *tip = engine->property(name).value<QQuickToolTip *>();
378 if (!tip && create) {
380 QQmlComponent component(engine);
381 component.setData(
"import QtQuick.Controls; ToolTip { }", QUrl());
383 QObject *object = component.create();
385 object->setParent(engine);
387 tip = qobject_cast<QQuickToolTip *>(object);
391 engine->setProperty(name, QVariant::fromValue(object));
396void QQuickToolTipAttachedPrivate::maybeSetVisibleImplicitly(
397 const QObject *attachee,
bool visible)
399 auto *toolTipAttached = qobject_cast<QQuickToolTipAttached *>(
400 qmlAttachedPropertiesObject<QQuickToolTip>(attachee,
false));
402 if (!toolTipAttached)
405 auto *toolTipAttachedPrivate = toolTipAttached->d_func();
408 if (toolTipAttachedPrivate->isVisibleExplicitlySet()
409 || toolTipAttachedPrivate->policy == QQuickToolTip::Manual) {
415 const bool effectiveVisible = visible ? visible && !toolTipAttachedPrivate->text.isEmpty() :
false;
416 toolTipAttachedPrivate->setVisible(effectiveVisible, QQml::PropertyUtils::State::ImplicitlySet);
419void QQuickToolTipAttachedPrivate::setVisible(
bool visible, QQml::PropertyUtils::State propertyState)
421 Q_Q(QQuickToolTipAttached);
422 if (warnIfAttacheeIsNotAnItem(QStringLiteral(
"setVisible")))
425 explicitVisible = isExplicitlySet(propertyState);
427 if (!complete && visible) {
438bool QQuickToolTipAttachedPrivate::isVisibleExplicitlySet()
const
440 return explicitVisible;
443void QQuickToolTipAttachedPrivate::setDelay(
int delay, QQml::PropertyUtils::State propertyState)
445 Q_Q(QQuickToolTipAttached);
446 if (warnIfAttacheeIsNotAnItem(QStringLiteral(
"setDelay")))
449 explicitDelay = isExplicitlySet(propertyState);
451 if (
this->delay == delay)
455 emit q->delayChanged();
458 instance(
true)->setDelay(delay);
461bool QQuickToolTipAttachedPrivate::isDelayExplicitlySet()
const
463 return explicitDelay;
466void QQuickToolTipAttachedPrivate::setTimeout(
int timeout, QQml::PropertyUtils::State propertyState)
468 Q_Q(QQuickToolTipAttached);
469 if (warnIfAttacheeIsNotAnItem(QStringLiteral(
"setTimeout")))
472 explicitTimeout = isExplicitlySet(propertyState);
474 if (
this->timeout == timeout)
477 this->timeout = timeout;
478 emit q->timeoutChanged();
481 instance(
true)->setTimeout(timeout);
484bool QQuickToolTipAttachedPrivate::isTimeoutExplicitlySet()
const
486 return explicitTimeout;
489void QQuickToolTipAttachedPrivate::inheritPolicy(QQuickToolTip::Policy policy)
491 Q_Q(QQuickToolTipAttached);
492 if (
this->policy == policy)
495 this->policy = policy;
497 emit q->policyChanged();
500void QQuickToolTipAttachedPrivate::propagatePolicy()
502 Q_Q(QQuickToolTipAttached);
503 const auto attachedToolTipChildren = q->attachedChildren();
504 for (QtPrivate::QQuickAttachedPropertyPropagator *child : attachedToolTipChildren) {
505 auto *attachedToolTipChild = qobject_cast<QQuickToolTipAttached *>(child);
506 if (attachedToolTipChild)
507 attachedToolTipChild->d_func()->inheritPolicy(policy);
512
513
514
515
516
517
518
519bool QQuickToolTipAttachedPrivate::warnIfAttacheeIsNotAnItem(
const QString &functionName)
521 QQuickItem *item = qobject_cast<QQuickItem *>(parent);
525 qmlWarning(parent).nospace().noquote() <<
"The attached function ToolTip::" << functionName
526 <<
" can only be called when the attachee derives from Item";
530int QQuickToolTipAttachedPrivate::calculateTimeout(
const QString &text)
532 if (Q_UNLIKELY(qt_quicktooltipattachedprivate_short_timeout)) {
538 return 10000 + 40 * qMax(0, text.length() - 100);
541QQuickToolTipAttached::QQuickToolTipAttached(QObject *parent)
542 : QtPrivate::QQuickAttachedPropertyPropagator(*(
new QQuickToolTipAttachedPrivate), parent)
548
549
550
551
552
553
554
555QString QQuickToolTipAttached::text()
const
557 Q_D(
const QQuickToolTipAttached);
561void QQuickToolTipAttached::setText(
const QString &text)
563 Q_D(QQuickToolTipAttached);
564 if (d->warnIfAttacheeIsNotAnItem(QStringLiteral(
"setText")))
573 d->instance(
true)->setText(text);
577
578
579
580
581
582
583
584
585
586
587int QQuickToolTipAttached::delay()
const
589 Q_D(
const QQuickToolTipAttached);
590 return d->explicitDelay || d->policy == QQuickToolTip::Manual ? d->delay
591 : qt_quicktooltipattachedprivate_delay;
594void QQuickToolTipAttached::setDelay(
int delay)
596 Q_D(QQuickToolTipAttached);
597 d->setDelay(delay, QQml::PropertyUtils::State::ExplicitlySet);
601
602
603
604
605
606
607
608
609
610
611int QQuickToolTipAttached::timeout()
const
613 Q_D(
const QQuickToolTipAttached);
614 return d->explicitTimeout || d->policy == QQuickToolTip::Manual ? d->timeout
615 : d->calculateTimeout(d->text);
618void QQuickToolTipAttached::setTimeout(
int timeout)
620 Q_D(QQuickToolTipAttached);
621 d->setTimeout(timeout, QQml::PropertyUtils::State::ExplicitlySet);
625
626
627
628
629
630
631
632bool QQuickToolTipAttached::isVisible()
const
634 Q_D(
const QQuickToolTipAttached);
635 QQuickToolTip *tip = d->instance(
false);
639 return tip->isVisible() && tip->parentItem() == parent();
642void QQuickToolTipAttached::setVisible(
bool visible)
644 Q_D(QQuickToolTipAttached);
645 d->setVisible(visible, QQml::PropertyUtils::State::ExplicitlySet);
649
650
651
652
653
654
655
656QQuickToolTip *QQuickToolTipAttached::toolTip()
const
658 Q_D(
const QQuickToolTipAttached);
659 return d->instance(
true);
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707QQuickToolTip::Policy QQuickToolTipAttached::policy()
const
709 Q_D(
const QQuickToolTipAttached);
713void QQuickToolTipAttached::setVisiblePolicy(QQuickToolTip::Policy policy)
715 Q_D(QQuickToolTipAttached);
716 if (d->policy == policy)
720 d->propagatePolicy();
721 emit policyChanged();
724void QQuickToolTipAttached::resetVisiblePolicy()
726 setVisiblePolicy(QQuickToolTip::Automatic);
730
731
732
733
734
735
736
737void QQuickToolTipAttached::show(
const QString &text,
int ms)
739 Q_D(QQuickToolTipAttached);
740 if (d->warnIfAttacheeIsNotAnItem(QStringLiteral(
"show")))
743 QQuickToolTip *tip = d->instance(
true);
749 tip->setParentItem(qobject_cast<QQuickItem *>(parent()));
750 tip->setDelay(delay());
751 tip->setTimeout(ms >= 0 ? ms : timeout());
756
757
758
759
760
761
762void QQuickToolTipAttached::hide()
764 Q_D(QQuickToolTipAttached);
765 QQuickToolTip *tip = d->instance(
false);
769 if (parent() == tip->parentItem())
773void QQuickToolTipAttached::attachedParentChange(QQuickAttachedPropertyPropagator *newParent,
774 QQuickAttachedPropertyPropagator *)
776 auto *attachedToolTipParent = qobject_cast<QQuickToolTipAttached *>(newParent);
777 if (!attachedToolTipParent)
780 Q_D(QQuickToolTipAttached);
781 d->inheritPolicy(attachedToolTipParent->policy());
784void QQuickToolTipAttached::classBegin()
786 Q_D(QQuickToolTipAttached);
790void QQuickToolTipAttached::componentComplete()
792 Q_D(QQuickToolTipAttached);
795 if (d->pendingShow) {
796 d->pendingShow =
false;
803#include "moc_qquicktooltip_p.cpp"
Combined button and popup list for selecting options.