How to add observer for macOS?
-
macx { HEADERS += \ mac.h OBJECTIVE_SOURCES += \ mac.mm LIBS += -framework Foundation LIBS += -framework Cocoa QMAKE_INFO_PLIST=$$PWD/Info.plist }
#include "mac.h" #include <QDebug> #include <Foundation/Foundation.h> #include <Cocoa/Cocoa.h> #include <AppKit/AppKit.h> Mac::Mac() { } Mac::~Mac() { } void Mac::workspaceChanged(NSNotification* notification) { qDebug() << "========================"; qDebug() << notification; qDebug() << "========================"; } void Mac::init() { qDebug() << "========================"; qDebug() << (id)this; //crashed in here qDebug() << "========================"; [[NSNotificationCenter defaultCenter] addObserver:(id)this selector:@selector(workspaceChanged:) name:NSWorkspaceActiveSpaceDidChangeNotification object:nil]; }
-
Hi,
You must create a NSObject and pass it at the first parameter.
See this post : https://forum.qt.io/topic/103078/how-to-get-notification-of-mouse-down-in-menu-bar/5 -
Thanks. Is like this?
#include "mac.h" #include <QDebug> #include <Foundation/Foundation.h> #include <Cocoa/Cocoa.h> #include <AppKit/AppKit.h> #include "com/util.h" Mac::Mac() { } Mac::~Mac() { } @interface MyObserver: NSObject { } - (void)workspaceChanged:(NSNotification*) notification; @end @implementation MyObserver - (void)workspaceChanged:(NSNotification*) notification { NSLog(@">>>>>>>>>>workspaceChanged %@", notification); } @end void Mac::init() { NSLog(@"mac init"); MyObserver *ob = [MyObserver init]; //crashed in here NSLog(@"ob %@", ob); [[NSNotificationCenter defaultCenter] addObserver:ob selector:@selector(workspaceChanged:) name:NSWorkspaceActiveSpaceDidChangeNotification object:nil]; }
But:
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** +[MyObserver<0x107842488> init]: cannot init a class object.'
I guess there is something wrong with the objective-c code?
-
Thanks. Is like this?
#include "mac.h" #include <QDebug> #include <Foundation/Foundation.h> #include <Cocoa/Cocoa.h> #include <AppKit/AppKit.h> #include "com/util.h" Mac::Mac() { } Mac::~Mac() { } @interface MyObserver: NSObject { } - (void)workspaceChanged:(NSNotification*) notification; @end @implementation MyObserver - (void)workspaceChanged:(NSNotification*) notification { NSLog(@">>>>>>>>>>workspaceChanged %@", notification); } @end void Mac::init() { NSLog(@"mac init"); MyObserver *ob = [MyObserver init]; //crashed in here NSLog(@"ob %@", ob); [[NSNotificationCenter defaultCenter] addObserver:ob selector:@selector(workspaceChanged:) name:NSWorkspaceActiveSpaceDidChangeNotification object:nil]; }
But:
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** +[MyObserver<0x107842488> init]: cannot init a class object.'
I guess there is something wrong with the objective-c code?
Hi,
@senmx said in How to add observer for macOS?:
[[NSNotificationCenter defaultCenter]
Shouldn't you rather use the Notification Center associated to the shared workspace of your application ?
Something like:
[[[NSWorkspace sharedWorkspace] notificationCenter] addObserver:obj selector:@selector(workspaceChanged:) name:NSWorkspaceActiveSpaceDidChangeNotification object:nil];
-
Hi,
@senmx said in How to add observer for macOS?:
[[NSNotificationCenter defaultCenter]
Shouldn't you rather use the Notification Center associated to the shared workspace of your application ?
Something like:
[[[NSWorkspace sharedWorkspace] notificationCenter] addObserver:obj selector:@selector(workspaceChanged:) name:NSWorkspaceActiveSpaceDidChangeNotification object:nil];
-
Hi,
@senmx said in How to add observer for macOS?:
[[NSNotificationCenter defaultCenter]
Shouldn't you rather use the Notification Center associated to the shared workspace of your application ?
Something like:
[[[NSWorkspace sharedWorkspace] notificationCenter] addObserver:obj selector:@selector(workspaceChanged:) name:NSWorkspaceActiveSpaceDidChangeNotification object:nil];