Storing data from QnetworkReply
-
I found this https://wiki.qt.io/Download_Data_from_URL but it's still not storing data to class, only keeping it alive by emiting other signal:
void FileDownloader::fileDownloaded(QNetworkReply* pReply) { m_DownloadedData = pReply->readAll(); //emit a signal pReply->deleteLater(); emit downloaded(); }
@LeeH
are you calling your function upon the reply's finished signal?! -
@LeeH
are you calling your function upon the reply's finished signal?!No because If I do that, I will be able to read the data from the other function in that ba object but it will not be stored, so if i try to call that same function outside of the class it will be empty.
-
No because If I do that, I will be able to read the data from the other function in that ba object but it will not be stored, so if i try to call that same function outside of the class it will be empty.
@LeeH
but thats the point.
readAll() only returns the data when it tells you that it has finished.
Encapsulate your code into a helper class (each class has it's own QByteArray buffer). -
@LeeH
but thats the point.
readAll() only returns the data when it tells you that it has finished.
Encapsulate your code into a helper class (each class has it's own QByteArray buffer).@raven-worx said in Storing data from QnetworkReply:
Encapsulate your code into a helper class (each class has it's own QByteArray buffer).
So would I have to set objects from other places directly to the reply data?
-
@raven-worx said in Storing data from QnetworkReply:
Encapsulate your code into a helper class (each class has it's own QByteArray buffer).
So would I have to set objects from other places directly to the reply data?
@LeeH
Have a look at this sample
http://www.bogotobogo.com/Qt/Qt5_Downloading_Files_QNetworkAccessManager_QNetworkRequest.php
It uses a small helper class
class Downloader : public QObject
to manage the data it downloads. It responds to the finished signal.
Here it stores to file but you can as well store in ByteArray or whatever is needed.Also its important to understand that you should NOT call the ReadData function yourself.
Your function will be called when data is ready. Its an asynchronous API. -
@LeeH
Have a look at this sample
http://www.bogotobogo.com/Qt/Qt5_Downloading_Files_QNetworkAccessManager_QNetworkRequest.php
It uses a small helper class
class Downloader : public QObject
to manage the data it downloads. It responds to the finished signal.
Here it stores to file but you can as well store in ByteArray or whatever is needed.Also its important to understand that you should NOT call the ReadData function yourself.
Your function will be called when data is ready. Its an asynchronous API. -
@LeeH
Have a look at this sample
http://www.bogotobogo.com/Qt/Qt5_Downloading_Files_QNetworkAccessManager_QNetworkRequest.php
It uses a small helper class
class Downloader : public QObject
to manage the data it downloads. It responds to the finished signal.
Here it stores to file but you can as well store in ByteArray or whatever is needed.Also its important to understand that you should NOT call the ReadData function yourself.
Your function will be called when data is ready. Its an asynchronous API.Have a look at this sample
http://www.bogotobogo.com/Qt/Qt5_Downloading_Files_QNetworkAccessManager_QNetworkRequest.php
It uses a small helper class
class Downloader : public QObject
to manage the data it downloads. It responds to the finished signal.
Here it stores to file but you can as well store in ByteArray or whatever is needed.I already have a class doing this job - as I said in the begining I can already save my reply data to file that can be read/used from anywhere in one signal slot connection, I just want to do the same thing without using a file so it can be read/used from anywhere in a single connection. If my class data object that has been assigned (or appended to) the value of reply's byte array is always empty when called, say from outside of the class (after download), meaning I have to re-download everytime I need the same data, then the reply data is not stored to my object is it?
-
Have a look at this sample
http://www.bogotobogo.com/Qt/Qt5_Downloading_Files_QNetworkAccessManager_QNetworkRequest.php
It uses a small helper class
class Downloader : public QObject
to manage the data it downloads. It responds to the finished signal.
Here it stores to file but you can as well store in ByteArray or whatever is needed.I already have a class doing this job - as I said in the begining I can already save my reply data to file that can be read/used from anywhere in one signal slot connection, I just want to do the same thing without using a file so it can be read/used from anywhere in a single connection. If my class data object that has been assigned (or appended to) the value of reply's byte array is always empty when called, say from outside of the class (after download), meaning I have to re-download everytime I need the same data, then the reply data is not stored to my object is it?
@LeeH said in Storing data from QnetworkReply:
Hi
If you keep the instance of class Downloader around,
there is no reason you cannot keep the data for later.
Say if you append to a QByteArray variable defined as
class member in DownloaderCan you please show the actual code how you use your helper class and how you later
try to access the data? -
@LeeH said in Storing data from QnetworkReply:
Hi
If you keep the instance of class Downloader around,
there is no reason you cannot keep the data for later.
Say if you append to a QByteArray variable defined as
class member in DownloaderCan you please show the actual code how you use your helper class and how you later
try to access the data?@mrjj said in Storing data from QnetworkReply:
Can you please show the actual code how you use your helper class and how you later
try to access the data?Certainly.
header:
class data : public QObject{ Q_OBJECT public: data(); void download(); QByteArray get(); public slots: void output(QNetworkReply* rep); private: QNetworkAccessManager* _qnam; QByteArray _ba; };
source:
data::data(){} void data::download(){ _qnam = new QNetworkAccessManager(this); _qnam->get(QNetworkRequest(QUrl("http://www.google.com"))); QObject::connect(_qnam, SIGNAL(finished(QNetworkReply*)), this, SLOT(output(QNetworkReply*))); } void data::output(QNetworkReply* rep){ if(!rep->error()){ _ba.append(rep->readAll()); } rep->deleteLater(); } QByteArray data::get(){ qDebug() << _ba; }
main:
data obj; obj.download(); obj.get();
-
@mrjj said in Storing data from QnetworkReply:
Can you please show the actual code how you use your helper class and how you later
try to access the data?Certainly.
header:
class data : public QObject{ Q_OBJECT public: data(); void download(); QByteArray get(); public slots: void output(QNetworkReply* rep); private: QNetworkAccessManager* _qnam; QByteArray _ba; };
source:
data::data(){} void data::download(){ _qnam = new QNetworkAccessManager(this); _qnam->get(QNetworkRequest(QUrl("http://www.google.com"))); QObject::connect(_qnam, SIGNAL(finished(QNetworkReply*)), this, SLOT(output(QNetworkReply*))); } void data::output(QNetworkReply* rep){ if(!rep->error()){ _ba.append(rep->readAll()); } rep->deleteLater(); } QByteArray data::get(){ qDebug() << _ba; }
main:
data obj; obj.download(); obj.get();
@LeeH said in Storing data from QnetworkReply:
QNetworkReply
You should connect your output() slot to http://doc.qt.io/qt-5/qiodevice.html#readyRead signal.
-
@LeeH said in Storing data from QnetworkReply:
QNetworkReply
You should connect your output() slot to http://doc.qt.io/qt-5/qiodevice.html#readyRead 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
Do you mean another connection, something like this:
void data::download(){ _qnam->get(QNetworkRequest(QUrl("http://www.google.com"))); QObject::connect(_qnam, SIGNAL(finished(QNetworkReply*)), this, SLOT(output(QNetworkReply*))); QObject::connect(QIODevice, SIGNAL(readyRead()), this, SLOT(output(QNetworkReply*))); }
I know the QIODevice usage here is wrong, but just trying to get an idea
thanks
-
@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
Do you mean another connection, something like this:
void data::download(){ _qnam->get(QNetworkRequest(QUrl("http://www.google.com"))); QObject::connect(_qnam, SIGNAL(finished(QNetworkReply*)), this, SLOT(output(QNetworkReply*))); QObject::connect(QIODevice, SIGNAL(readyRead()), this, SLOT(output(QNetworkReply*))); }
I know the QIODevice usage here is wrong, but just trying to get an idea
thanks
-
Ok... so how should I use QIOdevice here:
QObject::connect(QIODevice, SIGNAL(readyRead()), this, SLOT(output(QNetworkReply*)));
i get error: "expecting primary expression before ',' token" and it can't be instantiated
thanks
-
@m.sue said in Storing data from QnetworkReply:
then the object of the derived class that you use.
I modified my code to use readRead() from QNetworkReply object and it still doesn't work:
... void data::download(){ _rep = _qnam->get(QNetworkRequest(QUrl("http://www.google.com"))); QObject::connect(_rep, SIGNAL(readyRead()), this, SLOT(output())); } void data::output(){ if(!_rep->error()){ _ba.append(_rep->readAll()); } else{ qDebug() << _rep->error(); } _rep->deleteLater(); }
-
@m.sue said in Storing data from QnetworkReply:
then the object of the derived class that you use.
I modified my code to use readRead() from QNetworkReply object and it still doesn't work:
... void data::download(){ _rep = _qnam->get(QNetworkRequest(QUrl("http://www.google.com"))); QObject::connect(_rep, SIGNAL(readyRead()), this, SLOT(output())); } void data::output(){ if(!_rep->error()){ _ba.append(_rep->readAll()); } else{ qDebug() << _rep->error(); } _rep->deleteLater(); }
-
@LeeH said in Storing data from QnetworkReply:
qDebug() << _rep->error();
Is line ever activated ?
is _ba.append called ? -
Hi @LeeH
I would write:
connect(_rep, SIGNAL(readyRead()), SLOT(output()));
But this should not be essential.So maybe output() is not a slot.
-Michael.