Storing data from QnetworkReply
-
@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.
-
@LeeH said in Storing data from QnetworkReply:
qDebug() << _rep->error();
Is line ever activated ?
is _ba.append called ? -
@mrjj said in Storing data from QnetworkReply:
Is line ever activated ?
is _ba.append called ?Hi, there are no errors from server, and I always get reply.
I call _ba from main() using data::get() -
@LeeH
but is data appended?if(!_rep->error()){
_ba.append(_rep->readAll());
qDebug() <<"size" << _ba.size();
}have you tried with a breakpoint ?
is data::output() called? -
When I added:
qDebug() <<"size" << _ba.size();
i got:
size 2634
Yes data::output() is definately being called
-
but my data::get() I use to read _ba that i call from main() after downloading, is empty(""), where as if I read _ba from the slot inside the class I can see all my data
-
You know I think It's best I just stick to saving to file, that seem's to be the only way I can be sure my data sticks around and can be used anywhere.... I thank everyone for their help, and apologize for wasting all of our time!
@LeeH Well here's your problem:
QByteArray data::get(){ qDebug() << _ba; }
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; }
And your data.get() will work.
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.
-
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
@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>)));