Notification window in Mac. With or without Qt

后端 未结 5 1532
一整个雨季
一整个雨季 2021-02-06 13:03

Qt project on Mac OS X. I need to show notification window on top without stealing a focus from any active application.

Here the widget constructor part:



        
5条回答
  •  逝去的感伤
    2021-02-06 13:47

    I did it!

    #ifdef Q_OS_MAC
    #include 
    #endif
    
    NotifyWindow::NotifyWindow() : QWidget(0 /* This zero is the first point */) {
    
        setWindowFlags(
        #ifdef Q_OS_MAC
            Qt::SubWindow | // This type flag is the second point
        #else
            Qt::Tool |
        #endif
            Qt::FramelessWindowHint |
            Qt::WindowSystemMenuHint |
            Qt::WindowStaysOnTopHint
        );
        setAttribute(Qt::WA_TranslucentBackground);
    
        // And this conditional block is the third point
    #ifdef Q_OS_MAC
        winId(); // This call creates the OS window ID itself.
                 // qt_mac_window_for() doesn't
    
        int setAttr[] = {
            kHIWindowBitDoesNotHide, // Shows window even when app is hidden
    
            kHIWindowBitDoesNotCycle, // Not sure if required, but not bad
    
            kHIWindowBitNoShadow, // Keep this if you have your own design
                                  // with cross-platform drawn shadows
            0 };
        int clearAttr[] = { 0 };
        HIWindowChangeAttributes(qt_mac_window_for(this), setAttr, clearAttr);
    #endif
    }
    

    We get almost the same nice behavior as in Windows:

    • It does not stole focus on show. (Two weeks of searching over the Internet)
    • The controls there handle the first user click, while other windows need an extra click to activate.
    • When the window is being activated, the other windows of the same application, do not bubble up to the front.
    • And a small problem remains, but at least it has a simple workaround. Or even could be left.

提交回复
热议问题