Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to add observer for macOS?
Forum Update on Monday, May 27th 2025

How to add observer for macOS?

Scheduled Pinned Locked Moved Solved General and Desktop
8 Posts 3 Posters 799 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    senmx
    wrote on last edited by
    #1

    app.pro:

    macx {
        HEADERS += \
            mac.h
        OBJECTIVE_SOURCES += \
            mac.mm
        LIBS += -framework Foundation
        LIBS += -framework Cocoa
        QMAKE_INFO_PLIST=$$PWD/Info.plist
    }
    

    mac.mm:

    #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];
    }
    
    1 Reply Last reply
    0
    • M Offline
      M Offline
      mpergand
      wrote on last edited by
      #2

      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

      1 Reply Last reply
      0
      • S Offline
        S Offline
        senmx
        wrote on last edited by
        #3

        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?

        SGaistS 1 Reply Last reply
        0
        • M Offline
          M Offline
          mpergand
          wrote on last edited by
          #4
          MyObserver *ob = [[MyObserver alloc] init];
          
          S 1 Reply Last reply
          0
          • M mpergand
            MyObserver *ob = [[MyObserver alloc] init];
            
            S Offline
            S Offline
            senmx
            wrote on last edited by
            #5

            @mpergand Thank you very much! But I did not receive the notification of NSWorkspaceActiveSpaceDidChangeNotification, and there is no response to workspaceChanged here.

            1 Reply Last reply
            0
            • S senmx

              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?

              SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

              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];
              

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              M S 2 Replies Last reply
              3
              • SGaistS SGaist

                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];
                
                M Offline
                M Offline
                mpergand
                wrote on last edited by
                #7

                @SGaist good point !
                You're right, (have checked with some old code of mine)

                1 Reply Last reply
                0
                • SGaistS SGaist

                  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];
                  
                  S Offline
                  S Offline
                  senmx
                  wrote on last edited by
                  #8

                  @SGaist Thank you very much, it worked!

                  1 Reply Last reply
                  1

                  • Login

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • Users
                  • Groups
                  • Search
                  • Get Qt Extensions
                  • Unsolved