0

The goal is to display notifications (popup+notification center) in Mac Os.

Attemp #1. Create myclass.h + myclass.mm files (using OBJECTIVE_SOURCES). I was able to add notifications to notification center. But in order to create popups I should implement NSUserNotificationCenterDelegate:shouldPresentNotification. Naive implementation of the method and passing class as delegate raises compile-time exception: this is of *MyClass type, whereas setDelegate method requires id

Attempt #2. Define myclass using objective-c style with @interface directive and so on. Unfortunatly, compiler could not compile NSObject.h. Looks like class objective-c is not supported in Qt, and we forced to use C++ class declaration.

Any ideas, how to implement given mac-os protocol in C++ class?

Working example

MyClass.h

class MyClass
{
public:
    void test();
}

MyClass.mm

void MyClass::test()
{
  NSUserNotification *userNotification = [[[NSUserNotification alloc] init] autorelease];
  userNotification.title = @"Title";
  userNotification.informativeText = @"Test message";
  [[NSUserNotificationCenter defaultUserNotificationCenter] deliverNotification:userNotification];
}

Trying to add popups. Raises exception in setDelegate method

MyClass.h

class MyClass
{
public:
    explicit MyClass();
    void test();
    BOOL shouldPresentNotification(NSUserNotificationCenter *center, NSUserNotification *notification);
}

MyClass.mm

MyClass::MyClass()
{
    [[NSUserNotificationCenter defaultUserNotificationCenter] setDelegate:this];
}

BOOL MyClass::shouldPresentNotification(NSUserNotificationCenter *center, NSUserNotification *notification)
{
    return YES;
}

void MyClass::test()
{
  NSUserNotification *userNotification = [[[NSUserNotification alloc] init] autorelease];
  userNotification.title = @"Title";
  userNotification.informativeText = @"Test message";
  [[NSUserNotificationCenter defaultUserNotificationCenter] deliverNotification:userNotification];
}
developer
  • 319
  • 2
  • 13

1 Answers1

0

Solution turned out to be easy: you should just cast this to required protocol

MyClass::MyClass()
{
    id<NSUserNotificationCenterDelegate> self = (id<NSUserNotificationCenterDelegate>)this;
    [[NSUserNotificationCenter defaultUserNotificationCenter] setDelegate:self]; 
}

BOOL MyClass::shouldPresentNotification(NSUserNotificationCenter *center, NSUserNotification *notification)
{
    return YES;
}

void MyClass::test()
{
  NSUserNotification *userNotification = [[[NSUserNotification alloc] init] autorelease];
  userNotification.title = @"Title";
  userNotification.informativeText = @"Test message";
  [[NSUserNotificationCenter defaultUserNotificationCenter] deliverNotification:userNotification];
}
developer
  • 319
  • 2
  • 13