Solved 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? -
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 -
Connect connect from a signal to a slot(function)
so here it says
When the manager say "finished" then call replyFinished located in "this" objectconnect(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 beephandlerFor more info , please see
http://doc.qt.io/qt-5/signalsandslots.htmlThe 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? -
@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
- 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 callingsender()
,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 usingreadyRead
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? -
@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()));
-
@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?
-
show the actual real code
it says QNetworkAccessManager dont have finished signal but syntax looks wrongQNetworkAccessManager::finished(*QNetworkReply) <<< the * seems very wrong