Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Update: Forum Guidelines & Code of Conduct


    Qt World Summit: Early-Bird Tickets

    Unsolved I want to block my program until finishing the download !

    General and Desktop
    3
    8
    1155
    Loading More Posts
    • 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.
    • bluemmb
      bluemmb last edited by

      Hi . I am new in QT and I wrote a code for download file using QNetworkAccessManager , but I want to block my code until the download finishs.

      this is my main :

      int main(int argc, char *argv[])
      {
          QCoreApplication app(argc, argv);
          MyClass* obj = new MyClass();
      
          obj->downloadToQString(QUrl( "http://codeforces.com" ));
          if ( obj->error == false )
              qDebug() << obj->result;
          else
              qDebug() << "Error : " <<obj->errorString;
      
          app.exec();
      }
      

      my request for download :

      void downloadToQString(QUrl url)
          {
              error = false;
              errorString = "";
              result = "";
      
              reply = nam.get(QNetworkRequest(url));
              QObject::connect(reply, SIGNAL(finished()), this, SLOT(onFinishedQString()));
          }
      

      because download don't make block the program , it continues it's running and main prints empty string in output.

      I know Threading is good but it will be hard for me to handle it.

      1 Reply Last reply Reply Quote 0
      • mistralegna
        mistralegna last edited by mistralegna

        Hello,

        In your method downloadToQString, you connect your QNetworkReply* to a slot of your class MyClass. Why don't you print the result in this method ?

        bluemmb 1 Reply Last reply Reply Quote 0
        • bluemmb
          bluemmb @mistralegna last edited by

          @mistralegna Hi . I wrote this code just for testing downloading does work correctly or not.

          I will use this class in GUI project not in console. first I must prevent user from doing anything before download completes and after knowing the result of download , decide what i have to do.

          bluemmb raven-worx 2 Replies Last reply Reply Quote 0
          • bluemmb
            bluemmb @bluemmb last edited by

            @bluemmb sorry I mean Widget Application.

            1 Reply Last reply Reply Quote 0
            • raven-worx
              raven-worx Moderators @bluemmb last edited by

              @bluemmb
              Basically this is possible (e.g. using a "local" QEventLoop), but since you mentioned that you want to use it in GUI it would be way better usability/UX design if you show for example a modal dialog as long as the download isn't finished.

              You could use a QProgressDialog or even a custom QDialog subclass and set them modal. So the user knows that something is going on instead of a simply unresponding application.

              --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
              If you have a question please use the forum so others can benefit from the solution in the future

              bluemmb 1 Reply Last reply Reply Quote 3
              • bluemmb
                bluemmb @raven-worx last edited by

                @raven-worx thanks , i have been doing what you have told.
                But i really want to know how we can do it , is there any way at all ?

                raven-worx 1 Reply Last reply Reply Quote 0
                • raven-worx
                  raven-worx Moderators @bluemmb last edited by

                  @bluemmb

                  QNetworkReply* reply = nam->get( request )  ;
                  
                  QEventLoop loop;
                  connect(reply, SIGNAL(finished()), &loop, SLOT(quit()));
                  loop.exec();
                  

                  --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
                  If you have a question please use the forum so others can benefit from the solution in the future

                  bluemmb 1 Reply Last reply Reply Quote 4
                  • bluemmb
                    bluemmb @raven-worx last edited by

                    @bluemmb thanks very much , i used your code in another place :)

                    1 Reply Last reply Reply Quote 0
                    • First post
                      Last post