Solved macOS: how to get access to the NSApplicationDelegate?
-
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_Patternsbut 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?
-
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
-
Hi,
You can take some inspiration from QTBUG-38184 which ask a similar question for iOS.
Hope it helps
-
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; }
-
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
-