If you don't need an event loop then just don't call QCoreApplication::exec().
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:
#include <QtCore/QCoreApplication> #include <sasaja.h> int main(int argc, char *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. }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 withlike this:return a.exec();
Qt Code:
#include <QtCore/QCoreApplication> #include <sasaja.h> int main(int argc, char *argv[]) { Sasaja sasaja; //return a.exec(); // now that this line is commented, the application will exit }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.
You could at least
Although for an exact answer isn't needed as standard C++ compilers assume you mean precisely that if no return value is specified.
Would you even need theline in that case? I'm having similar confusion as I recently wrote about here:Qt Code:
To copy to clipboard, switch view to plain text mode
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.
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,
_
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:
2. Use a one shot timer in the main function to call the slot.Qt Code:
return;To copy to clipboard, switch view to plain text mode
So your main function would look like this:
Qt Code:
#include <QTimer> int main(int argc, char *argv[]) { Sasaja sasaja; return a.exec(); }To copy to clipboard, switch view to plain text mode
Karl
What is the advantage of your code over this one?
Qt Code:
int main(int argc, char **argv) { Sasaja sasaja; return sasaja.DoWork(); }To copy to clipboard, switch view to plain text mode
The difference is there is no event loop running.
Whether that is an advantage or not depends on what DoWork does.
Karl
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.
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
Bookmarks