Storing data from QnetworkReply
-
Hi
@ambershark said in Storing data from QnetworkReply:
You don't return anything. You should have gotten at least a compiler warning from that.
Anyway, just edit to be:
QByteArray data::get(){
qDebug() << _ba;
return _ba;
}Your right about returning _ba. I should have seen that as the function is not void... (my brains been fried working on real project lol), and I agree the compiler should have warned me about that. But that being said unfortunately has not resolved the problem. The output is still ""
However in the same function If I deliberately overwrite _ba, I get output:
QByteArray data::get(){ _ba = "testing..."; qDebug() << _ba; return _ba; }output: "testing..."
if I remove my call to data::get() in main() and output server data directly from the slot I get my data:
if(!_rep->error()){ qDebug() << _ba.append(_rep->readAll()); } else{ qDebug() << _rep->error(); } _rep->deleteLater();There is no reason to save the data to a file. You're just wasting resources on disk and slowing your app down for no reason.
Yep that was the reason for my post
@LeeH
You did read why it dont work ?
obj.download();
obj.get();
Cannot work as download() is not a blocking call
so you will call obj.get(); BEFORE any data ever is read.First when you get signal, its safe to use obj.get(); Not before.
-
Hi
@ambershark said in Storing data from QnetworkReply:
You don't return anything. You should have gotten at least a compiler warning from that.
Anyway, just edit to be:
QByteArray data::get(){
qDebug() << _ba;
return _ba;
}Your right about returning _ba. I should have seen that as the function is not void... (my brains been fried working on real project lol), and I agree the compiler should have warned me about that. But that being said unfortunately has not resolved the problem. The output is still ""
However in the same function If I deliberately overwrite _ba, I get output:
QByteArray data::get(){ _ba = "testing..."; qDebug() << _ba; return _ba; }output: "testing..."
if I remove my call to data::get() in main() and output server data directly from the slot I get my data:
if(!_rep->error()){ qDebug() << _ba.append(_rep->readAll()); } else{ qDebug() << _rep->error(); } _rep->deleteLater();There is no reason to save the data to a file. You're just wasting resources on disk and slowing your app down for no reason.
Yep that was the reason for my post
-
@LeeH said in Storing data from QnetworkReply:
Ok... so how should I use QIOdevice here
You use your QNetworkReply which is derived from QIODevice. And you cannot connect a class - you only can connect instances.
Did you read the documentation? http://doc.qt.io/qt-5/qnetworkaccessmanager.html
From there: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())); // THIS ONE connect(reply, SIGNAL(error(QNetworkReply::NetworkError)), this, SLOT(slotError(QNetworkReply::NetworkError))); connect(reply, SIGNAL(sslErrors(QList<QSslError>)), this, SLOT(slotSslErrors(QList<QSslError>)));Hi
I had already modified my code in earlier post. You suggested QIODevice doc for readyRead() but I didn't realise QNetworkReply also had a readRead signal.@jsulm said in Storing data from QnetworkReply:
You should connect your output() slot to http://doc.qt.io/qt-5/qiodevice.html#readyRead signal.
-
Hi guys
Ah at last it now works! When I did what @mrjj said:
Cannot work as download() is not a blocking call
so you will call obj.get(); BEFORE any data ever is read.and then what @J.Hilk said after making data::get() a slot:
QObject::connect(_rep, SIGNAL(readyRead()), this, SLOT(output())); QObject::connect(_rep, SIGNAL(finished()), this, SLOT(get()));Success... Thanks so much to EVERYONE that helped!
Lee
-
Super.
Good work not giving up.