#include <QtCore>
#include <QtNetwork>
#include <QDebug>
{
Q_OBJECT
public:
{
QTimer::singleShot(0,
this,
SLOT(start
()));
}
public slots:
void start() {
fetched = 0;
size = 0;
QNetworkRequest req
(QUrl("http://qt.nokia.com/files/pdf/faster-more-cost-effective-development-using-the-qt-cross-platform-application-ui-framework"));
reply = nam.get(req);
connect(reply, SIGNAL(metaDataChanged()), this, SLOT(replyMetaDataChanged()));
connect(reply, SIGNAL(readyRead()), this, SLOT(slotReadyRead()));
connect(reply, SIGNAL(finished()), this, SLOT(replyFinished()));
}
void replyMetaDataChanged() {
qDebug() << "Metadata changed";
qDebug() << "Content-Length:" << reply->header(QNetworkRequest::ContentLengthHeader);
size = reply->header(QNetworkRequest::ContentLengthHeader).toLongLong();
}
void slotReadyRead() {
fetched += ba.size();
qDebug() << "Read" << fetched << "of" << size;
}
void replyFinished() {
qDebug() << "Finished";
}
private:
QNetworkAccessManager nam;
QNetworkReply *reply;
qint64 fetched;
qint64 size;
};
int main(int argc, char *argv[])
{
Fetcher f;
return app.exec();
}
#include "main.moc"
#include <QtCore>
#include <QtNetwork>
#include <QDebug>
class Fetcher: public QObject
{
Q_OBJECT
public:
Fetcher(QObject *p = 0): QObject(p)
{
QTimer::singleShot(0, this, SLOT(start()));
}
public slots:
void start() {
fetched = 0;
size = 0;
QNetworkRequest req(QUrl("http://qt.nokia.com/files/pdf/faster-more-cost-effective-development-using-the-qt-cross-platform-application-ui-framework"));
reply = nam.get(req);
connect(reply, SIGNAL(metaDataChanged()), this, SLOT(replyMetaDataChanged()));
connect(reply, SIGNAL(readyRead()), this, SLOT(slotReadyRead()));
connect(reply, SIGNAL(finished()), this, SLOT(replyFinished()));
}
void replyMetaDataChanged() {
qDebug() << "Metadata changed";
qDebug() << "Content-Length:" << reply->header(QNetworkRequest::ContentLengthHeader);
size = reply->header(QNetworkRequest::ContentLengthHeader).toLongLong();
}
void slotReadyRead() {
QByteArray ba = reply->readAll();
fetched += ba.size();
qDebug() << "Read" << fetched << "of" << size;
}
void replyFinished() {
qDebug() << "Finished";
}
private:
QNetworkAccessManager nam;
QNetworkReply *reply;
qint64 fetched;
qint64 size;
};
int main(int argc, char *argv[])
{
QCoreApplication app(argc, argv);
Fetcher f;
return app.exec();
}
#include "main.moc"
To copy to clipboard, switch view to plain text mode
Bookmarks