Your loop is using the blocking waitForReadyRead() function, so it is incompatible with the typical asynchronous non-blocking process.
The general idea (no error checking etc.) is in this code snippet:
{
Q_OBJECT
public:
void fetch
(const QUrl &url
) { buffer.clear();
reply = nam.get(QNetworkRequest(url));
connect(reply, SIGNAL(finished()), SLOT(slotFinished()));
connect(reply, SIGNAL(readyRead()), SLOT(slotReadyRead()));
}
public slots:
void slotReadyRead() {
buffer.append(reply.readAll());
int index = buffer.indexOf("\n\n"); // a blank line indicates the end
if (index > 0) {
// call something to process buffer.left(index)
// remove the processed data and the blank line
buffer = buffer.mid(index+2);
}
}
void slotFinished() {
reply->deleteLater();
reply = 0;
}
private:
QNetworkAccessManager nam;
QNetworkReply *reply;
};
class Fetcher: public QObject
{
Q_OBJECT
public:
Fetcher(QObject *p = 0): QObject(p), reply(0) {}
void fetch(const QUrl &url) {
buffer.clear();
reply = nam.get(QNetworkRequest(url));
connect(reply, SIGNAL(finished()), SLOT(slotFinished()));
connect(reply, SIGNAL(readyRead()), SLOT(slotReadyRead()));
}
public slots:
void slotReadyRead() {
buffer.append(reply.readAll());
int index = buffer.indexOf("\n\n"); // a blank line indicates the end
if (index > 0) {
// call something to process buffer.left(index)
// remove the processed data and the blank line
buffer = buffer.mid(index+2);
}
}
void slotFinished() {
reply->deleteLater();
reply = 0;
}
private:
QNetworkAccessManager nam;
QNetworkReply *reply;
QByteArray buffer;
};
To copy to clipboard, switch view to plain text mode
Bookmarks