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

Problems with eventloop in CLI application

Scheduled Pinned Locked Moved Solved General and Desktop
2 Posts 2 Posters 410 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 1 Mar 2018, 09:50 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?

    J 1 Reply Last reply 1 Mar 2018, 10:04
    0
    • H HalfTough
      1 Mar 2018, 09:50

      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?

      J Offline
      J Offline
      jsulm
      Lifetime Qt Champion
      wrote on 1 Mar 2018, 10:04 last edited by jsulm 3 Jan 2018, 10:07
      #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

      1/2

      1 Mar 2018, 09:50

      • Login

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