99QTipLabel::QTipLabel(
const QString &text, QPoint pos, QWidget *w,
100 std::chrono::milliseconds displayTime)
101 : QLabel(w, Qt::ToolTip | Qt::BypassGraphicsProxyWidget)
102#if QT_CONFIG(style_stylesheet)
103 , styleSheetParent(
nullptr)
108 setTextFormat(Qt::AutoText);
112 setForegroundRole(QPalette::ToolTipText);
113 setBackgroundRole(QPalette::ToolTipBase);
114 setPalette(QToolTip::palette());
116 setMargin(1 + style()->pixelMetric(QStyle::PM_ToolTipLabelFrameWidth,
nullptr,
this));
117 setFrameStyle(QFrame::NoFrame);
118 setAlignment(Qt::AlignLeft);
120 qApp->installEventFilter(
this);
121 setWindowOpacity(style()->styleHint(QStyle::SH_ToolTipLabel_Opacity,
nullptr,
this) / 255.0);
122 setMouseTracking(
true);
123 reuseTip(text, displayTime, pos);
126void QTipLabel::restartExpireTimer(std::chrono::milliseconds displayTime)
129 std::chrono::milliseconds time;
130 if (displayTime > 0ms) {
133 const qsizetype textLength =
134 d->needTextControl() ? d->control->toPlainText().size() : text().size();
135 time = std::chrono::milliseconds(10000 + 40 * qMax(0, textLength - 100));
137 expireTimer.start(time,
this);
141void QTipLabel::reuseTip(
const QString &text, std::chrono::milliseconds displayTime, QPoint pos)
143#if QT_CONFIG(style_stylesheet)
144 if (styleSheetParent){
145 disconnect(styleSheetParent, &QWidget::destroyed,
146 this, &QTipLabel::styleSheetParentDestroyed);
147 styleSheetParent =
nullptr;
151 setWordWrap(Qt::mightBeRichText(text));
154 restartExpireTimer(displayTime);
157void QTipLabel::updateSize(
const QPoint &pos)
159 d_func()->setScreenForPoint(pos);
161 QFontMetrics fm(font());
164 if (fm.descent() == 2 && fm.ascent() >= 11)
166 QSize sh = sizeHint();
167 const QSize screenSize = getTipScreen(pos,
this)->size();
168 if (!wordWrap() && sh.width() > screenSize.width()) {
172 resize((sh + extra).boundedTo(screenSize));
186void QTipLabel::resizeEvent(QResizeEvent *e)
188 QStyleHintReturnMask frameMask;
190 option.initFrom(
this);
191 if (style()->styleHint(QStyle::SH_ToolTip_Mask, &option,
this, &frameMask))
192 setMask(frameMask.region);
194 QLabel::resizeEvent(e);
197void QTipLabel::mouseMoveEvent(QMouseEvent *e)
199 if (!rect.isNull()) {
200 QPointF pos = e->globalPosition();
202 pos = widget->mapFromGlobal(pos);
203 if (!rect.contains(pos.toPoint()))
206 QLabel::mouseMoveEvent(e);
246bool QTipLabel::eventFilter(QObject *o, QEvent *e)
250 case QEvent::KeyPress:
251 case QEvent::KeyRelease: {
252 const int key =
static_cast<QKeyEvent *>(e)->key();
254 if (key < Qt::Key_Shift || key > Qt::Key_ScrollLock)
255 hideTipImmediately();
264#if defined (Q_OS_QNX) || defined (Q_OS_WASM)
266 case QEvent::WindowActivate:
267 case QEvent::FocusIn:
269 case QEvent::WindowDeactivate:
272 hideTipImmediately();
274 case QEvent::FocusOut:
275 if (
reinterpret_cast<QWindow*>(o) != windowHandle())
277 hideTipImmediately();
280 case QEvent::WindowActivate:
281 case QEvent::WindowDeactivate:
282 case QEvent::FocusIn:
283 case QEvent::FocusOut:
285 case QEvent::MouseButtonPress:
286 case QEvent::MouseButtonRelease:
287 case QEvent::MouseButtonDblClick:
289 hideTipImmediately();
292 case QEvent::MouseMove:
293 if (o == widget && !rect.isNull() && !rect.contains(
static_cast<QMouseEvent*>(e)->position().toPoint()))
309void QTipLabel::placeTip(
const QPoint &pos, QWidget *w)
311#if QT_CONFIG(style_stylesheet)
312 if (testAttribute(Qt::WA_StyleSheet) || (w && qt_styleSheet(w->style()))) {
314 qTipLabel->setProperty(
"_q_stylesheet_parent", QVariant::fromValue(w));
316 qTipLabel->setStyleSheet(
"/* */"_L1);
319 qTipLabel->styleSheetParent = w;
321 connect(w, &QWidget::destroyed,
322 qTipLabel, &QTipLabel::styleSheetParentDestroyed);
328 qTipLabel->updateSize(pos);
333 const QScreen *screen = getTipScreen(pos, w);
335 if (
const QPlatformScreen *platformScreen = screen ? screen->handle() :
nullptr) {
336 QPlatformCursor *cursor = platformScreen->cursor();
338 const QSize nativeSize = cursor ? cursor->size() : QSize(16, 16);
339 const QSize cursorSize = QHighDpi::fromNativePixels(nativeSize,
341 QPoint offset(2, cursorSize.height());
343 if (cursorSize.height() > 2 *
this->height())
344 offset = QPoint(cursorSize.width() / 2, 0);
348#if QT_CONFIG(wayland)
351 if (
auto waylandWindow =
dynamic_cast<QNativeInterface::Private::QWaylandWindow*>(windowHandle()->handle())) {
352 QWidget *parentWindow = w->window();
353 parentWindow->createWinId();
354 windowHandle()->setTransientParent(parentWindow->windowHandle());
358 const QRect controlGeometry = QRect(p.x() - 4, p.y() - 24, 4, 24)
359 .translated(-parentWindow->mapToGlobal(QPoint(0, 0)));
360 waylandWindow->setParentControlGeometry(controlGeometry);
361 waylandWindow->setExtendedWindowType(QNativeInterface::Private::QWaylandWindow::ToolTip);
366 QRect screenRect = screen->availableGeometry();
367 if (p.x() +
this->width() > screenRect.x() + screenRect.width())
368 p.rx() -= 4 +
this->width();
369 if (p.y() +
this->height() > screenRect.y() + screenRect.height())
370 p.ry() -= 24 +
this->height();
371 if (p.y() < screenRect.y())
372 p.setY(screenRect.y());
373 if (p.x() +
this->width() > screenRect.x() + screenRect.width())
374 p.setX(screenRect.x() + screenRect.width() -
this->width());
375 if (p.x() < screenRect.x())
376 p.setX(screenRect.x());
377 if (p.y() +
this->height() > screenRect.y() + screenRect.height())
378 p.setY(screenRect.y() + screenRect.height() -
this->height());
431void QToolTip::showText(
const QPoint &pos,
const QString &text, QWidget *w,
const QRect &rect,
int msecDisplayTime)
433 showText(pos, text, w, rect, std::chrono::milliseconds(msecDisplayTime));
461void QToolTip::showText(QPoint pos,
const QString &text, QWidget *w,
const QRect &rect,
462 std::chrono::milliseconds displayTime)
464 if (qTipLabel && qTipLabel->isVisible()) {
466 qTipLabel->hideTip();
469 if (!qTipLabel->fadingOut) {
472 QPoint localPos = pos;
474 localPos = w->mapFromGlobal(pos);
475 if (qTipLabel->tipChanged(localPos, text, w)) {
476 qTipLabel->reuseTip(text, displayTime, pos);
477 qTipLabel->setTipRect(w, rect);
478 qTipLabel->placeTip(pos, w);
484 if (!text.isEmpty()) {
485 QWidget *tipLabelParent = [w]() -> QWidget* {
495 new QTipLabel(text, pos, tipLabelParent, displayTime);
496 QWidgetPrivate::get(qTipLabel)->setScreen(QTipLabel::getTipScreen(pos, w));
497 qTipLabel->setTipRect(w, rect);
498 qTipLabel->placeTip(pos, w);
499 qTipLabel->setObjectName(
"qtooltip_label"_L1);
501#if QT_CONFIG(effects)
502 if (QApplication::isEffectEnabled(Qt::UI_FadeTooltip))
503 qFadeEffect(qTipLabel);
504 else if (QApplication::isEffectEnabled(Qt::UI_AnimateTooltip))
505 qScrollEffect(qTipLabel);
507 qTipLabel->showNormal();
509 qTipLabel->showNormal();