#include <QtGui>
#include <QtNetwork>
#include <QDebug>
Q_OBJECT
public:
layout->addWidget(label);
layout->addWidget(edit);
layout->addWidget(button);
central->setLayout(layout);
setCentralWidget(central);
connect(button, SIGNAL(clicked()), this, SLOT(clicked()));
}
public slots:
void clicked() {
button->setEnabled(false);
reply = mgr.get( QNetworkRequest(url) );
connect(reply, SIGNAL(finished()), this, SLOT(done()));
};
void done() {
button->setEnabled(true);
qDebug() << response;
QFile out
("out.txt");
// on the stack and local to here if (out.
open(QFile::WriteOnly)) { out.write( response );
out.close();
}
reply->deleteLater(); // clean up the reply object
};
private:
QNetworkAccessManager mgr;
QNetworkReply *reply;
};
int main(int argc, char *argv[])
{
MainWindow m;
m.show();
return app.exec();
}
#include "main.moc"
#include <QtGui>
#include <QtNetwork>
#include <QDebug>
class MainWindow: public QMainWindow {
Q_OBJECT
public:
MainWindow(QWidget *p = 0): QMainWindow(p) {
QWidget *central = new QWidget(this);
QLabel *label = new QLabel("URL:", this);
edit = new QLineEdit(this);
button = new QPushButton("Go", this);
QVBoxLayout *layout = new QVBoxLayout(central);
layout->addWidget(label);
layout->addWidget(edit);
layout->addWidget(button);
central->setLayout(layout);
setCentralWidget(central);
connect(button, SIGNAL(clicked()), this, SLOT(clicked()));
}
public slots:
void clicked() {
button->setEnabled(false);
QUrl url(edit->text());
reply = mgr.get( QNetworkRequest(url) );
connect(reply, SIGNAL(finished()), this, SLOT(done()));
};
void done() {
button->setEnabled(true);
QByteArray response = reply->readAll();
qDebug() << response;
QFile out("out.txt"); // on the stack and local to here
if (out.open(QFile::WriteOnly)) {
out.write( response );
out.close();
}
reply->deleteLater(); // clean up the reply object
};
private:
QLineEdit *edit;
QPushButton *button;
QNetworkAccessManager mgr;
QNetworkReply *reply;
};
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
MainWindow m;
m.show();
return app.exec();
}
#include "main.moc"
To copy to clipboard, switch view to plain text mode
Bookmarks