Results 1 to 15 of 15

Thread: How to exit Qt console application?

  1. #1
    Join Date
    Aug 2010
    Posts
    4
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default How to exit Qt console application?

    Okay, I may be stupid, but I didn't manage to find anything that worked for me regarding this question. I want my Qt console application to exit after it has finished all the work, in this case after it's finished work it's still running and I need to force exit by clicking <Ctrl+C> (I'm on linux and running it from terminal). So here's the code, how do I supply it so that it exits?

    Qt Code:
    1. #include <QtCore/QCoreApplication>
    2. #include <sasaja.h>
    3. #include <stdio.h>
    4. #include <iostream>
    5. #include <QString>
    6. #include <string.h>
    7.  
    8. int main(int argc, char *argv[])
    9. {
    10. QCoreApplication a(argc, argv);
    11. std::cout << "world" << "ziauru";
    12. std::cout << "hello";
    13. std::cout << std::flush;
    14. Sasaja sasaja;
    15. return a.exec();
    16. }
    To copy to clipboard, switch view to plain text mode 

    Thanks.

  2. #2
    Join Date
    Oct 2009
    Location
    Germany
    Posts
    120
    Thanked 42 Times in 41 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: How to exit Qt console application?

    According to documentation QCoreApplication::quit() or QCoreApplication::exit(int) should do the job. Call one of those static functions inside your code to shut down the application.

  3. #3
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,364
    Thanks
    3
    Thanked 5,013 Times in 4,791 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: How to exit Qt console application?

    If you don't need an event loop then just don't call QCoreApplication::exec().
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  4. #4
    Join Date
    Oct 2009
    Posts
    364
    Thanks
    10
    Thanked 37 Times in 36 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: How to exit Qt console application?

    my post was irrelevant
    Last edited by schnitzel; 28th February 2011 at 23:39.

  5. #5
    Join Date
    Aug 2010
    Posts
    4
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: How to exit Qt console application?

    Quote Originally Posted by wysota View Post
    If you don't need an event loop then just don't call QCoreApplication::exec().
    OK, thanks to everyone's answers, this was actually the explanation, to other people for the future I'll try to explain in simple terms how to exit the application, let's say we have an application that looks like this:
    Qt Code:
    1. #include <QtCore/QCoreApplication>
    2. #include <sasaja.h>
    3.  
    4. int main(int argc, char *argv[])
    5. {
    6. QCoreApplication a(argc, argv);
    7. Sasaja sasaja;
    8. return a.exec(); // This application would actually exit by itself after all the tasks have been finished if this line was not there.
    9. }
    To copy to clipboard, switch view to plain text mode 

    So what do we do if we want the application to exit by itself after it's done it's jobs? Yeah, that's right, just remove or comment that line with
    return a.exec();
    like this:
    Qt Code:
    1. #include <QtCore/QCoreApplication>
    2. #include <sasaja.h>
    3.  
    4. int main(int argc, char *argv[])
    5. {
    6. QCoreApplication a(argc, argv);
    7. Sasaja sasaja;
    8. //return a.exec(); // now that this line is commented, the application will exit
    9. }
    To copy to clipboard, switch view to plain text mode 

    Hope this will help other people in the future too and thanks again to everyone who contributed to this.

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,364
    Thanks
    3
    Thanked 5,013 Times in 4,791 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: How to exit Qt console application?

    You could at least
    Qt Code:
    1. return 0;
    To copy to clipboard, switch view to plain text mode 
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  7. #7
    Join Date
    Aug 2011
    Posts
    1
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: How to exit Qt console application?

    Although for an exact answer
    Qt Code:
    1. return 0;
    To copy to clipboard, switch view to plain text mode 
    isn't needed as standard C++ compilers assume you mean precisely that if no return value is specified.

  8. #8
    Join Date
    Jun 2014
    Posts
    98
    Thanks
    43
    Thanked 4 Times in 4 Posts
    Platforms
    Windows

    Default Re: How to exit Qt console application?

    Quote Originally Posted by wysota View Post
    If you don't need an event loop then just don't call QCoreApplication::exec().
    Would you even need the
    Qt Code:
    1. QCoreApplication a(argc, argv);
    To copy to clipboard, switch view to plain text mode 
    line in that case? I'm having similar confusion as I recently wrote about here:
    How to control a pure console application
    I still don't feel like I understand how to properly initialize/kill/execute console applications in PyQt/PySide.

  9. #9
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,419
    Thanks
    37
    Thanked 1,546 Times in 1,496 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: How to exit Qt console application?

    Quote Originally Posted by neuronet View Post
    Would you even need the
    Qt Code:
    1. QCoreApplication a(argc, argv);
    To copy to clipboard, switch view to plain text mode 
    line in that case?
    It depends in whether you use any Qt functionality that expects a Qt application instance to exist.
    You can try without and add it when needed.

    As Wysota wrote, even if you've create an instance you don't have to run the event loop if you don't need that part of the application object's functionality.
    Like in the case of the applicaiton in your stackoverflow posting.

    Cheers,
    _

  10. #10
    Join Date
    Mar 2006
    Location
    Jarrell, Texas, USA
    Posts
    70
    Thanks
    7
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: How to exit Qt console application?

    For Qt Console apps, I have found it best to do the following:

    1. Create a class with a void public slot that represents your main thread (or only thread). The slot should always exit with something like this:
    Qt Code:
    1. QCoreApplication::exit(#); // Where # is the exit code
    2. return;
    To copy to clipboard, switch view to plain text mode 
    2. Use a one shot timer in the main function to call the slot.

    So your main function would look like this:
    Qt Code:
    1. #include <QTimer>
    2. int main(int argc, char *argv[])
    3. {
    4. QCoreApplication a(argc, argv);
    5. Sasaja sasaja;
    6. QTimer::singleShot(0, &sasaja, SLOT(DoWork()));
    7. return a.exec();
    8. }
    To copy to clipboard, switch view to plain text mode 

    Karl

  11. #11
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,364
    Thanks
    3
    Thanked 5,013 Times in 4,791 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: How to exit Qt console application?

    What is the advantage of your code over this one?

    Qt Code:
    1. int main(int argc, char **argv) {
    2. QCoreApplication a(argc, argv);
    3. Sasaja sasaja;
    4. return sasaja.DoWork();
    5. }
    To copy to clipboard, switch view to plain text mode 
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  12. #12
    Join Date
    Mar 2006
    Location
    Jarrell, Texas, USA
    Posts
    70
    Thanks
    7
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: How to exit Qt console application?

    The difference is there is no event loop running.
    Whether that is an advantage or not depends on what DoWork does.

    Karl

  13. #13
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,364
    Thanks
    3
    Thanked 5,013 Times in 4,791 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: How to exit Qt console application?

    In your version the event loop is not running as well since you exit it immediately after handling the slot which is started immediately after you call exec(). The only difference would be if the constructor of Sasaja scheduled any events by itself that would execute before the DoWork slot did.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  14. #14
    Join Date
    Mar 2006
    Location
    Jarrell, Texas, USA
    Posts
    70
    Thanks
    7
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: How to exit Qt console application?

    We use this method to write server daemons.
    We create a private slot that watches for signals that tell the application to exit.
    Without the signal, the program continues to run.

    Karl

  15. #15
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,364
    Thanks
    3
    Thanked 5,013 Times in 4,791 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: How to exit Qt console application?

    Quote Originally Posted by KaptainKarl View Post
    We use this method to write server daemons.
    We create a private slot that watches for signals that tell the application to exit.
    Without the signal, the program continues to run.
    So what is the purpose of the single-shot timer? How does the program "continue to run" if while you exit the slot, you call application's exit() slot which quits the event loop? Could you provide a minimal compilable example demonstrating what you mean?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. Console application beginning
    By Raccoon29 in forum Newbie
    Replies: 6
    Last Post: 24th March 2012, 00:42
  2. about console application
    By jirach_gag in forum Qt Programming
    Replies: 2
    Last Post: 5th January 2012, 12:39
  3. open console application
    By hirakjyoti in forum Newbie
    Replies: 1
    Last Post: 30th January 2011, 17:34
  4. Replies: 2
    Last Post: 21st November 2010, 19:03
  5. Exiting a Qt Console Application
    By nbetcher in forum Qt Programming
    Replies: 4
    Last Post: 23rd March 2009, 22:03

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.