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 <richard.gustavsen@qt.io>
bb10
Christian Ehrlicher 2019-09-24 11:22:50 +02:00
parent 1affd453af
commit 0392a74453
1 changed files with 22 additions and 20 deletions

View File

@ -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<QHelpEvent*>(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<QHelpEvent*>(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();
}
/*!