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. macOS: how to get access to the NSApplicationDelegate?
Forum Update on Monday, May 27th 2025

macOS: how to get access to the NSApplicationDelegate?

Scheduled Pinned Locked Moved Solved General and Desktop
4 Posts 3 Posters 2.8k 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.
  • D Offline
    D Offline
    davecotter
    wrote on last edited by davecotter
    #1

    Qt must, under the hood, have an appDelegate? How do i get access to that, if i say want to add a virtual method to it, and pass it to a library that needs access to it?

    i saw this:
    https://wiki.qt.io/Application_Start-up_Patterns

    but that talks about replacing QApplication, which i can't do because that's.. well, my entire app is in there already.

    I just want to get access to the existing (?) one. there must be one, right?

    1 Reply Last reply
    0
    • D Offline
      D Offline
      davecotter
      wrote on last edited by davecotter
      #4

      okay here:

      id	appDelegate = [[NSApplication sharedApplication] delegate];
      

      note that that "id" is actually of type QCocoaApplicationDelegate, but that's a private type so you can't use it :(

      some other info here too

      1 Reply Last reply
      1
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on last edited by
        #2

        Hi,

        You can take some inspiration from QTBUG-38184 which ask a similar question for iOS.

        Hope it helps

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

        1 Reply Last reply
        1
        • M Offline
          M Offline
          mpergand
          wrote on last edited by
          #3

          I used a custom delegate in the past (Qt 4 i think)

          static AppDelegate *sAppDelegate;
          
           void  CocoaBridge::installDelegate(MPQt::Application *app)
           {
          	sAppDelegate=[AppDelegate new];
          	sAppDelegate->qtApp=app;
          	[NSApp setDelegate:sAppDelegate];
           }
          

          The drawback is that you need to deal events like openFiles or applicationShouldTerminate by yourself:

          -(NSApplicationTerminateReply) applicationShouldTerminate:(NSApplication*) sender
          { Q_UNUSED(sender)
          	Log("NSApp should terminate")
          	QEvent ev(QEvent::Close);
          	qtApp->sendEvent(qtApp,&ev);
          
          	if(ev.isAccepted())
          		{
          		qtApp->quit();
          		return NSTerminateNow;
          		}
          
          	return NSTerminateCancel;
          }
          
          1 Reply Last reply
          2
          • D Offline
            D Offline
            davecotter
            wrote on last edited by davecotter
            #4

            okay here:

            id	appDelegate = [[NSApplication sharedApplication] delegate];
            

            note that that "id" is actually of type QCocoaApplicationDelegate, but that's a private type so you can't use it :(

            some other info here too

            1 Reply Last reply
            1
            • M myk321 referenced this topic on

            • Login

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