How to exit Qt console application?
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?
Code:
#include <QtCore/QCoreApplication>
#include <sasaja.h>
#include <stdio.h>
#include <iostream>
#include <QString>
#include <string.h>
int main(int argc, char *argv[])
{
std::cout << "world" << "ziauru";
std::cout << "hello";
std::cout << std::flush;
Sasaja sasaja;
return a.exec();
}
Thanks.
Re: How to exit Qt console application?
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.
Re: How to exit Qt console application?
If you don't need an event loop then just don't call QCoreApplication::exec().
Re: How to exit Qt console application?
Re: How to exit Qt console application?
Quote:
Originally Posted by
wysota
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:
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.
}
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 like this:
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
}
Hope this will help other people in the future too and thanks again to everyone who contributed to this.
Re: How to exit Qt console application?
Re: How to exit Qt console application?
Although for an exact answer isn't needed as standard C++ compilers assume you mean precisely that if no return value is specified.
Re: How to exit Qt console application?
Quote:
Originally Posted by
wysota
Would you even need the line in that case? I'm having similar confusion as I recently wrote about here:
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.
Re: How to exit Qt console application?
Quote:
Originally Posted by
neuronet
Would you even need the
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,
_
Re: How to exit Qt console application?
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.
So your main function would look like this:
Code:
#include <QTimer>
int main(int argc, char *argv[])
{
Sasaja sasaja;
QTimer::singleShot(0,
&sasaja,
SLOT(DoWork
()));
return a.exec();
}
Karl
Re: How to exit Qt console application?
What is the advantage of your code over this one?
Code:
int main(int argc, char **argv) {
Sasaja sasaja;
return sasaja.DoWork();
}
Re: How to exit Qt console application?
The difference is there is no event loop running.
Whether that is an advantage or not depends on what DoWork does.
Karl
Re: How to exit Qt console application?
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.
Re: How to exit Qt console application?
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
Re: How to exit Qt console application?
Quote:
Originally Posted by
KaptainKarl
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?