Question

I've got a Qt application that (for reasons that are outside the scope of this question) launches each of its windows as a separate process.

This works fine, but under MacOS/X, each window/process shows up as a separate entry in the Dock. Thus when the user has a dozen windows open, there are a dozen identical icons in the Docks, which isn't very helpful (since it's hard to tell which is which, and the icons start to get small).

Is there some way to tell the Dock to group all of these processes together under a single Dock icon? (a Mac-specific API would be fine)

Était-ce utile?

La solution

You can use the following snippet to hide the Dock tile of a process that is not active:

 - (void)applicationWillResignActive:(NSNotification *)notification
{
    ProcessSerialNumber psn = {0, kCurrentProcess};
    TransformProcessType(&psn, kProcessTransformToBackgroundApplication);
    if([self.window isVisible])
    {
        [self.window performSelector:@selector(orderFrontRegardless) withObject:nil afterDelay:0.05];
    }
}

- (void)applicationWillBecomeActive:(NSNotification *)notification
{
    ProcessSerialNumber psn = {0, kCurrentProcess};
    TransformProcessType(&psn, kProcessTransformToForegroundApplication);
    SetFrontProcessWithOptions(&psn, kSetFrontProcessCausedByUser);
}

Just add the above code to your app delegate and the app's Dock icon will vanish when the process resigns active.
To keep the window visible after the process was turned into an UIElement application, send a orderFrontRegardless message. (Very hacky, I know - but that must be the price for the non-standard window/process handling)

Probably you should also maintain a Dock menu that allows your users to select hidden windows.
You could dynamically add entries from your "main" app. Details can be found in the "Dynamically Adding Menu Items With the Application Delegate" section of the Dock Tile Programming Guide.

Update:
I slightly changed the code sample above as the previous approach led to a non-responsive main menu after re-activating the app.

Autres conseils

Call QSystemTrayIcon::hide().

If that doesn't work, here are some things you can try:


Use NSApplication's setActivationPolicy: method

Way 1: [NSApp setActivationPolicy: NSApplicationActivationPolicyAccessory];

Way 2: [NSApp setActivationPolicy: NSApplicationActivationPolicyProhibited];


OR

Use LSUIElement (LSUIElement=1).

You can also put this in your pfile: <key>LSUIElement</key> <string>1</string> or make Qt do it for you.


There is also some discussion about OSX menus on the Qt forums. In addition, you could try to use QFocusEvent to check when the app loses/gains focus and then update menus accordingly.


Sources

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top