You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何让Qt键盘启动器窗口像Spotlight或iTerm2热键窗口一样显示在所有Space及所有屏幕上?

Solution for Qt Launcher Window on macOS (Fixing Your Existing Issues)

Let's walk through exactly how to get your launcher window behaving as you need it to, fixing the problems you hit with your previous tries:

1. Hide Dock Icon, Remove Menu Bar, and Stay Out of App Switcher

The most reliable way to do this on macOS is tweaking your app's Info.plist:

  • Add the key LSUIElement and set its value to YES. This marks your app as a "UI Element" app, which does three key things:
    • Removes the app icon from the Dock
    • Hides the main menu bar entirely
    • Keeps the app out of the Cmd+Tab app switcher

Heads up: This makes your app run in the background by default, so we'll need to handle activation later to make sure your window can receive keyboard input.

2. Get the Window on All Screens and Above Fullscreen Apps

Your earlier Obj-C code was missing a critical behavior flag and didn't set the right window level. Here's the corrected code to add to your Qt project:

#include <objc/message.h>
#include <objc/runtime.h>

// Call this right after your launcher QWindow is created and shown
void setupLauncherWindow(QWindow* launcherWindow) {
    WId winId = launcherWindow->winId();
    objc_object* nsView = reinterpret_cast<objc_object*>(winId);
    
    // Grab the underlying NSWindow from the Qt view
    objc_object* nsWindow = objc_msgSend(nsView, sel_registerName("window"));
    if (!nsWindow) return;

    // Combine the necessary collection behaviors:
    // - CanJoinAllSpaces: Makes the window show up on every screen/space
    // - FullScreenAuxiliary: Ensures it stays on top of fullscreen apps
    // - Transient: Keeps it out of the Cmd+` window switcher
    // - Stationary: Prevents other windows from pushing it out of place
    const NSInteger collectionBehaviors = 
        (1 << 0)  // NSWindowCollectionBehaviorCanJoinAllSpaces
        | (1 << 8) // NSWindowCollectionBehaviorFullScreenAuxiliary
        | (1 << 3) // NSWindowCollectionBehaviorTransient
        | (1 << 2); // NSWindowCollectionBehaviorStationary

    objc_msgSend(nsWindow, sel_registerName("setCollectionBehavior:"), collectionBehaviors);

    // Set the window level to match the status bar (above regular windows, works with fullscreen auxiliary)
    const NSInteger statusLevel = objc_msgSend(objc_getClass("NSWindow"), sel_registerName("statusWindowLevel"));
    objc_msgSend(nsWindow, sel_registerName("setLevel:"), statusLevel);
}

3. Fix Keyboard Input Problems

Since LSUIElement apps run in the background, you need to explicitly activate the app when showing your launcher window to let it receive keyboard input. Add this right after you show the window:

launcherWindow->show();
launcherWindow->raise();
QApplication::activateWindow(launcherWindow);

// If the Qt activate call isn't enough, fall back to direct Obj-C activation:
objc_object* sharedApp = objc_msgSend(objc_getClass("NSApplication"), sel_registerName("sharedApplication"));
objc_msgSend(sharedApp, sel_registerName("activateIgnoringOtherApps:"), YES);

4. Keep Your Settings Window as a Regular Window

For your settings window, just skip all the custom configurations above. Use a standard QMainWindow or QDialog without any Obj-C tweaks. It will behave like a normal macOS window (supports resizing, minimizing, shows up in Cmd+` switcher) while still respecting the app's LSUIElement status (no Dock icon). If you need it to have full regular app behaviors (like a Dock icon when it's open), that's more complex, but this approach should cover your "regular window" expectation.

Why Your Previous Tries Didn't Work

  • QT_MAC_DISABLE_FOREGROUND_APPLICATION_TRANSFORM: This forces your app to stay in the background, so even if the window is visible, it can't receive keyboard input because the app isn't active.
  • Your original Obj-C code: You missed the NSWindowCollectionBehaviorCanJoinAllSpaces flag (so the window didn't show on all screens) and didn't set a high enough window level. Also, you didn't modify the Info.plist, so the Dock icon and menu bar stayed visible.

内容的提问来源于stack exchange,提问作者ManuelSchneid3r

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.30 21:52:34