I have a uploader with the following code:
if(!_canceled) {
_reply = _accessManager.put(request, item);
if(_reply) {
_currentItem = item;
bool status = connect(_reply, SIGNAL(uploadProgress(qint64, qint64)), this, SLOT(reportUploadProgress(qint64, qint64)));
pantheios::log(pantheios::debug, "StorageProvider(): connection uploadProgress(qint64, qint64), status", pantheios::boolean(status));
status = connect(_reply, SIGNAL(finished()), this, SLOT(handleFinishedRequest()));
pantheios::log(pantheios::debug, "StorageProvider(): connection finished(), status", pantheios::boolean(status));
} else {
emit noReply();
pantheios::log(pantheios::error, "StorageProvider(): no reply", item.toUtf8());
}
Then in the finished slot I do this:
QNetworkReply *reply = qobject_cast<QNetworkReply*>(sender());
if(reply->error() > QNetworkReply::NoError) {
pantheios::log(pantheios::error, "StorageProvider(handleFinishedRequest) ", reply->errorString().toUtf8());
if((reply->error() == QNetworkReply::TemporaryNetworkFailureError) || (reply->error() == QNetworkReply::ContentReSendError)) {
// retry the last request
_reply = accessManager.put(reply->request(), _currentItem);
}
} else {
...
}
This StorageProvider will be able to handle different request and the reply will have different connections depending on in what function it is created. The reason why the reply is a member variable is so that I can delete it before I do my next request.
So, my question is, do I have to do the connection again if I repoint the reply? Is the slot/signal connected to the pointer or the object? Also, is there any better way to delete the old reply?
Edit: Changed the code to this for the handeler of a finished request;
if(_currentReply->error() > QNetworkReply::NoError) {
pantheios::log(pantheios::error, "StorageProvider(handleFinishedRequest) ", _currentReply->errorString().toUtf8());
if(((_currentReply->error() == QNetworkReply::TemporaryNetworkFailureError) || (_currentReply->error() == QNetworkReply::ContentReSendError)) && (_currentRetries < 4)) {
QNetworkRequest lastRequest = _currentReply->request();
_currentReply->deleteLater();
_currentReply = _accessManager.put(lastRequest, _currentItem);
if(_currentReply) {
bool status = connect(_currentReply, SIGNAL(uploadProgress(qint64, qint64)), this, SLOT(reportUploadProgress(qint64, qint64)));
pantheios::log(pantheios::debug, "StorageProvider(retry): connection uploadProgress(qint64, qint64), status", pantheios::boolean(status));
status = connect(_currentReply, SIGNAL(finished()), this, SLOT(handleFinishedRequest()));
pantheios::log(pantheios::debug, "StorageProvider(retry): connection finished(), status", pantheios::boolean(status));
} else {
emit noReply();
pantheios::log(pantheios::error, "StorageProvider(retry): AccessManager no reply");
}
}
}
The signal is connected to the object, not to the pointer variable, so you have to make new connections each time.
And to delete the reply, just call deleteLater()
at the beginning of the finished()
slot:
QNetworkReply *reply = qobject_cast<QNetworkReply*>(sender());
reply->deleteLater();
that way it will be deleted at the end of the slot execution.
sender()
by wrapping each request (the initial one plus all the retries) inside its own QObject
derived class:
class PutRequest : public QObject {
Q_OBJECT
private:
QNetworkAccessManager *_manager;
QNetworkReply *_reply;
QIODevice *_item; // Or whatever type the second parameter of put is
public:
explicit PutRequest(QNetworkAccessManager *manager, QNetworkRequest *request, QIODevice *item, QObject *parent = 0)
: QObject(parent), _manager, _reply(0), _item(item)
{
_reply = _manager.put(request, item);
connectSignalsAndSlots();
}
private:
void connectSignalsAndSlots() {
// to delete the reply if the PutRequest object is destroyed
reply_->setParent(this);
// since the reply is encapsulated, the object has to emit its own
// upload progress signal
connect(_reply, SIGNAL(uploadProgress(qint64, qint64)), this, SIGNAL(uploadProgress(qint64, qint64)));
connect(_reply, SIGNAL(finished()), this, SLOT(handleFinishedRequest()));
}
private slots:
void handleFinishedRequest() {
_reply->deleteLater();
if(reply->error() != QNetworkReply::NoError) {
pantheios::log(pantheios::error, "StorageProvider(handleFinishedRequest) ", reply->errorString().toUtf8());
if((_reply->error() == QNetworkReply::TemporaryNetworkFailureError) || (reply->error() == QNetworkReply::ContentReSendError)) {
// retry the last request
_reply = _manager.put(_reply->request(), _item);
connectSignalsAndSlots();
}
} else {
...
emit finished(this);
}
}
signals:
void uploadProgress(qint64, qint64);
// emitted when the upload is successful (after the possible retries)
void finished(PutRequest*);
};
And you would create the request like this:
if(!_canceled) {
PutRequest *putRequest = new PutRequest(_accessManager, request, item);
// and you connect putRequest object signals to whatever you connected _reply to