Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Notification window in Mac. With or without Qt

Tags:

c++

macos

qt

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:

setWindowFlags(
    Qt::FramelessWindowHint |
    Qt::WindowSystemMenuHint |
    Qt::Tool |
    Qt::WindowStaysOnTopHint
);
setAttribute(Qt::WA_TranslucentBackground);

Qt::WA_ShowWithoutActivating doesn't affect anything.

Is there a way to do that? I'm ready to implement the native Carbon/Cocoa solution there, but Qt is preferred. Or maybe I'm wrong in Mac philosophy and I should notify user in a kind another manner?

Update Growl doesn't support editor line in its notifications, does it?

like image 607
Pavel Koryagin Avatar asked Apr 28 '11 19:04

Pavel Koryagin


2 Answers

I did it!

#ifdef Q_OS_MAC
#include <Carbon/Carbon.h>
#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.
like image 197
Pavel Koryagin Avatar answered Nov 20 '22 09:11

Pavel Koryagin


Pavel,

Have you heard of Growl? Growl is a VERY impressive notification app that you can bundle and use with your application. Adium - a popular instant messaging app for OS X - uses it for all notifications.

http://growl.info/

like image 44
Avisra Avatar answered Nov 20 '22 09:11

Avisra