【发布时间】:2012-03-20 01:24:53
【问题描述】:
我有一个带有以下代码的上传器:
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());
}
然后在完成的插槽中我这样做:
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 {
...
}
此 StorageProvider 将能够处理不同的请求,并且响应将具有不同的连接,具体取决于它创建的函数。回复是成员变量的原因是我可以在下一个请求之前将其删除。
所以,我的问题是,如果我重新回复回复,我是否必须再次进行连接?插槽/信号是否连接到指针或对象?另外,有没有更好的方法来删除旧回复?
编辑:将代码更改为已完成请求的处理程序;
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");
}
}
}
【问题讨论】:
标签: c++ qt signals-slots