PDA

View Full Version : How to exit Qt console application?



arcelivez
28th February 2011, 19:27
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?



#include <QtCore/QCoreApplication>
#include <sasaja.h>
#include <stdio.h>
#include <iostream>
#include <QString>
#include <string.h>

int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
std::cout << "world" << "ziauru";
std::cout << "hello";
std::cout << std::flush;
Sasaja sasaja;
return a.exec();
}


Thanks.

ars
28th February 2011, 19:39
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.

wysota
28th February 2011, 19:49
If you don't need an event loop then just don't call QCoreApplication::exec().

schnitzel
28th February 2011, 19:51
my post was irrelevant

arcelivez
28th February 2011, 20:28
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:


#include <QtCore/QCoreApplication>
#include <sasaja.h>

int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
Sasaja sasaja;
return a.exec(); // This application would actually exit by itself after all the tasks have been finished if this line was not there.
}


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:


#include <QtCore/QCoreApplication>
#include <sasaja.h>

int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
Sasaja sasaja;
//return a.exec(); // now that this line is commented, the application will exit
}


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

wysota
28th February 2011, 22:00
You could at least

return 0;

styne666
17th August 2011, 21:43
Although for an exact answer
return 0; isn't needed as standard C++ compilers assume you mean precisely that if no return value is specified.

neuronet
28th July 2014, 13:31
If you don't need an event loop then just don't call QCoreApplication::exec().

Would you even need the
QCoreApplication a(argc, argv); line in that case? I'm having similar confusion as I recently wrote about here:
How to control a pure console application (http://stackoverflow.com/questions/24983783/proper-control-of-a-pure-console-application-in-pyside-pyqt)
I still don't feel like I understand how to properly initialize/kill/execute console applications in PyQt/PySide.

anda_skoa
28th July 2014, 14:03
Would you even need the
QCoreApplication a(argc, argv); 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,
_

KaptainKarl
29th July 2014, 21:15
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:

QCoreApplication::exit(#); // Where # is the exit code
return;
2. Use a one shot timer in the main function to call the slot.

So your main function would look like this:

#include <QTimer>
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
Sasaja sasaja;
QTimer::singleShot(0, &sasaja, SLOT(DoWork()));
return a.exec();
}

Karl

wysota
29th July 2014, 21:47
What is the advantage of your code over this one?


int main(int argc, char **argv) {
QCoreApplication a(argc, argv);
Sasaja sasaja;
return sasaja.DoWork();
}

KaptainKarl
29th July 2014, 22:34
The difference is there is no event loop running.
Whether that is an advantage or not depends on what DoWork does.

Karl

wysota
29th July 2014, 23:01
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.

KaptainKarl
29th July 2014, 23:24
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

wysota
30th July 2014, 08:51
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?