From 5a6fb46488ebc26b79b9d37a7f91969e0d852b4f Mon Sep 17 00:00:00 2001 From: Paul Olav Tvete Date: Mon, 2 Dec 2019 17:30:18 +0100 Subject: [PATCH] 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 Reviewed-by: Andreas Aardal Hanssen --- src/widgets/graphicsview/qgraphicsscene.cpp | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/src/widgets/graphicsview/qgraphicsscene.cpp b/src/widgets/graphicsview/qgraphicsscene.cpp index a47d0d879d..d641d17232 100644 --- a/src/widgets/graphicsview/qgraphicsscene.cpp +++ b/src/widgets/graphicsview/qgraphicsscene.cpp @@ -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(item)->hasDecoration()); + }; + + if (d->allItemsIgnoreHoverEvents && needsMouseTracking(item->d_ptr.data())) { d->allItemsIgnoreHoverEvents = false; d->enableMouseTrackingOnViews(); }