#include <QApplication>
#include <QVBoxLayout>
#include <QLabel>
#include <QTimer>
#include <QHttp>
Q_OBJECT
int tick;
int bytes;
public:
timerLabel
= new QLabel("",
this);
infoLabel
= new QLabel("",
this);
layout->addWidget(timerLabel);
layout->addWidget(infoLabel);
connect(timer, SIGNAL(timeout()), SLOT(handleTimer()));
timer->setInterval(1000);
timer->start();
connect(http, SIGNAL(requestStarted(int)), SLOT(handleStarted(int)));
connect(http, SIGNAL(requestFinished(int,bool)), SLOT(handleFinished(int,bool)));
http->setHost("www.nasa.gov");
http->get("/sites/default/files/pia17582.jpg");
tick = 0;
bytes = 0;
}
private slots:
void handleTimer() {
timerLabel
->setText
(QString::number(tick
++));
}
void handleStarted(int id) {
infoLabel
->setText
(QString("Request %1 started").
arg(id
));
}
bytes += ba.size();
infoLabel
->setText
(QString("Read %1 bytes").
arg(bytes
));
}
void handleFinished(int id, bool error) {
infoLabel
->setText
(QString("Request %1 finished").
arg(id
));
}
};
int main(int argc, char **argv)
{
Widget w;
w.show();
return app.exec();
}
#include "main.moc"
#include <QApplication>
#include <QVBoxLayout>
#include <QLabel>
#include <QTimer>
#include <QHttp>
class Widget: public QWidget {
Q_OBJECT
QTimer *timer;
QHttp *http;
QLabel *timerLabel;
QLabel *infoLabel;
int tick;
int bytes;
public:
Widget(QWidget *p = 0): QWidget(p) {
timerLabel = new QLabel("", this);
infoLabel = new QLabel("", this);
QVBoxLayout *layout = new QVBoxLayout(this);
layout->addWidget(timerLabel);
layout->addWidget(infoLabel);
QTimer *timer = new QTimer(this);
connect(timer, SIGNAL(timeout()), SLOT(handleTimer()));
timer->setInterval(1000);
timer->start();
http = new QHttp(this);
connect(http, SIGNAL(requestStarted(int)), SLOT(handleStarted(int)));
connect(http, SIGNAL(readyRead(QHttpResponseHeader)), SLOT(handleRead(QHttpResponseHeader)));
connect(http, SIGNAL(requestFinished(int,bool)), SLOT(handleFinished(int,bool)));
http->setHost("www.nasa.gov");
http->get("/sites/default/files/pia17582.jpg");
tick = 0;
bytes = 0;
}
private slots:
void handleTimer() {
timerLabel->setText(QString::number(tick++));
}
void handleStarted(int id) {
infoLabel->setText(QString("Request %1 started").arg(id));
}
void handleRead(const QHttpResponseHeader &header) {
QByteArray ba = http->readAll();
bytes += ba.size();
infoLabel->setText(QString("Read %1 bytes").arg(bytes));
}
void handleFinished(int id, bool error) {
infoLabel->setText(QString("Request %1 finished").arg(id));
}
};
int main(int argc, char **argv)
{
QApplication app(argc, argv);
Widget w;
w.show();
return app.exec();
}
#include "main.moc"
To copy to clipboard, switch view to plain text mode
Bookmarks