PDA

View Full Version : How to quit/exit console app?



madawg
1st October 2018, 09:32
#include <QCoreApplication>
#include "MainProcess.h"
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
MainProcess mainProcess;
mainProcess.process();
return a.exec();
}
I called
qApp->exit(); Or
qApp->quit();
But my console app don't exit.

d_stranz
1st October 2018, 17:31
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:



int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
MainProcess mainProcess;

QTimer::singleShot( 0, &mainProcess, &MainProcess::process );

return a.exec();
}

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.