#include <QtCore>
#include <QtNetwork>
#include <QDebug>
Q_OBJECT
public:
QTimer::singleShot(0,
this,
SLOT(doIt
()));
}
public slots:
void doIt() {
qDebug() << "doIt called";
QNetworkRequest request;
// request.setUrl(QUrl("http://qt.nokia.com"));
// request.setUrl(QUrl("NOSCHEME://qt.nokia.com"));
// request.setUrl(QUrl("ftp://qt.nokia.com"));
// request.setUrl(QUrl("https://site.without.ssl"));
request.
setUrl(QUrl("http://RUBBISH.nokia.com"));
reply = manager.get(request);
// This message is not triggered
if(reply->error() != QNetworkReply::NoError)
qDebug() << "network error!" << reply->error();
connect(reply, SIGNAL(readyRead()), this, SLOT(slotReadyRead()));
connect(reply, SIGNAL(finished()), this, SLOT(slotFinished()));
connect(reply, SIGNAL(error(QNetworkReply::NetworkError)),
this, SLOT(slotError(QNetworkReply::NetworkError)));
qDebug() << "doIt finished";
}
void slotReadyRead() {
qDebug() << "slotReadyRead";
}
void slotFinished() {
qDebug() << reply->readAll();
reply->deleteLater();
}
void slotError(QNetworkReply::NetworkError e) {
qDebug() << "slotError" << e ;
}
private:
QNetworkAccessManager manager;
QNetworkReply *reply;
};
int main(int argc, char *argv[])
{
TestObject t;
return app.exec();
}
#include "main.moc"
#include <QtCore>
#include <QtNetwork>
#include <QDebug>
class TestObject: public QObject {
Q_OBJECT
public:
TestObject(QObject *p = 0): QObject(p), reply(0) {
QTimer::singleShot(0, this, SLOT(doIt()));
}
public slots:
void doIt() {
qDebug() << "doIt called";
QNetworkRequest request;
// request.setUrl(QUrl("http://qt.nokia.com"));
// request.setUrl(QUrl("NOSCHEME://qt.nokia.com"));
// request.setUrl(QUrl("ftp://qt.nokia.com"));
// request.setUrl(QUrl("https://site.without.ssl"));
request.setUrl(QUrl("http://RUBBISH.nokia.com"));
reply = manager.get(request);
// This message is not triggered
if(reply->error() != QNetworkReply::NoError)
qDebug() << "network error!" << reply->error();
connect(reply, SIGNAL(readyRead()), this, SLOT(slotReadyRead()));
connect(reply, SIGNAL(finished()), this, SLOT(slotFinished()));
connect(reply, SIGNAL(error(QNetworkReply::NetworkError)),
this, SLOT(slotError(QNetworkReply::NetworkError)));
qDebug() << "doIt finished";
}
void slotReadyRead() {
qDebug() << "slotReadyRead";
}
void slotFinished() {
qDebug() << reply->readAll();
reply->deleteLater();
}
void slotError(QNetworkReply::NetworkError e) {
qDebug() << "slotError" << e ;
}
private:
QNetworkAccessManager manager;
QNetworkReply *reply;
};
int main(int argc, char *argv[])
{
QCoreApplication app(argc, argv);
TestObject t;
return app.exec();
}
#include "main.moc"
To copy to clipboard, switch view to plain text mode
Doesn't fail at your debug statement with any of the examples. All the error messages come from the slot code. Some of these should fail immediately so there is no network delay.
Bookmarks