Results 1 to 3 of 3

Thread: Console application - exit application and order of code

  1. #1
    Join Date
    Jul 2010
    Location
    Poland
    Posts
    184
    Thanks
    70
    Thanked 7 Times in 6 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Console application - exit application and order of code

    Hello!

    I've got simple console application and I've got some questions.

    Qt Code:
    1. #include <QtCore/QCoreApplication>
    2. #include <QTextStream>
    3.  
    4. QString temp;
    5.  
    6. int main(int argc, char *argv[])
    7. {
    8. QTextStream out(stdout);
    9. temp = "test";
    10.  
    11. QCoreApplication a(argc, argv);
    12.  
    13. out << temp << endl;
    14.  
    15. return a.exec();
    16. }
    To copy to clipboard, switch view to plain text mode 

    First - is the order of code correct? Where should be "QCoreApplication a(argc, argv);"? As i assume "return a.exec();" should be at the end?
    What about exiting the application? When I run it in console, it works all the time - don't exits.

    thanks in advance
    best regards
    Tomasz

  2. #2
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Console application - exit application and order of code

    for that simple project you don't need a QCoreApplication. Simply call
    Qt Code:
    1. return 0;
    To copy to clipboard, switch view to plain text mode 
    and your program will exit. (alternatively you can call the quit slot of the applications object.)

    As to the order to avoid possible problems in future, put the initialization of the core application at the beginning of the main function.

  3. The following user says thank you to Lykurg for this useful post:

    Tomasz (21st November 2010)

  4. #3
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: Console application - exit application and order of code

    Just a note that you can call the quit slot before a.exec(), but it will not have any effect since the event loop is not running.
    Either call quit() after a.exec(), as in create objects that you instantiate in your main function and that use the event loop.
    Or use a timer, but then you don't really need the event loop in the first place I guess.

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. QT Console application QTextStream
    By Lis in forum Qt Programming
    Replies: 2
    Last Post: 6th November 2010, 06:18
  4. print something on console application
    By cemtopkaya in forum Qt Programming
    Replies: 2
    Last Post: 10th June 2010, 14:41
  5. Debugging a console application
    By Lorthirk in forum Qt Tools
    Replies: 1
    Last Post: 26th September 2009, 12:49

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.