Results 1 to 2 of 2

Thread: How to quit/exit console app?

  1. #1
    Join Date
    May 2018
    Posts
    19
    Thanks
    1
    Qt products
    Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default How to quit/exit console app?

    Qt Code:
    1. #include <QCoreApplication>
    2. #include "MainProcess.h"
    3. int main(int argc, char *argv[])
    4. {
    5. QCoreApplication a(argc, argv);
    6. MainProcess mainProcess;
    7. mainProcess.process();
    8. return a.exec();
    9. }
    To copy to clipboard, switch view to plain text mode 
    I called
    Qt Code:
    1. qApp->exit();
    To copy to clipboard, switch view to plain text mode 
    Or
    Qt Code:
    1. qApp->quit();
    To copy to clipboard, switch view to plain text mode 
    But my console app don't exit.

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: How to quit/exit console app?

    mainProcess.process();
    If this is what is doing all the work in your console app, then it is happening -before- your qApp event loop is running (a.exec()), and any QCoreApplication slot that you call from inside of process() will have no effect.

    You might be able to get around this problem by starting process() from a single-shot QTimer:

    Qt Code:
    1. int main(int argc, char *argv[])
    2. {
    3. QCoreApplication a(argc, argv);
    4. MainProcess mainProcess;
    5.  
    6. QTimer::singleShot( 0, &mainProcess, &MainProcess::process );
    7.  
    8. return a.exec();
    9. }
    To copy to clipboard, switch view to plain text mode 

    The reason this works is because the QTimer will not actually fire until the Qt event loop is running (a.exec()), and once the event loop is running your code can invoke slots.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

Similar Threads

  1. Replies: 0
    Last Post: 23rd March 2017, 15:48
  2. Correct way to quit a console application
    By c1223 in forum Qt Programming
    Replies: 4
    Last Post: 22nd November 2014, 19:51
  3. Replies: 3
    Last Post: 7th April 2011, 11:09
  4. Quit, Exit qApp (program) if error?
    By Arsenic in forum Newbie
    Replies: 13
    Last Post: 30th September 2008, 12:59
  5. QThread exit()/quit() question
    By TheKedge in forum Qt Programming
    Replies: 1
    Last Post: 28th August 2006, 14:38

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.