Skip to content

Fix Qt6 menus randomly not displaying#623

Open
Cordtus wants to merge 3 commits intobgrabitmap:masterfrom
Cordtus:fix-qt6-menus-missing
Open

Fix Qt6 menus randomly not displaying#623
Cordtus wants to merge 3 commits intobgrabitmap:masterfrom
Cordtus:fix-qt6-menus-missing

Conversation

@Cordtus
Copy link
Copy Markdown

@Cordtus Cordtus commented Dec 10, 2025

Fixes #624

Problem

When building LazPaint with Qt6 widgetset (--ws=qt6), menus would randomly fail to appear in the main menu bar. The "Image" menu was particularly affected, but other menus could also be missing on any given launch (roughly 30-70% failure rate across multiple launches).

This issue was tested and reproduced on Arch Linux with qt6pas 6.2.9, FPC 3.2.2, and Lazarus 3.8.

Root Causes and Fixes

1. TActionList.ActionByName race condition with Qt6

During early initialization, TActionList.ActionByName appears to have timing issues with Qt6 that cause it to fail to find actions that are actually present. Debug logging confirmed all 177 actions were loaded, but ActionByName would sometimes return nil.

Fix: Added FindActionByNameDirect helper function that iterates directly over ActionList.Actions[] instead of using the hash-based lookup.

2. Menu visibility not explicitly set

Qt6 sometimes fails to display TMenuItems even when they're correctly populated with all their child items.

Fix: Added explicit Visible := true for all used menus in the Apply procedure.

3. Main menu bar widget needs refresh

Even with correct menu data, Qt6's main menu bar widget wouldn't always render all items.

Fix: Added menu detach/reattach sequence with ProcessMessages calls after menu population to force Qt6 to rebuild the menu bar widget:

Self.Menu := nil;
Application.ProcessMessages;
Self.Menu := MainMenu1;
Application.ProcessMessages;

Testing

Before this fix: 2/6 launches had the Image menu (~33% success rate)
After this fix: 6/6 launches had the Image menu (100% success rate)

The fix has been tested repeatedly with consistent results.

Changes

  • lazpaint/umenu.pas: Added FindActionByNameDirect helper, replaced ActionByName calls, added explicit visibility setting
  • lazpaint/lazpaintmainform.pas: Added menu bar refresh after m.Apply

When building with Qt6 widgetset, menus would randomly fail to appear
in the main menu bar. The "Image" menu was particularly affected, but
other menus could also be missing on any given launch (roughly 30-70%
failure rate across multiple launches).

Root causes and fixes:

1. ActionByName issues: TActionList.ActionByName appears to have race
   condition issues with Qt6 during early initialization. Replaced with
   FindActionByNameDirect helper that directly iterates ActionList.Actions.

2. Menu visibility: Qt6 sometimes fails to display TMenuItems even when
   they're correctly populated. Added explicit Visible:=true for used menus.

3. Menu bar refresh: Qt6 widget handle needed forced refresh after menu
   population. Added Menu detach/reattach with ProcessMessages calls to
   force Qt6 to rebuild the menu bar widget.

Tested on Arch Linux with qt6pas 6.2.9, FPC 3.2.2, Lazarus 3.8.
@Cordtus
Copy link
Copy Markdown
Author

Cordtus commented Dec 12, 2025

Upon further testing there are some issues this has created.

  • I feel like the performance is quite degraded, but this could just be the fact that my pc is quite old, with a 4th gen i5.
  • I noticed just now that the entire selection of menus intermittently does not display at all, but I recall this happening quite a lot for other applications too so it may be completely unrelated.

Cordtus and others added 2 commits December 12, 2025 16:50
- Only run the menu rebuild workaround on Qt5/Qt6 widgetsets
- GTK and Win32 users no longer execute this code path
- Reduce ProcessMessages calls from 2 to 1
uimageview.pas:
- Keep IMAGEVIEW_DIRECTUPDATE disabled for Qt6 (bypasses Qt paint system)
- Enable IMAGEVIEW_QUICKUPDATE for Qt6 (forces immediate repaint)
- This fixes slow mouse wheel zoom and invisible paintbrush cursor

lazpaintmainform.pas:
- More aggressive Qt6 menu rebuild: toggle visibility on all menu items
- Add HandleNeeded call to force menu bar widget creation
- Multiple ProcessMessages calls to ensure Qt6 fully processes changes

Tested on Arch Linux with qt6pas 6.2.9, FPC 3.2.2, Lazarus 3.8.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Qt6: Menus randomly fail to display on launch

1 participant