QGraphicsItem: Fix mouse tracking with modal panels

This fixes the case where a mouse tracking item is added to the
scene while a modal panel is blocking. In order to optimize event
delivery, mouse tracking is enabled for the view only when an
item that needs it is added. This means that we cannot use
itemAcceptsHoverEvents_helper(), since that returns whether the
item _currently_ needs hover events, and we need to know whether it
will need them in the future.

[ChangeLog][QtWidgets][QGraphicsView] Fixed a bug where hover events
would not be delivered if the item was added while blocked by a modal panel.

Fixes: QTBUG-77233
Change-Id: Ifc95869f2cc9c8c048330928ef8a13cd27cfd0f9
Reviewed-by: Volker Hilsheimer <volker.hilsheimer@qt.io>
Reviewed-by: Andreas Aardal Hanssen <andreas@hanssen.name>
This commit is contained in:
Paul Olav Tvete 2019-12-02 17:30:18 +01:00
parent 913146ccd4
commit 5a6fb46488

View File

@ -2568,8 +2568,16 @@ void QGraphicsScene::addItem(QGraphicsItem *item)
++d->selectionChanging;
int oldSelectedItemSize = d->selectedItems.size();
// Enable mouse tracking if the item accepts hover events or has a cursor set.
if (d->allItemsIgnoreHoverEvents && d->itemAcceptsHoverEvents_helper(item)) {
// Enable mouse tracking if we haven't already done so, and the item needs it.
// We cannot use itemAcceptsHoverEvents_helper() here, since we need to enable
// mouse tracking also if this item is temporarily blocked by a modal panel.
auto needsMouseTracking = [](const QGraphicsItemPrivate *item) {
return item->acceptsHover
|| (item->isWidget && static_cast<const QGraphicsWidgetPrivate *>(item)->hasDecoration());
};
if (d->allItemsIgnoreHoverEvents && needsMouseTracking(item->d_ptr.data())) {
d->allItemsIgnoreHoverEvents = false;
d->enableMouseTrackingOnViews();
}