How to stop click propagation in QGraphicsScene without making ItemIsSelectable?
-
Besides three slices,
QGraphicsItem
, I've anellipse
and atext
in the scene:ellipse = new QGraphicsEllipseItem(); ellipse->setBrush(Qt::white); ellipse->setPen(Qt::NoPen); text = new QGraphicsTextItem(ellipse);
This ellipse with text is added/removed in the scene dynamically on
hoverEvents
of slices. Without any modification in the constructor of PieView,QGraphicsView
, this is what happens when mouse is over the ellipse or clicked:so even though the ellipse is opaque, slices beneath the ellipse receives
hover...Event
and when I click on the ellipse, those slices receivemousePressEvent
. If I change the definition of the ellipse like this:ellipse = new QGraphicsEllipseItem(); ellipse->setBrush(Qt::white); ellipse->setPen(Qt::NoPen); ellipse->setAcceptHoverEvents(true); ellipse->setAcceptedMouseButtons(Qt::AllButtons); ellipse->setFlag(QGraphicsItem::ItemStopsClickFocusPropagation); text = new QGraphicsTextItem(ellipse);
slices beneath the ellipse don't receive
hover...Event
as long as the mouse is over the ellipse BUT they receivemousePressEvent
:One way that I've found works is make the ellipse selectable like this:
ellipse->setFlags(QGraphicsItem::ItemIsSelectable);`
BUT that introduces unnecessary complicacy in code. Is there any other way/flag to stop click propagation when I click on the opaque ellipse?
-
@Emon-Haque
I saw also your other post about this. I don't claim to know what is going on, but I would suggest you look at theeventFilter
onQGraphicsScene
/View
. Everything ought to go through there, so you can debug to see what is going on and (presumably) filter events to stop propagation etc. -
@JonB, It's hard to control the selection mechanism of scene. Depending on where one clicks, scene automatically selects/deselects item. So instead of using the
setSelected/isSelected
in Slice, I've cretedisChecked/setChecked
to control the movements of Slice and removedItemIsSelectable
flag from Slice constructor. In PieView made these changes:scene->setSceneRect(0,0,200,200); ellipse = new QGraphicsEllipseItem(); ellipse->setBrush(Qt::white); ellipse->setPen(Qt::NoPen); ellipse->setAcceptHoverEvents(true); ellipse->setFlag(QGraphicsItem::ItemIsSelectable); text = new QGraphicsTextItem(ellipse); text->setAcceptHoverEvents(false);
the weird movement of slices, when mouse first entered, was solved by setting
setSceneRect
. With these changes, drawings and slice movements are almost perfect. A rectangular dashed border becomes visible when I click on the ellipse:That's weird! In some places I've seen people suggested to subclass the ellipse and override the
paint
BUT that's a lot for such a simple thing. Is there any other way to remove that border around the ellipse?