Bug 504260

Summary: Double View::contextMenuAboutToShow signals when using keyboard 'Menu' button if QEventLoop::exec is called
Product: [Frameworks and Libraries] frameworks-ktexteditor Reporter: ulterno
Component: generalAssignee: KWrite Developers <kwrite-bugs-null>
Status: REPORTED ---    
Severity: normal CC: christoph
Priority: NOR    
Version First Reported In: unspecified   
Target Milestone: ---   
Platform: Arch Linux   
OS: Linux   
Latest Commit: Version Fixed In:
Sentry Crash Report:

Description ulterno 2025-05-15 12:36:36 UTC
SUMMARY 

[Wayland]
When the application is calling QEventLoop::exec() in a slot connected to KTextEditor::View::contextMenuAboutToShow
And the user uses the Keyboard 'Menu' button in the text area of the View to open the right-click-context-menu
The signal KTextEditor::View::contextMenuAboutToShow gets called another time on QEventLoop::exec()

This happens despite using the `QEventLoop::ExcludeUserInputEvents` flag.

STEPS TO REPRODUCE
1. Use the minimal example at https://invent.kde.org/ulterno/UsingKTextEditor/-/tree/minimal. Build and run
2. Use the keyboard 'Menu' key to invoke the menu
3. Look at the debug output

OBSERVED RESULT
- The function MainWindow::doSomeMenuStuff gets called twice, corresponding to KTextEditor::View::contextMenuAboutToShow being emitted twice

EXPECTED RESULT
- KTextEditor::View::contextMenuAboutToShow is emitted only once and all subsequent connections act accordingly

SOFTWARE/OS VERSIONS
KTextEditor: master (dd56ddc2d64d02a4e258330af0d772444d94a488)
Operating System: EndeavourOS 
KDE Plasma Version: 6.3.5
KDE Frameworks Version: 6.14.0
Qt Version: 6.9.0
Kernel Version: 6.14.6-arch1-1 (64-bit)
Graphics Platform: Wayland

ADDITIONAL INFORMATION
Related to BUG 503813
Unable to reproduce on X11 session
Comment 1 ulterno 2025-05-15 14:03:03 UTC
Using guidance by Igor Kushnir, I see contextMenuEvent being called twice, in case the 'Menu' key is used.
The QContextMenuEvent in this case, has different addresses.

The resulting QMenu pointer for the KTextEditor::View::contextMenuAboutToShow signal ends up being the same due to KTextEditor::ViewPrivate::contextMenu() using the KXMLGUIFactory
Comment 2 Christoph Cullmann 2025-05-30 18:14:47 UTC
We could block that until 

aboutToHideContextMenu

is triggered.
Comment 3 ulterno 2025-05-31 06:40:11 UTC
That should work since we don't open another context menu on right click when one is already open (unlike Microsoft things).
It might be good if we could find out why that happens in case of Wayland and not X11.

Maybe one can try checking with something other than KWin?
Comment 4 Christoph Cullmann 2025-07-20 17:54:16 UTC
Has somebody time to test that and provide a patch?