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. Why my app don't work?
Forum Update on Monday, May 27th 2025

Why my app don't work?

Scheduled Pinned Locked Moved General and Desktop
7 Posts 3 Posters 1.9k 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.
  • A Offline
    A Offline
    Armin
    wrote on last edited by
    #1

    Hi
    Why my app don't work?
    I want to get data from download link and write to directory

    {
    QNetworkAccessManager netaccessmanager1;
    QNetworkReply *reply = netaccessmanager1.get(QNetworkRequest(QUrl(ui->textEdit->toPlainText())));
    QFile file (urldirectory.toString() );
    file.open(QIODevice::WriteOnly);
    QDataStream out(&file);
    out << reply;
    }
    
    K 1 Reply Last reply
    0
    • A Armin

      Hi
      Why my app don't work?
      I want to get data from download link and write to directory

      {
      QNetworkAccessManager netaccessmanager1;
      QNetworkReply *reply = netaccessmanager1.get(QNetworkRequest(QUrl(ui->textEdit->toPlainText())));
      QFile file (urldirectory.toString() );
      file.open(QIODevice::WriteOnly);
      QDataStream out(&file);
      out << reply;
      }
      
      K Offline
      K Offline
      koahnig
      wrote on last edited by
      #2

      @Armin

      I am suggesting to start out with one of the examples in detailed description of QNetworkManager. Also this example may be of help for you.

      For the first two statement, it takes usually a moment for teh reply, but you are not waiting neither are you using a connect statement.

      Vote the answer(s) that helped you to solve your issue(s)

      A 1 Reply Last reply
      1
      • K koahnig

        @Armin

        I am suggesting to start out with one of the examples in detailed description of QNetworkManager. Also this example may be of help for you.

        For the first two statement, it takes usually a moment for teh reply, but you are not waiting neither are you using a connect statement.

        A Offline
        A Offline
        Armin
        wrote on last edited by
        #3

        @koahnig Thanks
        So i send request by this code : QNetworkReply *reply = netaccessmanager1.get(QNetworkRequest(QUrl(ui->textEdit->toPlainText())));

        My code is wrong?

        K 1 Reply Last reply
        0
        • A Armin

          @koahnig Thanks
          So i send request by this code : QNetworkReply *reply = netaccessmanager1.get(QNetworkRequest(QUrl(ui->textEdit->toPlainText())));

          My code is wrong?

          K Offline
          K Offline
          koahnig
          wrote on last edited by
          #4

          @Armin

          the documentation says :
          Posts a request to obtain the contents of the target request and returns a new QNetworkReply object opened for reading which emits the readyRead() signal whenever new data arrives.

          The contents as well as associated headers will be downloaded.

          However, in your implementation the app is probably already exiting before the reply has been received. This means you are already past your out statement.
          Assuming that the rest of the code is not completely wrong, the "out << reply;" will output only the address where the reply is located, but not the content. You are creating a memory leak there too.

          Please check out the examples already given above. When you download this example you get a working example and start to modify to your needs. This shall ensure that get the proper understanding of what you are doing and how to do it next time.

          Vote the answer(s) that helped you to solve your issue(s)

          1 Reply Last reply
          2
          • VRoninV Offline
            VRoninV Offline
            VRonin
            wrote on last edited by VRonin
            #5

            Not ideal (as if an error occurs it will probably hang forever) but probably this works:

            
                QNetworkAccessManager netaccessmanager1;
                QFile file (urldirectory.toString() );
                file.open(QIODevice::WriteOnly);
                QNetworkReply *reply = netaccessmanager1.get(QNetworkRequest(QUrl(ui->textEdit->toPlainText())));
                QEventLoop blocker;
                QObject::connect(reply,&QNetworkReply::readyRead,[&file,&reply]()->void{file.write(reply->readAll());});
                QObject::connect(reply,&QNetworkReply::finished,[&file]()->void{file.close();});
                QObject::connect(reply,&QNetworkReply::finished,&blocker,&QEventLoop::quit);
                QObject::connect(reply,&QNetworkReply::finished,reply,&QNetworkReply::deleteLater);
                blocker.exec();
                
            

            "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
            ~Napoleon Bonaparte

            On a crusade to banish setIndexWidget() from the holy land of Qt

            A 1 Reply Last reply
            1
            • VRoninV VRonin

              Not ideal (as if an error occurs it will probably hang forever) but probably this works:

              
                  QNetworkAccessManager netaccessmanager1;
                  QFile file (urldirectory.toString() );
                  file.open(QIODevice::WriteOnly);
                  QNetworkReply *reply = netaccessmanager1.get(QNetworkRequest(QUrl(ui->textEdit->toPlainText())));
                  QEventLoop blocker;
                  QObject::connect(reply,&QNetworkReply::readyRead,[&file,&reply]()->void{file.write(reply->readAll());});
                  QObject::connect(reply,&QNetworkReply::finished,[&file]()->void{file.close();});
                  QObject::connect(reply,&QNetworkReply::finished,&blocker,&QEventLoop::quit);
                  QObject::connect(reply,&QNetworkReply::finished,reply,&QNetworkReply::deleteLater);
                  blocker.exec();
                  
              
              A Offline
              A Offline
              Armin
              wrote on last edited by
              #6

              @VRonin Thanks but this get errors :|

              1 Reply Last reply
              0
              • VRoninV Offline
                VRoninV Offline
                VRonin
                wrote on last edited by VRonin
                #7

                yep, sorry, file is not a pointer and you should use write instead of append. Updated the code above now.

                This example works:

                QNetworkAccessManager netaccessmanager1;
                    QFile file ("C:/Temp/Nyrss.xml" );
                    file.open(QIODevice::WriteOnly);
                    QNetworkReply *reply = netaccessmanager1.get(QNetworkRequest(QUrl("http://rss.nytimes.com/services/xml/rss/nyt/World.xml")));
                    QEventLoop blocker;
                    QObject::connect(reply,&QNetworkReply::readyRead,[&file,&reply]()->void{file.write(reply->readAll());});
                    QObject::connect(reply,&QNetworkReply::finished,[&file]()->void{file.close();});
                    QObject::connect(reply,&QNetworkReply::finished,&blocker,&QEventLoop::quit);
                    QObject::connect(reply,&QNetworkReply::finished,reply,&QNetworkReply::deleteLater);
                    blocker.exec();
                

                "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                ~Napoleon Bonaparte

                On a crusade to banish setIndexWidget() from the holy land of Qt

                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