From 0392a744530ac4868e33d5345c7197ac14d7320d Mon Sep 17 00:00:00 2001 From: Christian Ehrlicher Date: Tue, 24 Sep 2019 11:22:50 +0200 Subject: QItemViews: hide ToolTip/What's this when cell has no data for it A QToolTip/QWhatsThis was not hidden when the cursor moved to a cell which does not return valid data for Qt::ToolTip/WhatsThisRole or when the index is not valid. Therefore a wrong information was shown e.g. when the cursor moved from a cell with a tooltip to one without. Fix it by passing an empty string to QToolTip/QWhatsThis::showText(). This syncs the behavior with QGraphicsScene::helpEvent(). Fixes: QTBUG-78722 Change-Id: Ie99fe3b1d35d2f5be41dd65e2fe3173b0cc551b2 Reviewed-by: Richard Moe Gustavsen --- src/widgets/itemviews/qabstractitemdelegate.cpp | 42 +++++++++++++------------ 1 file changed, 22 insertions(+), 20 deletions(-) (limited to 'src/widgets/itemviews') diff --git a/src/widgets/itemviews/qabstractitemdelegate.cpp b/src/widgets/itemviews/qabstractitemdelegate.cpp index 31dde8832b..eecc18e5c7 100644 --- a/src/widgets/itemviews/qabstractitemdelegate.cpp +++ b/src/widgets/itemviews/qabstractitemdelegate.cpp @@ -387,44 +387,46 @@ bool QAbstractItemDelegate::helpEvent(QHelpEvent *event, const QStyleOptionViewItem &option, const QModelIndex &index) { - Q_D(QAbstractItemDelegate); - Q_UNUSED(d); - Q_UNUSED(index); - Q_UNUSED(option); - if (!event || !view) return false; + Q_D(QAbstractItemDelegate); switch (event->type()) { #ifndef QT_NO_TOOLTIP case QEvent::ToolTip: { QHelpEvent *he = static_cast(event); const int precision = inherits("QItemDelegate") ? 10 : 6; // keep in sync with DBL_DIG in qitemdelegate.cpp - const QString tooltip = d->textForRole(Qt::ToolTipRole, index.data(Qt::ToolTipRole), option.locale, precision); - if (!tooltip.isEmpty()) { - QToolTip::showText(he->globalPos(), tooltip, view); - return true; + const QString tooltip = index.isValid() ? + d->textForRole(Qt::ToolTipRole, index.data(Qt::ToolTipRole), option.locale, precision) : + QString(); + QRect rect; + if (index.isValid()) { + const QRect r = view->visualRect(index); + rect = QRect(view->mapToGlobal(r.topLeft()), r.size()); + } + QToolTip::showText(he->globalPos(), tooltip, view, rect); + event->setAccepted(!tooltip.isEmpty()); + break; } - break;} #endif #if QT_CONFIG(whatsthis) - case QEvent::QueryWhatsThis: { - if (index.data(Qt::WhatsThisRole).isValid()) - return true; - break; } + case QEvent::QueryWhatsThis: + event->setAccepted(index.data(Qt::WhatsThisRole).isValid()); + break; case QEvent::WhatsThis: { QHelpEvent *he = static_cast(event); const int precision = inherits("QItemDelegate") ? 10 : 6; // keep in sync with DBL_DIG in qitemdelegate.cpp - const QString whatsthis = d->textForRole(Qt::WhatsThisRole, index.data(Qt::WhatsThisRole), option.locale, precision); - if (!whatsthis.isEmpty()) { - QWhatsThis::showText(he->globalPos(), whatsthis, view); - return true; + const QString whatsthis = index.isValid() ? + d->textForRole(Qt::WhatsThisRole, index.data(Qt::WhatsThisRole), option.locale, precision) : + QString(); + QWhatsThis::showText(he->globalPos(), whatsthis, view); + event->setAccepted(!whatsthis.isEmpty()); + break; } - break ; } #endif default: break; } - return false; + return event->isAccepted(); } /*! -- cgit v1.2.3