Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Toggle main menu visibility
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
// Qt-Security score:significant reason:default
4
5
#
include
"qquicktooltip_p.h"
6
#
include
"qquicktooltip_p_p.h"
7
#
include
"qquickpopup_p_p.h"
8
#
include
"qquickpopupitem_p_p.h"
9
#
include
"qquickcontrol_p_p.h"
10
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
>
17
18
#
include
<
QtCore
/
q26numeric
.
h
>
19
20
QT_BEGIN_NAMESPACE
21
22
/*!
23
\qmltype ToolTip
24
\inherits Popup
25
//! \nativetype QQuickToolTip
26
\inqmlmodule QtQuick.Controls
27
\since 5.7
28
\ingroup qtquickcontrols-popups
29
\brief Provides tool tips for any control.
30
31
A tool tip is a short piece of text that informs the user of a control's
32
function. It is typically placed above or below the parent control. The
33
tip text can be any \l{Rich Text Processing}{rich text} formatted string.
34
35
\image qtquickcontrols-tooltip.png
36
{Tooltip displaying helpful text}
37
38
\section2 Attached Tool Tips
39
40
The most straight-forward way to setup tool tips for controls is to
41
specify \l text and \l {visible}{visibility} via attached properties.
42
The following example illustrates this approach:
43
44
\snippet qtquickcontrols-tooltip.qml 1
45
46
Under normal circumstances, there is only one tool tip visible at a time.
47
In order to save resources, all items that use the ToolTip attached property
48
share the same visual tool tip label instance. Even though the visuals are
49
shared, \c text, \c timeout and \c delay are stored individually for each item
50
that uses the respective attached property. However, multiple items cannot
51
make the shared tool tip visible at the same time. The shared tool tip is only
52
shown for the last item that made it visible. The position of the shared tool
53
tip is determined by the framework.
54
55
\include qquicktooltip.qdocinc customize-note
56
57
\section2 Delay and Timeout
58
59
Tool tips are typically transient in a sense that they are shown as a
60
result of a certain external event or user interaction, and they usually
61
hide after a certain timeout. It is possible to control the delay when
62
a tool tip is shown, and the timeout when it is hidden. This makes it
63
possible to implement varying strategies for showing and hiding tool tips.
64
65
For example, on touch screens, it is a common pattern to show a tool tip
66
as a result of pressing and holding down a button. The following example
67
demonstrates how to delay showing a tool tip until the press-and-hold
68
interval is reached. In this example, the tool tip hides as soon as the
69
button is released.
70
71
\snippet qtquickcontrols-tooltip-pressandhold.qml 1
72
73
With pointer devices, however, it might be desired to show a tool tip as
74
a result of hovering a button for a while. The following example presents
75
how to show a tool tip after hovering a button for a second, and hide it
76
after a timeout of five seconds.
77
78
\snippet qtquickcontrols-tooltip-hover.qml 1
79
80
\section2 Custom Tool Tips
81
82
Should one need more fine-grained control over the tool tip position, or
83
multiple simultaneous tool tip instances are needed, it is also possible
84
to create local tool tip instances. This way, it is possible to
85
\l {Customizing ToolTip}{customize} the tool tip, and the whole \l Popup
86
API is available. The following example presents a tool tip that presents
87
the value of a slider when the handle is dragged.
88
89
\image qtquickcontrols-tooltip-slider.png
90
{Tooltip attached to slider showing current value}
91
92
\snippet qtquickcontrols-tooltip-slider.qml 1
93
94
\sa {Customizing ToolTip}, {Popup Controls},
95
{QtQuick.Controls::Popup::closePolicy}{closePolicy}
96
*/
97
98
// These enable auto tests to test the default behaviour of delay and timeout when using
99
// the automatic policy, while also shortening their execution time.
100
#
ifdef
QT_BUILD_INTERNAL
101
Q_CONSTINIT Q_AUTOTEST_EXPORT
102
#
else
103
// Can't be constexpr because we set it in our constructor (which we have to do because
104
// toolTipWakeUpDelay() is not constexpr).
105
static
106
#
endif
107
int
qt_quicktooltipattachedprivate_delay
= -1;
108
109
#
ifdef
QT_BUILD_INTERNAL
110
Q_CONSTINIT Q_AUTOTEST_EXPORT
111
#
else
112
constexpr
113
#
endif
114
bool
qt_quicktooltipattachedprivate_short_timeout
=
false
;
115
116
class
QQuickToolTipPrivate
:
public
QQuickPopupPrivate
117
{
118
Q_DECLARE_PUBLIC(QQuickToolTip)
119
120
public
:
121
QQuickToolTipPrivate
();
122
123
void
startDelay
();
124
void
stopDelay
();
125
126
void
startTimeout
();
127
void
stopTimeout
();
128
129
void
opened
()
override
;
130
131
QPalette
defaultPalette
()
const
override
{
return
QQuickTheme::palette(QQuickTheme::ToolTip); }
132
133
int
delay
= 0;
134
int
timeout
= -1;
135
QString
text
;
136
QBasicTimer
delayTimer
;
137
QBasicTimer
timeoutTimer
;
138
};
139
140
QQuickToolTipPrivate
::QQuickToolTipPrivate()
141
{
142
windowFlags = Qt::ToolTip;
143
if
(
qt_quicktooltipattachedprivate_delay
== -1) {
144
qt_quicktooltipattachedprivate_delay
= q26::saturating_cast<
int
>(
145
qGuiApp->styleHints()->toolTipWakeUpDelay().count()
146
);
147
}
148
#
if
QT_CONFIG
(
wayland
)
149
extendedWindowType = QNativeInterface::Private::QWaylandWindow::ToolTip;
150
#
endif
151
#
if
QT_CONFIG
(
xcb
)
152
wmWindowType = QNativeInterface::Private::QXcbWindow::Tooltip;
153
#
endif
154
155
}
156
157
void
QQuickToolTipPrivate
::
startDelay
()
158
{
159
Q_Q(QQuickToolTip);
160
if
(delay > 0)
161
delayTimer.start(delay, q);
162
}
163
164
void
QQuickToolTipPrivate
::
stopDelay
()
165
{
166
delayTimer.stop();
167
}
168
169
void
QQuickToolTipPrivate
::
startTimeout
()
170
{
171
Q_Q(QQuickToolTip);
172
if
(timeout > 0)
173
timeoutTimer.start(timeout, q);
174
}
175
176
void
QQuickToolTipPrivate
::
stopTimeout
()
177
{
178
timeoutTimer.stop();
179
}
180
181
void
QQuickToolTipPrivate
::
opened
()
182
{
183
QQuickPopupPrivate::opened();
184
startTimeout
(
)
;
185
}
186
187
QQuickToolTip::QQuickToolTip(QQuickItem *parent)
188
: QQuickPopup(*(
new
QQuickToolTipPrivate), parent)
189
{
190
Q_D(QQuickToolTip);
191
d->allowVerticalFlip =
true
;
192
d->allowHorizontalFlip =
true
;
193
d->popupItem->setHoverEnabled(
false
);
// QTBUG-63644
194
}
195
196
/*!
197
\qmlproperty string QtQuick.Controls::ToolTip::text
198
199
This property holds the text shown on the tool tip.
200
*/
201
QString QQuickToolTip::text()
const
202
{
203
Q_D(
const
QQuickToolTip);
204
return
d->text;
205
}
206
207
void
QQuickToolTip::setText(
const
QString &text)
208
{
209
Q_D(QQuickToolTip);
210
if
(d->text == text)
211
return
;
212
213
d->text = text;
214
maybeSetAccessibleName(text);
215
emit textChanged();
216
}
217
218
/*!
219
\qmlproperty int QtQuick.Controls::ToolTip::delay
220
221
This property holds the delay (milliseconds) after which the tool tip is
222
shown. A tooltip with a negative delay is shown immediately. The default
223
value is \c 0.
224
225
\sa {Delay and Timeout}
226
*/
227
int
QQuickToolTip::delay()
const
228
{
229
Q_D(
const
QQuickToolTip);
230
return
d->delay;
231
}
232
233
void
QQuickToolTip::setDelay(
int
delay)
234
{
235
Q_D(QQuickToolTip);
236
if
(d->delay == delay)
237
return
;
238
239
d->delay = delay;
240
emit delayChanged();
241
}
242
243
/*!
244
\qmlproperty int QtQuick.Controls::ToolTip::timeout
245
246
This property holds the timeout (milliseconds) after which the tool tip is
247
hidden. A tooltip with a negative timeout does not hide automatically. The
248
default value is \c -1.
249
250
\sa {Delay and Timeout}
251
*/
252
int
QQuickToolTip::timeout()
const
253
{
254
Q_D(
const
QQuickToolTip);
255
return
d->timeout;
256
}
257
258
void
QQuickToolTip::setTimeout(
int
timeout)
259
{
260
Q_D(QQuickToolTip);
261
if
(d->timeout == timeout)
262
return
;
263
264
d->timeout = timeout;
265
266
if
(timeout <= 0)
267
d->stopTimeout();
268
else
if
(isOpened())
269
d->startTimeout();
270
271
emit timeoutChanged();
272
}
273
274
void
QQuickToolTip::setVisible(
bool
visible)
275
{
276
Q_D(QQuickToolTip);
277
if
(visible) {
278
if
(!d->visible) {
279
// We are being made visible, and we weren't before.
280
if
(d->delay > 0) {
281
d->startDelay();
282
return
;
283
}
284
}
285
}
else
{
286
d->stopDelay();
287
}
288
QQuickPopup::setVisible(visible);
289
}
290
291
QQuickToolTipAttached *QQuickToolTip::qmlAttachedProperties(QObject *object)
292
{
293
return
new
QQuickToolTipAttached(object);
294
}
295
296
/*!
297
\since QtQuick.Controls 2.5 (Qt 5.12)
298
\qmlmethod void QtQuick.Controls::ToolTip::show(string text, int timeout)
299
300
This method shows the \a text as a tooltip, which times out in
301
\a timeout (milliseconds).
302
*/
303
void
QQuickToolTip::show(
const
QString &text,
int
ms)
304
{
305
if
(ms >= 0)
306
setTimeout(ms);
307
setText(text);
308
open();
309
}
310
311
/*!
312
\since QtQuick.Controls 2.5 (Qt 5.12)
313
\qmlmethod void QtQuick.Controls::ToolTip::hide()
314
315
This method hides the tooltip.
316
*/
317
void
QQuickToolTip::hide()
318
{
319
close();
320
}
321
322
QFont QQuickToolTip::defaultFont()
const
323
{
324
return
QQuickTheme::font(QQuickTheme::ToolTip);
325
}
326
327
void
QQuickToolTip::itemChange(QQuickItem::ItemChange change,
const
QQuickItem::ItemChangeData &data)
328
{
329
Q_D(QQuickToolTip);
330
QQuickPopup::itemChange(change, data);
331
if
(change == QQuickItem::ItemVisibleHasChanged) {
332
if
(!data.boolValue)
333
d->stopTimeout();
334
335
QQuickToolTipAttached *attached = qobject_cast<QQuickToolTipAttached *>(qmlAttachedPropertiesObject<QQuickToolTip>(d->parentItem,
false
));
336
if
(attached)
337
emit attached->visibleChanged();
338
}
339
}
340
341
void
QQuickToolTip::timerEvent(QTimerEvent *event)
342
{
343
Q_D(QQuickToolTip);
344
if
(event->timerId() == d->timeoutTimer.timerId()) {
345
d->stopTimeout();
346
QQuickPopup::setVisible(
false
);
347
return
;
348
}
349
if
(event->timerId() == d->delayTimer.timerId()) {
350
d->stopDelay();
351
QQuickPopup::setVisible(
true
);
352
return
;
353
}
354
QQuickPopup::timerEvent(event);
355
}
356
357
#
if
QT_CONFIG
(
accessibility
)
358
QAccessible::Role QQuickToolTip::accessibleRole()
const
359
{
360
return
QAccessible::ToolTip;
361
}
362
363
void
QQuickToolTip::accessibilityActiveChanged(
bool
active)
364
{
365
Q_D(QQuickToolTip);
366
QQuickPopup::accessibilityActiveChanged(active);
367
368
if
(active)
369
maybeSetAccessibleName(d->text);
370
}
371
#
endif
372
373
QQuickToolTip *QQuickToolTipAttachedPrivate::instance(
bool
create)
const
374
{
375
QQmlEngine *engine = qmlEngine(parent);
376
if
(!engine)
377
return
nullptr
;
378
379
// QQuickAttachedPropertyPropagator uses "_q_QQuickToolTip", so we add "shared_"
380
// to make this unique.
381
static
const
char
*name =
"_q_shared_QQuickToolTip"
;
382
383
QQuickToolTip *tip = engine->property(name).value<QQuickToolTip *>();
384
if
(!tip && create) {
385
QQmlComponent component(engine,
"QtQuick.Controls"
,
"ToolTip"
);
386
387
QObject *object = component.create();
388
if
(object)
389
object->setParent(engine);
390
391
tip = qobject_cast<QQuickToolTip *>(object);
392
if
(!tip)
393
delete
object;
394
else
395
engine->setProperty(name, QVariant::fromValue(object));
396
}
397
return
tip;
398
}
399
400
void
QQuickToolTipAttachedPrivate::maybeSetVisibleImplicitly(
401
const
QObject *attachee,
bool
visible)
402
{
403
auto
*toolTipAttached = qobject_cast<QQuickToolTipAttached *>(
404
qmlAttachedPropertiesObject<QQuickToolTip>(attachee,
false
));
405
// Not using an attached tool tip.
406
if
(!toolTipAttached)
407
return
;
408
409
auto
*toolTipAttachedPrivate = toolTipAttached->d_func();
410
// Don't interfere if the user has set ToolTip.visible explicitly or if they've
411
// set a manual policy.
412
if
(toolTipAttachedPrivate->isVisibleExplicitlySet()
413
|| toolTipAttachedPrivate->policy == QQuickToolTip::Manual) {
414
return
;
415
}
416
417
// Don't show ourselves if we have no text. We save calling code having to get our text and
418
// instead just test it here.
419
const
bool
effectiveVisible = visible ? visible && !toolTipAttachedPrivate->text.isEmpty() :
false
;
420
toolTipAttachedPrivate->setVisible(effectiveVisible, QQml::PropertyUtils::State::ImplicitlySet);
421
}
422
423
void
QQuickToolTipAttachedPrivate::setVisible(
bool
visible, QQml::PropertyUtils::State propertyState)
424
{
425
Q_Q(QQuickToolTipAttached);
426
if
(warnIfAttacheeIsNotAnItem(QStringLiteral(
"setVisible"
)))
427
return
;
428
429
explicitVisible = isExplicitlySet(propertyState);
430
431
if
(!complete && visible) {
432
pendingShow =
true
;
433
return
;
434
}
435
436
if
(visible)
437
q->show(text);
438
else
439
q->hide();
440
}
441
442
bool
QQuickToolTipAttachedPrivate::isVisibleExplicitlySet()
const
443
{
444
return
explicitVisible;
445
}
446
447
void
QQuickToolTipAttachedPrivate::setDelay(
int
delay, QQml::PropertyUtils::State propertyState)
448
{
449
Q_Q(QQuickToolTipAttached);
450
if
(warnIfAttacheeIsNotAnItem(QStringLiteral(
"setDelay"
)))
451
return
;
452
453
explicitDelay = isExplicitlySet(propertyState);
454
455
if
(
this
->delay == delay)
456
return
;
457
458
this
->delay = delay;
459
emit q->delayChanged();
460
461
if
(q->isVisible())
462
instance(
true
)->setDelay(delay);
463
}
464
465
bool
QQuickToolTipAttachedPrivate::isDelayExplicitlySet()
const
466
{
467
return
explicitDelay;
468
}
469
470
void
QQuickToolTipAttachedPrivate::setTimeout(
int
timeout, QQml::PropertyUtils::State propertyState)
471
{
472
Q_Q(QQuickToolTipAttached);
473
if
(warnIfAttacheeIsNotAnItem(QStringLiteral(
"setTimeout"
)))
474
return
;
475
476
explicitTimeout = isExplicitlySet(propertyState);
477
478
if
(
this
->timeout == timeout)
479
return
;
480
481
this
->timeout = timeout;
482
emit q->timeoutChanged();
483
484
if
(q->isVisible())
485
instance(
true
)->setTimeout(timeout);
486
}
487
488
bool
QQuickToolTipAttachedPrivate::isTimeoutExplicitlySet()
const
489
{
490
return
explicitTimeout;
491
}
492
493
void
QQuickToolTipAttachedPrivate::inheritPolicy(QQuickToolTip::Policy policy)
494
{
495
Q_Q(QQuickToolTipAttached);
496
if
(
this
->policy == policy)
497
return
;
498
499
this
->policy = policy;
500
propagatePolicy();
501
emit q->policyChanged();
502
}
503
504
void
QQuickToolTipAttachedPrivate::propagatePolicy()
505
{
506
Q_Q(QQuickToolTipAttached);
507
const
auto
attachedToolTipChildren = q->attachedChildren();
508
for
(QtPrivate::QQuickAttachedPropertyPropagator *child : attachedToolTipChildren) {
509
auto
*attachedToolTipChild = qobject_cast<QQuickToolTipAttached *>(child);
510
if
(attachedToolTipChild)
511
attachedToolTipChild->d_func()->inheritPolicy(policy);
512
}
513
}
514
515
/*!
516
\internal
517
518
We used to warn that the ToolTip attached property must be attached to an object deriving
519
from Item. That made sense before the introduction of ToolTip.policy, but now we need to
520
be able to set a policy on e.g. ApplicationWindow and have it propagate down to the rest
521
of the scene. So instead of warning in the constructor, we warn in the individual functions.
522
*/
523
bool
QQuickToolTipAttachedPrivate::warnIfAttacheeIsNotAnItem(
const
QString &functionName)
524
{
525
QQuickItem *item = qobject_cast<QQuickItem *>(parent);
526
if
(Q_LIKELY(item))
527
return
false
;
528
529
qmlWarning(parent).nospace().noquote() <<
"The attached function ToolTip::"
<< functionName
530
<<
" can only be called when the attachee derives from Item"
;
531
return
true
;
532
}
533
534
int
QQuickToolTipAttachedPrivate::calculateTimeout(
const
QString &text)
535
{
536
if
(Q_UNLIKELY(qt_quicktooltipattachedprivate_short_timeout)) {
537
// For auto tests, to ensure that the default automatic timeout works.
538
return
123;
539
}
540
541
// Based on QTipLabel::restartExpireTimer.
542
return
10000 + 40 * qMax(0, text.length() - 100);
543
}
544
545
QQuickToolTipAttached::QQuickToolTipAttached(QObject *parent)
546
: QtPrivate::QQuickAttachedPropertyPropagator(*(
new
QQuickToolTipAttachedPrivate), parent)
547
{
548
initialize();
549
}
550
551
/*!
552
\qmlattachedproperty string QtQuick.Controls::ToolTip::text
553
554
This attached property holds the text of the shared tool tip.
555
The property can be attached to any item.
556
557
\sa {Attached Tool Tips}
558
*/
559
QString QQuickToolTipAttached::text()
const
560
{
561
Q_D(
const
QQuickToolTipAttached);
562
return
d->text;
563
}
564
565
void
QQuickToolTipAttached::setText(
const
QString &text)
566
{
567
Q_D(QQuickToolTipAttached);
568
if
(d->warnIfAttacheeIsNotAnItem(QStringLiteral(
"setText"
)))
569
return
;
570
if
(d->text == text)
571
return
;
572
573
d->text = text;
574
emit textChanged();
575
576
if
(isVisible())
577
d->instance(
true
)->setText(text);
578
}
579
580
/*!
581
\qmlattachedproperty int QtQuick.Controls::ToolTip::delay
582
583
This attached property holds the delay (milliseconds) of the shared tool tip.
584
The property can be attached to any item.
585
586
The default value is \c 0 if \l policy is \c ToolTip.Manual, otherwise it
587
is \l QStyleHints::toolTipWakeUpDelay().
588
589
\sa {Attached Tool Tips}, {Delay and Timeout}
590
*/
591
int
QQuickToolTipAttached::delay()
const
592
{
593
Q_D(
const
QQuickToolTipAttached);
594
return
d->explicitDelay || d->policy == QQuickToolTip::Manual ? d->delay
595
: qt_quicktooltipattachedprivate_delay;
596
}
597
598
void
QQuickToolTipAttached::setDelay(
int
delay)
599
{
600
Q_D(QQuickToolTipAttached);
601
d->setDelay(delay, QQml::PropertyUtils::State::ExplicitlySet);
602
}
603
604
/*!
605
\qmlattachedproperty int QtQuick.Controls::ToolTip::timeout
606
607
This attached property holds the timeout (milliseconds) of the shared tool tip.
608
The property can be attached to any item.
609
610
The default value is based on the length of the text, and will always be
611
at least 10 seconds long.
612
613
\sa {Attached Tool Tips}, {Delay and Timeout}
614
*/
615
int
QQuickToolTipAttached::timeout()
const
616
{
617
Q_D(
const
QQuickToolTipAttached);
618
return
d->explicitTimeout || d->policy == QQuickToolTip::Manual ? d->timeout
619
: d->calculateTimeout(d->text);
620
}
621
622
void
QQuickToolTipAttached::setTimeout(
int
timeout)
623
{
624
Q_D(QQuickToolTipAttached);
625
d->setTimeout(timeout, QQml::PropertyUtils::State::ExplicitlySet);
626
}
627
628
/*!
629
\qmlattachedproperty bool QtQuick.Controls::ToolTip::visible
630
631
This attached property holds whether the shared tool tip is visible.
632
The property can be attached to any item.
633
634
\sa {Attached Tool Tips}
635
*/
636
bool
QQuickToolTipAttached::isVisible()
const
637
{
638
Q_D(
const
QQuickToolTipAttached);
639
QQuickToolTip *tip = d->instance(
false
);
640
if
(!tip)
641
return
false
;
642
643
return
tip->isVisible() && tip->parentItem() == parent();
644
}
645
646
void
QQuickToolTipAttached::setVisible(
bool
visible)
647
{
648
Q_D(QQuickToolTipAttached);
649
d->setVisible(visible, QQml::PropertyUtils::State::ExplicitlySet);
650
}
651
652
/*!
653
\qmlattachedproperty ToolTip QtQuick.Controls::ToolTip::toolTip
654
655
This attached property holds the shared tool tip instance. The property
656
can be attached to any item.
657
658
\sa {Attached Tool Tips}
659
*/
660
QQuickToolTip *QQuickToolTipAttached::toolTip()
const
661
{
662
Q_D(
const
QQuickToolTipAttached);
663
return
d->instance(
true
);
664
}
665
666
/*!
667
\qmlattachedproperty enumeration QtQuick.Controls::ToolTip::policy
668
\since 6.12
669
670
This attached property controls whether the visibility of the
671
\l {toolTip}{shared tool tip instance} is handled automatically.
672
It only has an effect for items on which \c ToolTip.visible \e {has not}
673
been set. Only items that set \c ToolTip.text will be made visible. It only
674
has an effect for the following items and their derived types: \l Control,
675
\l TextArea and \l TextField. All other types require \l visible to be
676
manually set.
677
678
It also determines the default values for the delay and timeout properties
679
of the shared tool tip instance (regardless of whether \c ToolTip.visible
680
or \c ToolTip.text have been set).
681
682
This property is propagated to attached ToolTip children.
683
684
Available values:
685
\value ToolTip.Automatic The shared tool tip will be shown when the
686
attachee is hovered or long-pressed (if triggered by touch and the
687
attachee is an \l AbstractButton or one of its derived types). If
688
the visible property has been explicitly set, or the text property has
689
not been set, this value has no effect, and the behavior will be
690
equivalent to \c ToolTip.Manual.
691
The shared tool tip will also default to platform-specific values
692
for its delay and timeout properties.
693
\value ToolTip.Manual The shared tool tip will not be shown automatically,
694
and the developer is responsible for setting the visible property.
695
The shared tool tip will not default to platform-specific values
696
for its delay and timeout properties.
697
698
The default value is \c {ToolTip.Automatic}.
699
700
The property provides a way to opt-out of the default values for shared
701
tool tips introduced in Qt 6.12. This is particularly relevant for legacy
702
code that doesn't explicitly set the visible property before an item is
703
interacted with. For applications that declaratively set the visible
704
property and find the new default values for delay and timeout acceptable,
705
\c policy is not needed.
706
707
\sa {Attached Tool Tips}
708
*/
709
QQuickToolTip::Policy QQuickToolTipAttached::policy()
const
710
{
711
Q_D(
const
QQuickToolTipAttached);
712
return
d->policy;
713
}
714
715
void
QQuickToolTipAttached::setVisiblePolicy(QQuickToolTip::Policy policy)
716
{
717
Q_D(QQuickToolTipAttached);
718
if
(d->policy == policy)
719
return
;
720
721
d->policy = policy;
722
d->propagatePolicy();
723
emit policyChanged();
724
}
725
726
void
QQuickToolTipAttached::resetVisiblePolicy()
727
{
728
setVisiblePolicy(QQuickToolTip::Automatic);
729
}
730
731
/*!
732
\qmlattachedmethod void QtQuick.Controls::ToolTip::show(string text, int timeout = -1)
733
734
This attached method shows the shared tooltip with \a text and \a timeout (milliseconds).
735
The method can be attached to any item.
736
737
\sa {Attached Tool Tips}
738
*/
739
void
QQuickToolTipAttached::show(
const
QString &text,
int
ms)
740
{
741
Q_D(QQuickToolTipAttached);
742
if
(d->warnIfAttacheeIsNotAnItem(QStringLiteral(
"show"
)))
743
return
;
744
745
QQuickToolTip *tip = d->instance(
true
);
746
if
(!tip)
747
return
;
748
749
tip->resetWidth();
750
tip->resetHeight();
751
tip->setParentItem(qobject_cast<QQuickItem *>(parent()));
752
tip->setDelay(delay());
753
tip->setTimeout(ms >= 0 ? ms : timeout());
754
tip->show(text);
755
}
756
757
/*!
758
\qmlattachedmethod void QtQuick.Controls::ToolTip::hide()
759
760
This attached method hides the shared tooltip. The method can be attached to any item.
761
762
\sa {Attached Tool Tips}
763
*/
764
void
QQuickToolTipAttached::hide()
765
{
766
Q_D(QQuickToolTipAttached);
767
QQuickToolTip *tip = d->instance(
false
);
768
if
(!tip)
769
return
;
770
// check the parent item to prevent unexpectedly closing tooltip by new created invisible tooltip
771
if
(parent() == tip->parentItem())
772
tip->close();
773
}
774
775
void
QQuickToolTipAttached::attachedParentChange(QQuickAttachedPropertyPropagator *newParent,
776
QQuickAttachedPropertyPropagator *
/*oldParent*/
)
777
{
778
auto
*attachedToolTipParent = qobject_cast<QQuickToolTipAttached *>(newParent);
779
if
(!attachedToolTipParent)
780
return
;
781
782
Q_D(QQuickToolTipAttached);
783
d->inheritPolicy(attachedToolTipParent->policy());
784
}
785
786
void
QQuickToolTipAttached::classBegin()
787
{
788
Q_D(QQuickToolTipAttached);
789
d->complete =
false
;
790
}
791
792
void
QQuickToolTipAttached::componentComplete()
793
{
794
Q_D(QQuickToolTipAttached);
795
d->complete =
true
;
796
797
if
(d->pendingShow) {
798
d->pendingShow =
false
;
799
show(d->text);
800
}
801
}
802
803
QT_END_NAMESPACE
804
805
#
include
"moc_qquicktooltip_p.cpp"
QQuickToolTipPrivate
Definition
qquicktooltip.cpp:117
QQuickToolTipPrivate::defaultPalette
QPalette defaultPalette() const override
Definition
qquicktooltip.cpp:131
QQuickToolTipPrivate::timeoutTimer
QBasicTimer timeoutTimer
Definition
qquicktooltip.cpp:137
QQuickToolTipPrivate::delay
int delay
Definition
qquicktooltip.cpp:133
QQuickToolTipPrivate::opened
void opened() override
Definition
qquicktooltip.cpp:181
QQuickToolTipPrivate::startDelay
void startDelay()
Definition
qquicktooltip.cpp:157
QQuickToolTipPrivate::stopTimeout
void stopTimeout()
Definition
qquicktooltip.cpp:176
QQuickToolTipPrivate::startTimeout
void startTimeout()
Definition
qquicktooltip.cpp:169
QQuickToolTipPrivate::stopDelay
void stopDelay()
Definition
qquicktooltip.cpp:164
QQuickToolTipPrivate::timeout
int timeout
Definition
qquicktooltip.cpp:134
QQuickToolTipPrivate::delayTimer
QBasicTimer delayTimer
Definition
qquicktooltip.cpp:136
QQuickToolTipPrivate::text
QString text
Definition
qquicktooltip.cpp:135
QT_BEGIN_NAMESPACE
Combined button and popup list for selecting options.
Definition
qsequentialanimationgroup.cpp:47
qt_quicktooltipattachedprivate_delay
static QT_BEGIN_NAMESPACE int qt_quicktooltipattachedprivate_delay
Provides tool tips for any control.
Definition
qquicktooltip.cpp:107
qt_quicktooltipattachedprivate_short_timeout
constexpr bool qt_quicktooltipattachedprivate_short_timeout
Definition
qquicktooltip.cpp:114
qtdeclarative
src
quicktemplates
qquicktooltip.cpp
Generated on
for Qt by
1.18.0