Solved Looping inside console application
-
Hello All,
I wonder if somebody can advice what the best practice is to loop inside a Qt console application.
My test example below should:- send request over network
- handle response
- if response is "quit" leave, else go to point 1
Here the example code:
Test::Test() : network(this) { QObject::connect(network, SIGNAL(handleResponse(const QString &)), this, SLOT(slotHandleResponse(const QString &))); } void Test::run() { network->sendRequest(); } void Test::slotHandleResponse(const QString response) { if(response == "quit") { emit finished(); } else { // send next request run(); } } int main(int argc, char **argv) { QCoreApplication app(argc, argv); Test test(); QObject::connect(&test, SIGNAL(finished()), &app, SLOT(quit())); QTimer::singleShot(0, &test, SLOT(run())); return(app.exec()); }
Is it ok to call run() from within slotHandleResponse() again and again until I get "quit" ? Or should I use QTimer::singleShot() again ? Or something else ?
Thanks for any help
-
Hi,
What about using a QStateMachine ? You'd need a custom transition to handle the "end/restart" use case.
-
Hello SGaist,
thanks for the hint, this is exactly what I was looking for.