Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Looping inside console application

Looping inside console application

Scheduled Pinned Locked Moved Solved General and Desktop
3 Posts 2 Posters 1.0k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    sbielmann
    wrote on last edited by
    #1

    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

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      What about using a QStateMachine ? You'd need a custom transition to handle the "end/restart" use case.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      2
      • S Offline
        S Offline
        sbielmann
        wrote on last edited by
        #3

        Hello SGaist,

        thanks for the hint, this is exactly what I was looking for.

        1 Reply Last reply
        1

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved