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. Problems with eventloop in CLI application
Forum Updated to NodeBB v4.3 + New Features

Problems with eventloop in CLI application

Scheduled Pinned Locked Moved Solved General and Desktop
2 Posts 2 Posters 422 Views
  • 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.
  • H Offline
    H Offline
    HalfTough
    wrote on last edited by
    #1

    I'm making CLI application in qt and I was doing fine until I needed to download some files from the internet. Slot connected to QNetworkAccessManager::finished wouldn't trigger unless I run qApplication.exec() at the end of my main function.

    So I did, but that created another problem

    int main(int argc, char **argv){
        QApplication qApplication(argc, argv);
        ClassForDownloading::download();
    
        doTheRestOfstuff();
    
        return qApplication.exec();
    }
    

    doTheRestOfStuff() waits for download to finish (i use QSemaphore for that), but because I need to call qApplication.exec() in order for download to start it waits forever.

    So I put restOfStuff into a Thread

    int main(int argc, char **argv){
        QApplication qApplication(argc, argv);
        ClassForDownloading::download();
    
        RestOfstuffThread *restOfStuffThread = new RestOfStuffThread();
        restOfStuffThread->start()
    
        return qApplication.exec();
    }
    

    And now everything works, except it never closes, even if I call QCoreApplication::quit(); or QCoreApplication::exit() inside of the thread.

    How do I handle qt Event Loop correctly?

    jsulmJ 1 Reply Last reply
    0
    • H HalfTough

      I'm making CLI application in qt and I was doing fine until I needed to download some files from the internet. Slot connected to QNetworkAccessManager::finished wouldn't trigger unless I run qApplication.exec() at the end of my main function.

      So I did, but that created another problem

      int main(int argc, char **argv){
          QApplication qApplication(argc, argv);
          ClassForDownloading::download();
      
          doTheRestOfstuff();
      
          return qApplication.exec();
      }
      

      doTheRestOfStuff() waits for download to finish (i use QSemaphore for that), but because I need to call qApplication.exec() in order for download to start it waits forever.

      So I put restOfStuff into a Thread

      int main(int argc, char **argv){
          QApplication qApplication(argc, argv);
          ClassForDownloading::download();
      
          RestOfstuffThread *restOfStuffThread = new RestOfStuffThread();
          restOfStuffThread->start()
      
          return qApplication.exec();
      }
      

      And now everything works, except it never closes, even if I call QCoreApplication::quit(); or QCoreApplication::exit() inside of the thread.

      How do I handle qt Event Loop correctly?

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by jsulm
      #2

      @HalfTough Why don't you instead emit a signal when ClassForDownloading::download(); is finished which you then connect to a slot in another class which executes doTheRestOfstuff()? No need for any threads.
      This would be the way to do it in an event based application like Qt app. There is no need to "wait" for something, instead you simply react on events (signals).
      Something like

      int main(int argc, char **argv){
          QApplication qApplication(argc, argv);
      
          ClassForDownloading downloading;
          ClassTodoStuff stuff;
          connect(&downloading, SIGNAL("finished()"), &stuff, SLOT("doTheRestOfstuff()"));
          downloading.download();
      
          return qApplication.exec();
      }

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      3

      • Login

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