GraphicsView::contextMenuEvent(QContextMenuEvent*) conficts with LineEdit::contextMenuEvent
-
Hi all. I have a custom
Graphics{View, Scene}
and an embeddedLineEdit
to input some numbers.Because I need to right-click on my
LineEdit
and be able to copy, paste etc, I usedLineEdit::contextMenuEvent
:void LineEdit::contextMenuEvent( QContextMenuEvent* event ) { QMenu* menu = createStandardContextMenu(); menu->setStyleSheet( normalStyleSheet ); menu->exec( event->globalPos() ); delete menu; }
I don't forward the event to
QLineEdit
though and it works beautifully.At the same time, I want to right-click on the
GraphicsView
and select some actions related to my view.void GraphicsView::contextMenuEvent( QContextMenuEvent* event ) { // .... menu.exec( event->globalPos() ); QGraphicsView::contextMenuEvent(event); }
Now, the problem occurs when I right click on
LineEdit
object. I get LineEdit's menu and after is closedGraphicsView
's too.How can I let my
GraphicsView
know that when the right click is on top of another widget (let's sayLineEdit
), it will ignore the event for itself. Maybe an event filter is a better alternative?Thank you in advance.
-
@Petross404_Petros-S said in GraphicsView::contextMenuEvent(QContextMenuEvent*) conficts with LineEdit::contextMenuEvent:
Now, the problem occurs when I right click on LineEdit object. I get LineEdit's menu and after is closed GraphicsView's too.
Not sure but: did you try putting
event->accept()
at the end ofvoid LineEdit::contextMenuEvent( QContextMenuEvent* event )
, to indicate your handler has dealt with the event and does not want it passed on? -
Thanks for the answer. I did just now and I am afraid it doesn't help.
-
@Petross404_Petros-S said in GraphicsView::contextMenuEvent(QContextMenuEvent*) conficts with LineEdit::contextMenuEvent:
void GraphicsView::contextMenuEvent( QContextMenuEvent* event ) { // .... menu.exec( event->globalPos() ); QGraphicsView::contextMenuEvent(event); }
QGraphicsView::contextMenuEvent() will attempt to call QGraphicsScene::contextMenuEvent() if the view is associated with a scene:
The default implementation forwards the event to the topmost visible item that accepts context menu events at the position of the event. If no items accept context menu events at this position, the event is ignored.
This looks like the source of the problem, although the order of the menus in the description of the symptom is opposite of what the source shows. GraphicsView::contextMenuEvent() should call the base version, and only show its own menu if the event was ignored.
LineEdit::contextMenuEvent() should mark the event as accepted to facilitate this logic.
-
I removed the GraphicsView handler after all. It wasn't worth it.
Thank you very much for your help.