QSystemTrayIcon: Menu gets disabled if no window has focus
I encountered a problem with the QSystemTrayIcon and don't know why it behaves like it does.
My mac application is an agent (LSUIElement=true in info.plist) and displays a QSystemTrayIcon at all time. When I click the tray icon and the app is not the active app, all actions in the displayed QMenu are disabled. I checked, if my actions are disabled or if the menu is disabled, but non of them are.
Does anyone have an idea what is getting disabled and how I can prevent that?
Hi and welcome to devnet,
Which version of Qt ?
Which version of macOS ?
Can you provide a minimal compilable example that shows that behaviour ?
I'm using macOS 10.15.6 and Qt 5.14.2.
I've built a basic demo app, but unfortunately it is working completely normal. Seems to be a problem inside of my implementation. I will strip down my application to find the error or provide a malfunctioning app if it still appears.
vic.ju last edited by vic.ju
I found what's causing the issue. I have a custom NSApplicationDelegate that is somehow interfering. The only thing that I implemented for the delegate are
- (BOOL)applicationShouldHandleReopen:(NSApplication*)sender hasVisibleWindows:(BOOL)flag; - (NSApplicationTerminateReply)applicationShouldTerminate:(NSApplication*)sender;
Maybe I need to implement some other features? I have another application that also use a NSApplicationDelegate and it is working fine.
I uploaded the demo app to my google drive: TrayIconTest.zip
I found the reason for the problem. I set the NSApplicationDelegate after I created the QApplication and this confuses the application (firstResponder). So the solution is to just set it before.
Glad you found out and thanks for sharing the solution.
Out of curiosity, are you using applicationShouldTerminate to avoid the application to end while the system tray icon stays alive ? If so, it's not needed, Qt already provides quitOnLastWindowClosed for that kind of use cases.
I use the quitOnLastWindowClosed function for that as you stated. I use applicationShouldTerminate to call the correct cleanup methods on application exit. I'm not sure why we do it this way, but there were some problems in the past when exiting the application.