3

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?

TheDarkKnight
  • 27,181
  • 6
  • 55
  • 85

2 Answers2

6

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];
}
Community
  • 1
  • 1
Linville
  • 3,613
  • 1
  • 27
  • 41
  • 1
    Wow, that was easy. I thought that mixing Objective C with Qt was only going to be simple from XCode, but Qt Creator handles it just as well. Thanks. – TheDarkKnight Sep 20 '13 at 08:21
1

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

njahnke
  • 1,369
  • 2
  • 10
  • 33
  • sorry it's not the same. The question concerns posting messages to the Notification Centre, which can be done from any application, not just those with a System Tray icon. These messages are different. – TheDarkKnight Oct 16 '14 at 17:08
  • I don't understand the distinction. These messages appear in the Notification Center for me, just like those from any other app. – njahnke Oct 16 '14 at 17:18
  • they may appear in the Notification Center, but your code requires a QSystemTrayIcon, so only works with that present. – TheDarkKnight Oct 20 '14 at 07:41
  • The icon itself need not be present - simply remove the call to `QSystemTrayIcon::show()`. Or are you saying that because you are developing a daemon, you do not want to include `QtGui`? – njahnke Oct 20 '14 at 16:34
  • No, a daemon is irrelevant as it can't display UI. Creating a QSystemTrayIcon in this instance, just to display the message and not use the system tray icon is a bit overkill. In addition, you can't set the calling application's icon using this method, so it's not ideal. However, you're right in that it could be used for basic messages. – TheDarkKnight Oct 21 '14 at 08:18
  • Did you have Growl installed? `QSystemTrayIcon::showMessage()` required Growl to be installed on the system. Otherwise it simply doesn't work. – masnun Jul 15 '16 at 06:30