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. I want to block my program until finishing the download !

I want to block my program until finishing the download !

Scheduled Pinned Locked Moved Unsolved General and Desktop
8 Posts 3 Posters 1.6k 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.
  • bluemmbB Offline
    bluemmbB Offline
    bluemmb
    wrote on last edited by
    #1

    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
    0
    • mistralegnaM Offline
      mistralegnaM Offline
      mistralegna
      wrote on last edited by mistralegna
      #2

      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 ?

      bluemmbB 1 Reply Last reply
      0
      • mistralegnaM 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 ?

        bluemmbB Offline
        bluemmbB Offline
        bluemmb
        wrote on last edited by
        #3

        @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.

        bluemmbB raven-worxR 2 Replies Last reply
        0
        • bluemmbB bluemmb

          @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.

          bluemmbB Offline
          bluemmbB Offline
          bluemmb
          wrote on last edited by
          #4

          @bluemmb sorry I mean Widget Application.

          1 Reply Last reply
          0
          • bluemmbB bluemmb

            @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.

            raven-worxR Offline
            raven-worxR Offline
            raven-worx
            Moderators
            wrote on last edited by
            #5

            @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

            bluemmbB 1 Reply Last reply
            3
            • raven-worxR raven-worx

              @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.

              bluemmbB Offline
              bluemmbB Offline
              bluemmb
              wrote on last edited by
              #6

              @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-worxR 1 Reply Last reply
              0
              • bluemmbB bluemmb

                @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-worxR Offline
                raven-worxR Offline
                raven-worx
                Moderators
                wrote on last edited by
                #7

                @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

                bluemmbB 1 Reply Last reply
                4
                • raven-worxR raven-worx

                  @bluemmb

                  QNetworkReply* reply = nam->get( request )  ;
                  
                  QEventLoop loop;
                  connect(reply, SIGNAL(finished()), &loop, SLOT(quit()));
                  loop.exec();
                  
                  bluemmbB Offline
                  bluemmbB Offline
                  bluemmb
                  wrote on last edited by
                  #8

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

                  1 Reply Last reply
                  0

                  • Login

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