How to exit a Qt console app from an inner class? [ SOLVED ]
-
But now you have to delete these two, in your case you can use:
int main(int argc, char *argv[]) { QCoreApplication app(argc, argv); InternetService service; QObject::connect(&service, SIGNAL(finished()), &app, SLOT(quit())); return app.exec(); }
-
@SGaist I made a little modification in my code but now it is not working and I can't figure out why?
This is my situation:int main(int argc, char *argv[]) { QCoreApplication app(argc, argv); InternetService service; QObject::connect(&service, SIGNAL(finished()), &app, SLOT(quit())); service.verifyEnv(); return app.exec(); }
Just for testing, inside the verifyEnv() (public) method of the InternetService class, I only added this line:
void InternetService::verifyEnv() { emit finished(); }
Nevertheless, the QCoreApplication object never quits. Why?
-
Got any message when starting the application in your terminal ?
-
@xtingray In your new code that isn't working you are sending the signal
emit finished()
before you are in your event loop. So AFAIK that signal isn't caught once the loop starts.The loop i.e.
app.exec()
needs to be running before you callservice.verifyEnv()
and it's resultingemit finished()
.Since you are trying to exit right away by calling
verifyEnv
there is no need for theapp.exec
in that case, justreturn 0
from your main and never enter the loop. -
First at all, thank you for all your suggestions. My comments:
- I tried to replace return app.exec() with return 0. The problem is that the program quits as far as I run it, even if I remove the signal instruction. As it is a service, I need it running all the time. I mean, the finished() signal is required but only for specific exceptions.
- I tried using the method qApp->quit(); too and surprisingly the application keeps running :O
I decided to write a "Hello World" example, in case some of you want to help me to resolve this mystery:
http://maefloresta.com/portal/files/test.zipThe challenge is quite simple: If I run a first instance of the example, the service must keep running indefinitely. If I run a (concurrent) second instance, it has to quit as soon as it discovers that the port is busy.
Anyone interested in to try it? Thanks.
-
@xtingray Make InternetService inherit from QObject and verifyEnv a slot. Then invoke the method as follows:
int main(int argc, char *argv[])
{
QCoreApplication app(argc, argv);
InternetService service;
QObject::connect(&service, SIGNAL(finished()), &app, SLOT(quit()));
QMetaObject::invokeMethod(&service, "verifyEnv", Qt::QueuedConnection);
return app.exec();
}As an alternative, you could use the QtSingleApplication solution to restrict your application to a single instance only.
-
I use QtSingleApplication for that stuff. It's probably better than just checking a port as you may want to do something if the port is busy. Just because a port is busy doesn't mean your service is actually running and is the one using it.
It could be another app. You could have permission issues and be unable to bind to that port. Or a host of other things.
-
Finally, the best/clean way I found to deal with my requirement was this:
- Inside the main.cpp and before I start my service, I make all the verifications I need... if something goes wrong, then I call "return 0;" and that's it, a clean exit. If everything goes well, then I start my service. End of the story.
Thank you for your advices guys! :)