Question

I'm developing a daemon with Qt and am wanting to send messages to the Mountain Lion notification centre: -

enter image description here

Qt seems to lack support for this, which isn't surprising as it's not cross-platform.

If I were using Cocoa, it looks like I would use the NSUserNotificationCenter class with an NSUserNotification.

Up until now, whenever I've seen a Foundation class, there has always been a matching Core Foundation class, but it appears that there is no CFUserNotificationCenter, but there is a CFUserNotification.

So, am I right that NSNotificationCenter is what Cocoa uses for this and is it possible to send messages to the Notification Centre without Cocoa?

If it is possible, please can someone post a code example in C or C++, or at least point out which functions to use?

Was it helpful?

Solution

You can easily integrate Qt/C++ code with Cocoa/Objective-C. XCode supports Objective-C++ which is a mix of C++ and Objective-C. These files will have a .mm extension and are added to your QMake project using the OBJECTIVE_SOURCES variable (which is unfortunately not in the official QMake documentation). So just add the NSUserNotification code right into a C++ class.

QMake Project File

HEADERS += Foo.h
OBJECTIVE_SOURCES += Foo.mm
LIBS += -framework Foundation
INCLUDEPATH += /System/Library/Frameworks/Foundation.framework/Versions/C/Headers

Foo.h

#ifndef FOO_H
#define FOO_H
class Foo {
public:
  void test();
};

#endif

Foo.mm

#include "foo.h"
#include <NSUserNotification.h>

void Foo::test() {
  NSUserNotification *userNotification = [[[NSUserNotification alloc] init] autorelease];
  userNotification.title = @"My Great Title";
  userNotification.informativeText = @"Blah Blah Blah";

  [[NSUserNotificationCenter defaultUserNotificationCenter] deliverNotification:userNotification];
}

OTHER TIPS

No need for native code. QSystemTrayIcon::showMessage() is working as expected for me in 10.9.5 (Qt 5.3.2).

QSystemTrayIcon tray_icon;
auto menu = new QMenu;
tray_icon.setContextMenu(menu);
tray_icon.show();
tray_icon.showMessage("Test message", "Test body");

enter image description here

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top