novichkov
4th August 2010, 19:28
Hi, I have a problem. I'm using QNetworkAccessManager:
QNetworkRequest request;
request.setUrl(QUrl("http://www.discogs.com/search?type=releases&q="
+QString(QUrl::toPercentEncoding(query))
+"&f=xml&api_key="
+Settings::instance()->discogsKey));
request.setRawHeader("Accept-Encoding", "gzip");
request.setAttribute(QNetworkRequest::User,"discogs search");
...
QNetworkReply *reply = networkManager->get(request);
connect(reply, SIGNAL(finished()), this, SLOT(handleNetworkReply()));
connect(reply, SIGNAL(error(QNetworkReply::NetworkError)),
this, SLOT(handleNetworkError()));
connect(reply,SIGNAL(metaDataChanged()),SLOT(handl eNetworkState()));
and then working with the reply in the slot handleNetworkReply():
void TagsFiller::handleNetworkReply()
{
QNetworkReply *reply = qobject_cast<QNetworkReply *>(sender());
if (!reply) return;
QString networkType=reply->request().attribute(QNetworkRequest::User).toStrin g();
int n = reply->request().attribute(QNetworkRequest::Attribute(QNe tworkRequest::User+1)).toInt();
QByteArray rawResponse = reply->readAll();
QByteArray response = rawResponse;
if (reply->hasRawHeader("Content-Encoding") && reply->rawHeader("Content-Encoding")=="gzip") {
//we have to unzip the response, so we are trying to get content size
QTemporaryFile file;
if (file.open()) {
file.write(rawResponse);
file.close();
response = deflateFile(file.fileName());
}
}
//un-xml response
if (networkType=="discogs search")
parseDiscogsSearch(response);
if (networkType=="discogs release")
parseDiscogsRelease(response,n);
if (networkType=="discogs image")
parseDiscogsImage(rawResponse,"",n);
if (networkType=="freedb search")
parseFreedbSearch(response);
if (networkType=="freedb query")
parseFreedbQuery(response);
if (networkType=="freedb read")
parseFreedbRead(response,n);
reply->deleteLater();
}
But if the connection with the Internet is down and I'm closing the dialog without timeout waiting, I have network replies still processing, and my program won't quit at all.
I tested it in Windows (Qt 4.6) and Linux (Qt 4.5), the problem is still present.
I know that QNetworkReply has slot abort(), but this solution doesn't work:
TagsFiller::~TagsFiller()
{
if (!repliesStack.isEmpty()) {
foreach(QNetworkReply *reply,repliesStack.values()) {
reply->abort();
reply->deleteLater();
}
repliesStack.clear();
}
}
Can anyone help me?
QNetworkRequest request;
request.setUrl(QUrl("http://www.discogs.com/search?type=releases&q="
+QString(QUrl::toPercentEncoding(query))
+"&f=xml&api_key="
+Settings::instance()->discogsKey));
request.setRawHeader("Accept-Encoding", "gzip");
request.setAttribute(QNetworkRequest::User,"discogs search");
...
QNetworkReply *reply = networkManager->get(request);
connect(reply, SIGNAL(finished()), this, SLOT(handleNetworkReply()));
connect(reply, SIGNAL(error(QNetworkReply::NetworkError)),
this, SLOT(handleNetworkError()));
connect(reply,SIGNAL(metaDataChanged()),SLOT(handl eNetworkState()));
and then working with the reply in the slot handleNetworkReply():
void TagsFiller::handleNetworkReply()
{
QNetworkReply *reply = qobject_cast<QNetworkReply *>(sender());
if (!reply) return;
QString networkType=reply->request().attribute(QNetworkRequest::User).toStrin g();
int n = reply->request().attribute(QNetworkRequest::Attribute(QNe tworkRequest::User+1)).toInt();
QByteArray rawResponse = reply->readAll();
QByteArray response = rawResponse;
if (reply->hasRawHeader("Content-Encoding") && reply->rawHeader("Content-Encoding")=="gzip") {
//we have to unzip the response, so we are trying to get content size
QTemporaryFile file;
if (file.open()) {
file.write(rawResponse);
file.close();
response = deflateFile(file.fileName());
}
}
//un-xml response
if (networkType=="discogs search")
parseDiscogsSearch(response);
if (networkType=="discogs release")
parseDiscogsRelease(response,n);
if (networkType=="discogs image")
parseDiscogsImage(rawResponse,"",n);
if (networkType=="freedb search")
parseFreedbSearch(response);
if (networkType=="freedb query")
parseFreedbQuery(response);
if (networkType=="freedb read")
parseFreedbRead(response,n);
reply->deleteLater();
}
But if the connection with the Internet is down and I'm closing the dialog without timeout waiting, I have network replies still processing, and my program won't quit at all.
I tested it in Windows (Qt 4.6) and Linux (Qt 4.5), the problem is still present.
I know that QNetworkReply has slot abort(), but this solution doesn't work:
TagsFiller::~TagsFiller()
{
if (!repliesStack.isEmpty()) {
foreach(QNetworkReply *reply,repliesStack.values()) {
reply->abort();
reply->deleteLater();
}
repliesStack.clear();
}
}
Can anyone help me?