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:

    1. send request over network
    2. handle response
    3. 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


  • Lifetime Qt Champion

    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.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.