Results 1 to 11 of 11

Thread: QConsoleApplication,aboutToQuit and "signal" do not hit the breakpoints...

  1. #1
    Join Date
    Nov 2009
    Location
    US, Midwest
    Posts
    215
    Thanks
    62
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default QConsoleApplication,aboutToQuit and "signal" do not hit the breakpoints...

    Following code, running in qtCreator (Windows 10) in the debug mode, does not reach breakpoints "brakepoint1" or "breakpoint2" upon clicking on "x" console window.
    What can be the case?
    Thanks!

    Qt Code:
    1. #include <QCoreApplication>
    2. #include <iostream>
    3. #include <signal.h>
    4.  
    5. void cleanup(int sig)
    6. {
    7. std::cout << sig << "quitting \n"; // breakpoint 2
    8. }
    9.  
    10. int main(int argc, char *argv[])
    11. {
    12. QCoreApplication a(argc, argv);
    13. QObject::connect(&a, &QCoreApplication::aboutToQuit, [&]() {
    14. std::cout << "about to quit\n"; //breakpoint 1
    15. });
    16. signal(SIGINT, cleanup);
    17. signal(SIGABRT, cleanup);
    18. signal(SIGTERM, cleanup);
    19.  
    20. std::cout<< "about to run\n";
    21. return a.exec();
    22. std::cout<< "exiting\n";
    23. }
    To copy to clipboard, switch view to plain text mode 

  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: QConsoleApplication,aboutToQuit and "signal" do not hit the breakpoints...

    Maybe the program is killed with SIGKILL when you remove the terminal it is connected to?

    Have you tried if it works if you terminate the program yourself?

    Cheers,
    _

  3. #3
    Join Date
    Nov 2009
    Location
    US, Midwest
    Posts
    215
    Thanks
    62
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QConsoleApplication,aboutToQuit and "signal" do not hit the breakpoints...

    I think that SIGKILL is SIGTERM in Windows, so if it is true then I am tracking this signal.

    No, I did not try to terminate the console programmatically, I know it will work. I am interested in doing proper clean up when the console is terminated via "x"-closure. Do you think there is a way to do it?

  4. #4
    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: QConsoleApplication,aboutToQuit and "signal" do not hit the breakpoints...

    You'll have to check the Windows documentation, it looks like the program simply gets killed.

    Cheers,
    _

  5. #5
    Join Date
    Nov 2009
    Location
    US, Midwest
    Posts
    215
    Thanks
    62
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QConsoleApplication,aboutToQuit and "signal" do not hit the breakpoints...

    Right, that's what I do, but still - shouldn't it be a cross-platform Qt-solution that addresses the proper handling of such case? I'll try to post the question in qt forum.

  6. #6
    Join Date
    Dec 2009
    Location
    New Orleans, Louisiana
    Posts
    791
    Thanks
    13
    Thanked 153 Times in 150 Posts
    Qt products
    Qt5
    Platforms
    MacOS X

    Default Re: QConsoleApplication,aboutToQuit and "signal" do not hit the breakpoints...

    Your question has nothing to do with Qt and is operating system specific (for signal handling).

    Edit: Here are the signals I trap on Mac OSX which works great: SIGHUP, SIGINT, SIGQUIT, SIGABRT, and SIGTERM. My guess is your app isn't receiving the aboutToQuit signal because a QCoreApplication::quit() has not been done.
    Last edited by jefftee; 23rd October 2016 at 19:27.
    I write the best type of code possible, code that I want to write, not code that someone tells me to write!

  7. #7
    Join Date
    Nov 2009
    Location
    US, Midwest
    Posts
    215
    Thanks
    62
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QConsoleApplication,aboutToQuit and "signal" do not hit the breakpoints...

    jefftee:

    I disagree, it has everything to do with Qt being a cross-platform framework. Trapping the signals was not my first choice, I thought that "aboutToQuit" signal will be fired, and it is not, at least in the code sample that I published. Then I moved to signals.
    I am happy to simplify my question: forget signals. Why in this case, when I am closing the console window by clicking on "x", the aboutToQuit signal is not fired? Does the sample that I published work on Mac?

  8. #8
    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: QConsoleApplication,aboutToQuit and "signal" do not hit the breakpoints...

    Quote Originally Posted by TorAn View Post
    I disagree, it has everything to do with Qt being a cross-platform framework.
    It is, but it is not changing how operating systems work.
    It is just part of processes on these operating systems.

    Quote Originally Posted by TorAn View Post
    Trapping the signals was not my first choice, I thought that "aboutToQuit" signal will be fired, and it is not, at least in the code sample that I published.
    Are you sure it is not fired when the application quits?

    Have you called QCoreApplication::quit() and the signal did not get emitted?

    Quote Originally Posted by TorAn View Post
    Why in this case, when I am closing the console window by clicking on "x", the aboutToQuit signal is not fired?
    Apparently the process is killed, no?
    How would the process execute code when that happens?

    Cheers,
    _

  9. #9
    Join Date
    Dec 2009
    Location
    New Orleans, Louisiana
    Posts
    791
    Thanks
    13
    Thanked 153 Times in 150 Posts
    Qt products
    Qt5
    Platforms
    MacOS X

    Default Re: QConsoleApplication,aboutToQuit and "signal" do not hit the breakpoints...

    You do realize that Qt doesn't abstract *everything* on all OS's, right? Signal handling is one of those things that Qt does not provide any coverage for.

    The simple answer to your question is that the aboutToQuit signal is never received because your program has never done a QCoreApplication::quit. Being killed by the OS is not the same as the application shutting down. If you want to have your app cleanup when being killed by closing the window, you'll have to handle the correct signal, which my guess is SIGKILL.

    You mention that you *think* SIGKILL is the same as SIGTERM, but it would be easy enough for you to try SIGKILL to see for certain.

    Edit: A quick google shows that the signal you are looking for is SIGBREAK. Give that a try.
    Last edited by jefftee; 23rd October 2016 at 22:43.
    I write the best type of code possible, code that I want to write, not code that someone tells me to write!

  10. The following user says thank you to jefftee for this useful post:

    TorAn (24th October 2016)

  11. #10
    Join Date
    Nov 2009
    Location
    US, Midwest
    Posts
    215
    Thanks
    62
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QConsoleApplication,aboutToQuit and "signal" do not hit the breakpoints...

    jefftee:

    First of all, thank you very much for the help! Yes, SIGBREAK worked.
    Now, to the points that you mentioned:

    SIGKILL is not available on windows, at least I did not find it in signal.h, that's why I mention the possibility that SIGTERM might be the same thing.

    But more importantly no, I absolutely did not realize that Qt does not handle the "abnormal" termination. I tried to use "aboutToQuit" because, as documentation states:
    This signal is emitted when the application is about to quit the main event loop, e.g. when the event loop level drops to zero. This may happen either after a call to quit() from inside the application or when the users shuts down the entire desktop session. The signal is particularly useful if your application has to do some last-second cleanup.
    I thought that it applies to the termination of the console window.

  12. #11
    Join Date
    Dec 2009
    Location
    New Orleans, Louisiana
    Posts
    791
    Thanks
    13
    Thanked 153 Times in 150 Posts
    Qt products
    Qt5
    Platforms
    MacOS X

    Default Re: QConsoleApplication,aboutToQuit and "signal" do not hit the breakpoints...

    Glad you got it working. My interpretation of the aboutToClose documentation is for a normal (clean) shutdown of your app. i.e. your app calls QCoreApplication::quit() or when the OS informs your app that it's shutting down (or logging off, etc). Those would be considered orderly shutdowns of your console app, but clicking the X on the console window, terminating through the task manager, or kill command all would not result in your app going through normal shutdown, etc.
    I write the best type of code possible, code that I want to write, not code that someone tells me to write!

Similar Threads

  1. Replies: 1
    Last Post: 20th November 2015, 11:02
  2. QSqlError("", "Parameter count mismatch", "")
    By Alberto7 in forum Newbie
    Replies: 2
    Last Post: 9th October 2015, 23:09
  3. Replies: 3
    Last Post: 16th March 2015, 08:31
  4. Translation QFileDialog standart buttons ("Open"/"Save"/"Cancel")
    By victor.yacovlev in forum Qt Programming
    Replies: 4
    Last Post: 24th January 2008, 20:05
  5. Signal defined in "a.h" can not emit in "b.cpp"
    By Shawn in forum Qt Programming
    Replies: 9
    Last Post: 21st May 2007, 17:55

Tags for this Thread

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.