Results 1 to 4 of 4

Thread: Qt OS X closing application issue

  1. #1
    Join Date
    Jan 2015
    Posts
    15
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Windows

    Default Qt OS X closing application issue

    Hi All,

    Recently i started to program Qt on OS X.
    My simple application is composed by only one window with a system tray icon.
    When the user press the closing button (x) on the top left corner, the application becomes hide.
    I also connect an action from the tray icon menu to quit the application. Everything works fine with the expected behavior.

    My only problem is that, the app also have the icon dock menu with the action "quit". This action is a native function provided by the OS.
    What i would like to do is to "catch" this signal emit from that action.
    Because seems that the quit from the dock icon is different from the CMD+Q action(i'm able to manage this signal in my overloaded function).
    Qt Code:
    1. void MainWindow::closeEvent(QCloseEvent* event)
    To copy to clipboard, switch view to plain text mode 
    The quit from the Dock Menu seems like an interrupt(like SIGTERM) and the app quit in any case. If the application main window is shown, i don't want to close the app but simply hide the window.

    Another weird thing is that when i press on quit from the Dock menu, the
    Qt Code:
    1. closeEvent(QCloseEvent* event)
    To copy to clipboard, switch view to plain text mode 
    function is called twice and if i ignore the close event
    Qt Code:
    1. event->ignore()
    To copy to clipboard, switch view to plain text mode 
    the app quit anyway.

    Thanks to All in advance.

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Qt OS X closing application issue

    You could check if there is native API to catch that event, if it is available to the native event filter used by the Qt application's event dispatcher, etc.

    If it is indeed just a signal, then installing a signal handler might do the trick.

    Cheers,
    _

  3. #3
    Join Date
    Sep 2015
    Posts
    12
    Thanked 3 Times in 3 Posts
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Qt OS X closing application issue

    Qt with OS X works swimmingly. Whatever is going on is likely your approach.

    Can you post some code (particularly your main.cpp file) and anything related to creating and closing windows.

  4. #4
    Join Date
    Jan 2015
    Posts
    15
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Windows

    Default Re: Qt OS X closing application issue

    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
    Qt Code:
    1. #ifdef Q_OS_MAC
    2. #include <objc/objc.h>
    3. #include <objc/message.h>
    4. void setupDockClickHandler();
    5. int quitFromDockMenuHandler(id self, SEL _cmd,...);
    6. #endif
    To copy to clipboard, switch view to plain text mode 
    in the .c file:
    Qt Code:
    1. #ifdef Q_OS_MAC
    2. void setupDockClickHandler() {
    3. Class cls = objc_getClass("NSApplication");
    4. objc_object *appInst = objc_msgSend((objc_object*)cls, sel_registerName("sharedApplication"));
    5. if(appInst != NULL) {
    6. objc_object* delegate = objc_msgSend(appInst, sel_registerName("delegate"));
    7. Class delClass = (Class)objc_msgSend(delegate, sel_registerName("class"));
    8. SEL closeHandle = sel_registerName("applicationShouldTerminate:");
    9. if (class_getInstanceMethod(delClass, closeHandle)) {
    10. if (class_replaceMethod(delClass, closeHandle, (IMP)quitFromDockMenuHandler, "B@:"))
    11. qDebug() << "Registered quit click handler (replaced original method)";
    12. else
    13. qWarning() << "Failed to replace method for quit click handler";
    14. }
    15. else {
    16. if (class_addMethod(delClass, closeHandle, (IMP)quitFromDockMenuHandler,"B@:"))
    17. qDebug() << "Registered quit click handler";
    18. else
    19. qWarning() << "Failed to register quit click handler";
    20. }
    21.  
    22. }
    23. }
    24.  
    25. int quitFromDockMenuHandler(id self,SEL _cmd,...) {
    26. Q_UNUSED(self)
    27. Q_UNUSED(_cmd)
    28. qDebug() << "QUIT!";
    29. ((SingleApplication*)qApp)->onQuitAppAction();
    30. /// Return NO (false) to suppress the default OS X actions
    31. return 1;
    32. }
    33.  
    34. void SingleApplication:: onQuitAppAction(){
    35. emit sig_quitAppAction();
    36. }
    37. #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!

Similar Threads

  1. Closing an external application with qt
    By Capton in forum Newbie
    Replies: 1
    Last Post: 19th June 2013, 23:45
  2. Access violation when closing application
    By doggrant in forum Qt Programming
    Replies: 8
    Last Post: 15th March 2012, 15:07
  3. Application Closing
    By manti_madhu in forum Qt Programming
    Replies: 2
    Last Post: 27th November 2009, 09:55
  4. Problem with closing the application
    By macbeth in forum Qt Programming
    Replies: 1
    Last Post: 27th May 2008, 15:02
  5. application freezes without crashing (and closing)
    By nass in forum Qt Programming
    Replies: 15
    Last Post: 25th September 2007, 11:21

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.