Ok, thanks for your help.
Like anda_skoa suggest me:
You could check if there is native API to catch that event, ...
i used the cocoa code(mixed with the Qt code) to manage the operation.
I registered another function instead of the system function related to the Exit action.
This operation can be done extending the QApplication class with a custom class.
For someone who's interested below the code:
In CustomAppClass.h that extend QApplication
#ifdef Q_OS_MAC
#include <objc/objc.h>
#include <objc/message.h>
void setupDockClickHandler();
int quitFromDockMenuHandler(id self, SEL _cmd,...);
#endif
#ifdef Q_OS_MAC
#include <objc/objc.h>
#include <objc/message.h>
void setupDockClickHandler();
int quitFromDockMenuHandler(id self, SEL _cmd,...);
#endif
To copy to clipboard, switch view to plain text mode
in the .c file:
#ifdef Q_OS_MAC
void setupDockClickHandler() {
Class cls = objc_getClass("NSApplication");
objc_object *appInst = objc_msgSend((objc_object*)cls, sel_registerName("sharedApplication"));
if(appInst != NULL) {
objc_object* delegate = objc_msgSend(appInst, sel_registerName("delegate"));
Class delClass = (Class)objc_msgSend(delegate, sel_registerName("class"));
SEL closeHandle = sel_registerName("applicationShouldTerminate:");
if (class_getInstanceMethod(delClass, closeHandle)) {
if (class_replaceMethod(delClass, closeHandle, (IMP)quitFromDockMenuHandler, "B@:"))
qDebug() << "Registered quit click handler (replaced original method)";
else
qWarning() << "Failed to replace method for quit click handler";
}
else {
if (class_addMethod(delClass, closeHandle, (IMP)quitFromDockMenuHandler,"B@:"))
qDebug() << "Registered quit click handler";
else
qWarning() << "Failed to register quit click handler";
}
}
}
int quitFromDockMenuHandler(id self,SEL _cmd,...) {
Q_UNUSED(self)
Q_UNUSED(_cmd)
qDebug() << "QUIT!";
((SingleApplication*)qApp)->onQuitAppAction();
/// Return NO (false) to suppress the default OS X actions
return 1;
}
void SingleApplication:: onQuitAppAction(){
emit sig_quitAppAction();
}
#endif
#ifdef Q_OS_MAC
void setupDockClickHandler() {
Class cls = objc_getClass("NSApplication");
objc_object *appInst = objc_msgSend((objc_object*)cls, sel_registerName("sharedApplication"));
if(appInst != NULL) {
objc_object* delegate = objc_msgSend(appInst, sel_registerName("delegate"));
Class delClass = (Class)objc_msgSend(delegate, sel_registerName("class"));
SEL closeHandle = sel_registerName("applicationShouldTerminate:");
if (class_getInstanceMethod(delClass, closeHandle)) {
if (class_replaceMethod(delClass, closeHandle, (IMP)quitFromDockMenuHandler, "B@:"))
qDebug() << "Registered quit click handler (replaced original method)";
else
qWarning() << "Failed to replace method for quit click handler";
}
else {
if (class_addMethod(delClass, closeHandle, (IMP)quitFromDockMenuHandler,"B@:"))
qDebug() << "Registered quit click handler";
else
qWarning() << "Failed to register quit click handler";
}
}
}
int quitFromDockMenuHandler(id self,SEL _cmd,...) {
Q_UNUSED(self)
Q_UNUSED(_cmd)
qDebug() << "QUIT!";
((SingleApplication*)qApp)->onQuitAppAction();
/// Return NO (false) to suppress the default OS X actions
return 1;
}
void SingleApplication:: onQuitAppAction(){
emit sig_quitAppAction();
}
#endif
To copy to clipboard, switch view to plain text mode
Do not forget to put int the .pro file the LIBS variable with the correct information to inform the linker!
Bookmarks