QAbstractItemDelegate: fix rect given to tooltip handing

* The rect passed to QToolTip::showText() is in view coordinates,
not in global coordinates.

* Determining this rect in the first place doesn't need calling view->visualRect(index)
The view already did this before calling this method, and stored it in
option.rect, so just use that.

* The widget passed to QToolTip::showText() should be the viewport,
that's what option.rect is relative to (thanks Giuseppe!).

Found these issues when implementing my own tooltip handing (for a subrect
of a delegate) by looking at this code.

Pick-to: 6.3 6.2 5.15
Change-Id: I852e5409def28da98137cd0c4c996083e5e45706
Reviewed-by: Richard Moe Gustavsen <richard.gustavsen@qt.io>
This commit is contained in:
David Faure 2021-03-02 00:05:00 +01:00
parent 1ae30395f3
commit 741afd6752

View File

@ -380,12 +380,7 @@ bool QAbstractItemDelegate::helpEvent(QHelpEvent *event,
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);
QToolTip::showText(he->globalPos(), tooltip, view->viewport(), option.rect);
event->setAccepted(!tooltip.isEmpty());
break;
}