#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QtCore>
#include <QtNetwork/QtNetwork>
#include <QDebug>
#include <QFile>
#include <QtGui>
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = 0);
QFile file;
QString link;
QLineEdit* edit;
QPushButton* btn;
QGridLayout* layout;
QNetworkAccessManager* networkManager() { return pNetManager; }
~MainWindow();
private:
QNetworkAccessManager *pNetManager;
public slots:
void slotReadyRead();
void slotError(QNetworkReply::NetworkError);
void slotFinished();
};
#endif // MAINWINDOW_H
#include "mainwindow.h"
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
{
pNetManager = new QNetworkAccessManager(this);
QNetworkRequest request;
request.setUrl(QUrl("http://www.google.ru/favicon.ico"));
QNetworkReply *reply = pNetManager->get(request);
connect(reply, SIGNAL(readyRead()), this, SLOT(slotReadyRead()));
connect(reply, SIGNAL(error(QNetworkReply::NetworkError)),
this, SLOT(slotError(QNetworkReply::NetworkError)));
connect(reply, SIGNAL(finished()), this, SLOT(slotFinished()));
layout = new QGridLayout;
btn=new QPushButton("download");
edit = new QLineEdit("adress");
layout->addWidget(edit,0,0);
layout->addWidget(btn,1,0);
QWidget *widget = new QWidget;
widget->setLayout(layout);
setCentralWidget(widget);
file.setFileName("favicon.ico");
file.open(QIODevice::WriteOnly);
}
void MainWindow::slotReadyRead()
{
QNetworkReply *reply = qobject_cast<QNetworkReply*>(sender());
file.write(reply->readAll());
}
void MainWindow::slotFinished()
{
QNetworkReply *reply = qobject_cast<QNetworkReply*>(sender());
file.close();
reply->deleteLater();
}
void MainWindow::slotError(QNetworkReply::NetworkError error)
{
}
MainWindow::~MainWindow()
{
}
#include <QtGui/QApplication>
#include "mainwindow.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
Add a code snippet to your website: www.paste.org