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. How can i write any data on directory
Forum Updated to NodeBB v4.3 + New Features

How can i write any data on directory

Scheduled Pinned Locked Moved Solved General and Desktop
15 Posts 4 Posters 3.5k Views 1 Watching
  • 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.
  • mrjjM Offline
    mrjjM Offline
    mrjj
    Lifetime Qt Champion
    wrote on last edited by
    #2

    Hi
    1 - it will send a signal when something is read (replyFinished) or error happened.
    void getDoc::on_pushButton_2_clicked()
    {
    manager = new QNetworkAccessManager(this);
    connect(manager, SIGNAL(finished(QNetworkReply*)), this, SLOT(replyFinished(QNetworkReply*)));
    manager->get(QNetworkRequest(QUrl("http://www.google.com")));
    }

    void getDoc::replyFinished(QNetworkReply *reply)
    {
    // qDebug() << reply->error();
    QByteArray data=reply->readAll();
    }

    2-
    use QFile and qdatastream to save it to a file.

    A 5 Replies Last reply
    6
    • X Offline
      X Offline
      Xiami
      wrote on last edited by Xiami
      #3
          auto reply = Manager.get(QNetworkRequest(QUrl("...")));
          connect(reply,&QNetworkReply::finished,this,[&]{
              auto array = ((QNetworkReply*)sender())->readAll();
              sender()->deleteLater();
              if(array.isEmpty())return;
              QFile file(QCryptographicHash::hash(array,QCryptographicHash::Md5));
              if(file.open(QIODevice::WriteOnly)){
                  file.write(array);
                  file.close();
              }
          });
      
      1 Reply Last reply
      2
      • mrjjM mrjj

        Hi
        1 - it will send a signal when something is read (replyFinished) or error happened.
        void getDoc::on_pushButton_2_clicked()
        {
        manager = new QNetworkAccessManager(this);
        connect(manager, SIGNAL(finished(QNetworkReply*)), this, SLOT(replyFinished(QNetworkReply*)));
        manager->get(QNetworkRequest(QUrl("http://www.google.com")));
        }

        void getDoc::replyFinished(QNetworkReply *reply)
        {
        // qDebug() << reply->error();
        QByteArray data=reply->readAll();
        }

        2-
        use QFile and qdatastream to save it to a file.

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

        @mrjj said in How can i write any data on directory:

        connect(manager, SIGNAL(finished(QNetworkReply*)), this, SLOT(replyFinished(QNetworkReply*)));

        Can you more explain for that?
        I can't understand

        mrjjM 1 Reply Last reply
        0
        • A Armin

          @mrjj said in How can i write any data on directory:

          connect(manager, SIGNAL(finished(QNetworkReply*)), this, SLOT(replyFinished(QNetworkReply*)));

          Can you more explain for that?
          I can't understand

          mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by mrjj
          #5

          @Armin

          Connect connect from a signal to a slot(function)

          so here it says
          When the manager say "finished" then call replyFinished located in "this" object

          connect(manager, SIGNAL(finished(QNetworkReply*)), this, SLOT(replyFinished(QNetworkReply*)));

          and the slot / function us
          void mainwindow::replyFinished(QNetworkReply *reply)

          So all it does is to set up the connect for a signal and which function we want it to call.

          so say we have 2 classes

          A and B
          A has beep signal and B have beepHandler
          then we say
          connect( pointer to a, beep , pointer to b, beephandler )
          so beep goes to beephandler

          For more info , please see
          http://doc.qt.io/qt-5/signalsandslots.html

          The version @Xiami is using just the same, but he uses the new syntax and
          uses a lambda which is a c++ things that allows you to define functions in place/right there.
          connect(reply,&QNetworkReply::finished,this,[&] { this is your slot }

          A 1 Reply Last reply
          3
          • mrjjM mrjj

            @Armin

            Connect connect from a signal to a slot(function)

            so here it says
            When the manager say "finished" then call replyFinished located in "this" object

            connect(manager, SIGNAL(finished(QNetworkReply*)), this, SLOT(replyFinished(QNetworkReply*)));

            and the slot / function us
            void mainwindow::replyFinished(QNetworkReply *reply)

            So all it does is to set up the connect for a signal and which function we want it to call.

            so say we have 2 classes

            A and B
            A has beep signal and B have beepHandler
            then we say
            connect( pointer to a, beep , pointer to b, beephandler )
            so beep goes to beephandler

            For more info , please see
            http://doc.qt.io/qt-5/signalsandslots.html

            The version @Xiami is using just the same, but he uses the new syntax and
            uses a lambda which is a c++ things that allows you to define functions in place/right there.
            connect(reply,&QNetworkReply::finished,this,[&] { this is your slot }

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

            @mrjj Thanks
            Why in this example you have pointed to class?

            mrjjM 1 Reply Last reply
            0
            • A Armin

              @mrjj Thanks
              Why in this example you have pointed to class?

              mrjjM Offline
              mrjjM Offline
              mrjj
              Lifetime Qt Champion
              wrote on last edited by mrjj
              #7

              @Armin
              because connects wants the address of a real objects.
              like in
              ClassX * inst= new ClassX;
              inst is the right kind for connect.
              Its a instance of the type/class ClassX;

              Its not for all ClassX created, but only
              for those you connect.

              1 Reply Last reply
              3
              • mrjjM mrjj

                Hi
                1 - it will send a signal when something is read (replyFinished) or error happened.
                void getDoc::on_pushButton_2_clicked()
                {
                manager = new QNetworkAccessManager(this);
                connect(manager, SIGNAL(finished(QNetworkReply*)), this, SLOT(replyFinished(QNetworkReply*)));
                manager->get(QNetworkRequest(QUrl("http://www.google.com")));
                }

                void getDoc::replyFinished(QNetworkReply *reply)
                {
                // qDebug() << reply->error();
                QByteArray data=reply->readAll();
                }

                2-
                use QFile and qdatastream to save it to a file.

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

                @mrjj I feel, your code is hard and twisted , so can you make it simpler for me?

                VRoninV 1 Reply Last reply
                0
                • A Armin

                  @mrjj I feel, your code is hard and twisted , so can you make it simpler for me?

                  VRoninV Offline
                  VRoninV Offline
                  VRonin
                  wrote on last edited by VRonin
                  #9

                  @Armin said in How can i write any data on directory:

                  your code is hard and twisted

                  No it's not, I wouldn't know how to make it easier. You should probably take a look at http://doc.qt.io/qt-5/signalsandslots.html


                  @Xiami

                  • no need to use the "controversial" sender() here, just pass reply by value in the capturing group of the lambda
                  • do not use C-cast to downcast, it's unsafe, use dynamic_cast or, if it's a QObject as it's always the case when calling sender(), qobject_cast
                  • if the downloaded file is too big you might run out of memory if you wait for finished do the processing as soon as there is data available using readyRead
                  QFile* file=new QFile("DownloadResult.dat"); //the file to save the data into
                  if(file->open(QIODevice::WriteOnly)){ //if you can open the file for writing
                      QNetworkAccessManager *netaccessmanager = new QNetworkAccessManager; //create the manager
                      QNetworkReply* reply = netaccessmanager.get(QNetworkRequest(QUrl(ui->textEdit->toPlainText()))); // send the request
                      connect(reply,&QNetworkReply::readyRead,this,[reply,file]()->void{ //when there is data available
                          file->write(reply->readAll()); //save it to file
                      });
                      connect(reply,&QNetworkReply::finished,reply,&QNetworkReply::deleteLater); //when finished clear the memory
                      connect(netaccessmanager,&QNetworkAccessManager::finished,reply,&QNetworkAccessManager::deleteLater); //when finished clear the memory
                      connect(reply,&QNetworkReply::finished,file,&QFile::deleteLater);//when finished close the file and clear the memory
                  }
                  

                  "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
                  4
                  • mrjjM mrjj

                    Hi
                    1 - it will send a signal when something is read (replyFinished) or error happened.
                    void getDoc::on_pushButton_2_clicked()
                    {
                    manager = new QNetworkAccessManager(this);
                    connect(manager, SIGNAL(finished(QNetworkReply*)), this, SLOT(replyFinished(QNetworkReply*)));
                    manager->get(QNetworkRequest(QUrl("http://www.google.com")));
                    }

                    void getDoc::replyFinished(QNetworkReply *reply)
                    {
                    // qDebug() << reply->error();
                    QByteArray data=reply->readAll();
                    }

                    2-
                    use QFile and qdatastream to save it to a file.

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

                    @mrjj said in How can i write any data on directory:

                    connect(manager, SIGNAL(finished(QNetworkReply*)), this, SLOT(replyFinished(QNetworkReply*)));
                    manager->get(QNetworkRequest(QUrl("http://www.google.com")));

                    why connect is on 5 line ?
                    shouldn't be on 4 line?

                    mrjjM 1 Reply Last reply
                    0
                    • A Armin

                      @mrjj said in How can i write any data on directory:

                      connect(manager, SIGNAL(finished(QNetworkReply*)), this, SLOT(replyFinished(QNetworkReply*)));
                      manager->get(QNetworkRequest(QUrl("http://www.google.com")));

                      why connect is on 5 line ?
                      shouldn't be on 4 line?

                      mrjjM Offline
                      mrjjM Offline
                      mrjj
                      Lifetime Qt Champion
                      wrote on last edited by
                      #11

                      @Armin
                      Hi
                      well its just important to connect it up before starting to use it.
                      So best spot is usual when you create it.

                      1 Reply Last reply
                      2
                      • mrjjM mrjj

                        Hi
                        1 - it will send a signal when something is read (replyFinished) or error happened.
                        void getDoc::on_pushButton_2_clicked()
                        {
                        manager = new QNetworkAccessManager(this);
                        connect(manager, SIGNAL(finished(QNetworkReply*)), this, SLOT(replyFinished(QNetworkReply*)));
                        manager->get(QNetworkRequest(QUrl("http://www.google.com")));
                        }

                        void getDoc::replyFinished(QNetworkReply *reply)
                        {
                        // qDebug() << reply->error();
                        QByteArray data=reply->readAll();
                        }

                        2-
                        use QFile and qdatastream to save it to a file.

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

                        @mrjj
                        Can i use code in below?

                        QNetworkRequest request;
                        request.setUrl(QUrl("http://qt-project.org"));
                        request.setRawHeader("User-Agent", "MyOwnBrowser 1.0");
                        
                        QNetworkReply *reply = manager->get(request);
                        connect(reply, SIGNAL(readyRead()), this, SLOT(slotReadyRead()));
                        
                        mrjjM 1 Reply Last reply
                        0
                        • A Armin

                          @mrjj
                          Can i use code in below?

                          QNetworkRequest request;
                          request.setUrl(QUrl("http://qt-project.org"));
                          request.setRawHeader("User-Agent", "MyOwnBrowser 1.0");
                          
                          QNetworkReply *reply = manager->get(request);
                          connect(reply, SIGNAL(readyRead()), this, SLOT(slotReadyRead()));
                          
                          mrjjM Offline
                          mrjjM Offline
                          mrjj
                          Lifetime Qt Champion
                          wrote on last edited by
                          #13

                          @Armin
                          Yes looks fine :)

                          1 Reply Last reply
                          2
                          • mrjjM mrjj

                            Hi
                            1 - it will send a signal when something is read (replyFinished) or error happened.
                            void getDoc::on_pushButton_2_clicked()
                            {
                            manager = new QNetworkAccessManager(this);
                            connect(manager, SIGNAL(finished(QNetworkReply*)), this, SLOT(replyFinished(QNetworkReply*)));
                            manager->get(QNetworkRequest(QUrl("http://www.google.com")));
                            }

                            void getDoc::replyFinished(QNetworkReply *reply)
                            {
                            // qDebug() << reply->error();
                            QByteArray data=reply->readAll();
                            }

                            2-
                            use QFile and qdatastream to save it to a file.

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

                            @mrjj said in How can i write any data on directory:

                            1 - it will send a signal when something is read (replyFinished) or error happened.

                            I get this message :
                            QObject::connect: No such signal QNetworkAccessManager::finished(*QNetworkReply) in ..\DownloadManager\mainwindow.cpp:20
                            QObject::connect: (receiver name: 'MainWindow')

                            why?

                            1 Reply Last reply
                            0
                            • mrjjM Offline
                              mrjjM Offline
                              mrjj
                              Lifetime Qt Champion
                              wrote on last edited by
                              #15

                              show the actual real code
                              it says QNetworkAccessManager dont have finished signal but syntax looks wrong

                              QNetworkAccessManager::finished(*QNetworkReply) <<< the * seems very wrong

                              1 Reply Last reply
                              1

                              • Login

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