diff options
Diffstat (limited to 'src/widgets/kernel/qtooltip.cpp')
-rw-r--r-- | src/widgets/kernel/qtooltip.cpp | 95 |
1 files changed, 34 insertions, 61 deletions
diff --git a/src/widgets/kernel/qtooltip.cpp b/src/widgets/kernel/qtooltip.cpp index b532ac8d3c..35eaa8042a 100644 --- a/src/widgets/kernel/qtooltip.cpp +++ b/src/widgets/kernel/qtooltip.cpp @@ -1,41 +1,5 @@ -/**************************************************************************** -** -** Copyright (C) 2016 The Qt Company Ltd. -** Contact: https://www.qt.io/licensing/ -** -** This file is part of the QtWidgets module of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:LGPL$ -** Commercial License Usage -** Licensees holding valid commercial Qt licenses may use this file in -** accordance with the commercial license agreement provided with the -** Software or, alternatively, in accordance with the terms contained in -** a written agreement between you and The Qt Company. For licensing terms -** and conditions see https://www.qt.io/terms-conditions. For further -** information use the contact form at https://www.qt.io/contact-us. -** -** GNU Lesser General Public License Usage -** Alternatively, this file may be used under the terms of the GNU Lesser -** General Public License version 3 as published by the Free Software -** Foundation and appearing in the file LICENSE.LGPL3 included in the -** packaging of this file. Please review the following information to -** ensure the GNU Lesser General Public License version 3 requirements -** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. -** -** GNU General Public License Usage -** Alternatively, this file may be used under the terms of the GNU -** General Public License version 2.0 or (at your option) the GNU General -** Public license version 3 or any later version approved by the KDE Free -** Qt Foundation. The licenses are as published by the Free Software -** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 -** included in the packaging of this file. Please review the following -** information to ensure the GNU General Public License requirements will -** be met: https://www.gnu.org/licenses/gpl-2.0.html and -** https://www.gnu.org/licenses/gpl-3.0.html. -** -** $QT_END_LICENSE$ -** -****************************************************************************/ +// Copyright (C) 2016 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only #include <QtWidgets/private/qtwidgetsglobal_p.h> @@ -62,6 +26,8 @@ QT_BEGIN_NAMESPACE +using namespace Qt::StringLiterals; + /*! \class QToolTip @@ -79,15 +45,24 @@ QT_BEGIN_NAMESPACE Rich text displayed in a tool tip is implicitly word-wrapped unless specified differently with \c{<p style='white-space:pre'>}. - The simplest and most common way to set a widget's tool tip is by - calling its QWidget::setToolTip() function. + UI elements that are created via \l{QAction} use the tooltip property + of the QAction, so for most interactive UI elements, setting that + property is the easiest way to provide tool tips. + + \snippet tooltips/main.cpp action_tooltip + + For any other widgets, the simplest and most common way to set + a widget's tool tip is by calling its QWidget::setToolTip() function. + + \snippet tooltips/main.cpp static_tooltip It is also possible to show different tool tips for different regions of a widget, by using a QHelpEvent of type QEvent::ToolTip. Intercept the help event in your widget's \l {QWidget::}{event()} function and call QToolTip::showText() with - the text you want to display. The \l{widgets/tooltips}{Tooltips} - example illustrates this technique. + the text you want to display. + + \snippet tooltips/main.cpp dynamic_tooltip If you are calling QToolTip::hideText(), or QToolTip::showText() with an empty string, as a result of a \l{QEvent::}{ToolTip}-event you @@ -109,7 +84,7 @@ QT_BEGIN_NAMESPACE \note Tool tips use the inactive color group of QPalette, because tool tips are not active windows. - \sa QWidget::toolTip, QAction::toolTip, {Tool Tips Example} + \sa QWidget::toolTip, QAction::toolTip */ class QTipLabel : public QLabel @@ -147,7 +122,7 @@ protected: #ifndef QT_NO_STYLE_STYLESHEET public slots: /** \internal - Cleanup the _q_stylesheet_parent propery. + Cleanup the _q_stylesheet_parent property. */ void styleSheetParentDestroyed() { setProperty("_q_stylesheet_parent", QVariant()); @@ -191,7 +166,9 @@ QTipLabel::QTipLabel(const QString &text, const QPoint &pos, QWidget *w, int mse void QTipLabel::restartExpireTimer(int msecDisplayTime) { - int time = 10000 + 40 * qMax(0, text().length()-100); + Q_D(const QLabel); + const qsizetype textLength = d->needTextControl() ? d->control->toPlainText().size() : text().size(); + qsizetype time = 10000 + 40 * qMax(0, textLength - 100); if (msecDisplayTime > 0) time = msecDisplayTime; expireTimer.start(time, this); @@ -202,8 +179,8 @@ void QTipLabel::reuseTip(const QString &text, int msecDisplayTime, const QPoint { #ifndef QT_NO_STYLE_STYLESHEET if (styleSheetParent){ - disconnect(styleSheetParent, SIGNAL(destroyed()), - QTipLabel::instance, SLOT(styleSheetParentDestroyed())); + disconnect(styleSheetParent, &QWidget::destroyed, + this, &QTipLabel::styleSheetParentDestroyed); styleSheetParent = nullptr; } #endif @@ -342,13 +319,6 @@ bool QTipLabel::eventFilter(QObject *o, QEvent *e) case QEvent::FocusIn: case QEvent::FocusOut: #endif - case QEvent::Close: - // For QTBUG-55523 (QQC) specifically: Hide tooltip when windows are closed, - // unless the close event is for the this tooltip, in which case we don't - // recursively hide/close again. - if (o != windowHandle() && o != this) - hideTipImmediately(); - break; case QEvent::MouseButtonPress: case QEvent::MouseButtonRelease: case QEvent::MouseButtonDblClick: @@ -359,6 +329,7 @@ bool QTipLabel::eventFilter(QObject *o, QEvent *e) case QEvent::MouseMove: if (o == widget && !rect.isNull() && !rect.contains(static_cast<QMouseEvent*>(e)->position().toPoint())) hideTip(); + break; default: break; } @@ -379,17 +350,19 @@ void QTipLabel::placeTip(const QPoint &pos, QWidget *w) //the stylesheet need to know the real parent QTipLabel::instance->setProperty("_q_stylesheet_parent", QVariant::fromValue(w)); //we force the style to be the QStyleSheetStyle, and force to clear the cache as well. - QTipLabel::instance->setStyleSheet(QLatin1String("/* */")); + QTipLabel::instance->setStyleSheet("/* */"_L1); // Set up for cleaning up this later... QTipLabel::instance->styleSheetParent = w; if (w) { - connect(w, SIGNAL(destroyed()), - QTipLabel::instance, SLOT(styleSheetParentDestroyed())); - // QTBUG-64550: A font inherited by the style sheet might change the size, - // particular on Windows, where the tip is not parented on a window. - QTipLabel::instance->updateSize(pos); + connect(w, &QWidget::destroyed, + QTipLabel::instance, &QTipLabel::styleSheetParentDestroyed); } + // QTBUG-64550: A font inherited by the style sheet might change the size, + // particular on Windows, where the tip is not parented on a window. + // The updatesSize() also makes sure that the content size be updated with + // correct content margin. + QTipLabel::instance->updateSize(pos); } #endif //QT_NO_STYLE_STYLESHEET @@ -499,7 +472,7 @@ void QToolTip::showText(const QPoint &pos, const QString &text, QWidget *w, cons QWidgetPrivate::get(QTipLabel::instance)->setScreen(QTipLabel::getTipScreen(pos, w)); QTipLabel::instance->setTipRect(w, rect); QTipLabel::instance->placeTip(pos, w); - QTipLabel::instance->setObjectName(QLatin1String("qtooltip_label")); + QTipLabel::instance->setObjectName("qtooltip_label"_L1); #if QT_CONFIG(effects) if (QApplication::isEffectEnabled(Qt::UI_FadeTooltip)) |