How can i write any data on directory



  • Hi
    Look at the code please :

    void MainWindow::on_pushButton_clicked()
    {
    QNetworkAccessManager *netaccessmanager;
    QNetworkReply *netreply = netaccessmanager->get(QNetworkRequest(QUrl(ui->textEdit->toPlainText())));

    }

    I have two question :

    1- Asked data that is returned where will stored in *netreply?

    2- How can i write any data on directory ? ( like c:/armin/download )
    Which member in QIOdevice i should use?


  • Qt Champions 2016

    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.



  •     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();
            }
        });
    


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


  • Qt Champions 2016

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



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


  • Qt Champions 2016

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



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



  • @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
    }
    


  • @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?


  • Qt Champions 2016

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



  • @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()));
    

  • Qt Champions 2016

    @Armin
    Yes looks fine :)



  • @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?


  • Qt Champions 2016

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

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


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.