Заголовок диалога:
#ifndef DIALOG_H
#define DIALOG_H
#include <QtGui>
#include <QtWidgets>
#include <QtNetwork>
class Dialog : public QDialog {
Q_OBJECT
public:
QLineEdit *Line;
QPushButton *Button;
QProgressBar *Progress;
Dialog(QWidget *parent = 0);
~Dialog();
public slots:
void SlotGet();
void updateDataTransferProgress(qint64 readBytes, qint64 totalBytes);
};
#endif // DIALOG_H
Реализация:
#include "dialog.h"
Dialog::Dialog(QWidget *parent)
: QDialog(parent) {
Line = new QLineEdit();
Button = new QPushButton("Get!");
Progress = new QProgressBar();
Progress->setValue(0);
Progress->hide();
QHBoxLayout *Top = new QHBoxLayout();
Top->addWidget(Line,1);
Top->addWidget(Button);
QVBoxLayout *Out = new QVBoxLayout();
Out->addLayout(Top);
Out->addWidget(Progress);
setLayout(Out);
setFixedSize(320,82);
connect(Button, &QPushButton::clicked, this, &Dialog::SlotGet);
}
Dialog::~Dialog() {
}
void Dialog::SlotGet() {
qDebug() << "Start";
Progress->show();
QEventLoop Loop;
QNetworkAccessManager *Net = new QNetworkAccessManager(this);
QNetworkRequest Req = QNetworkRequest(Line->text());
QNetworkReply *Reply = Net->get(Req);
connect(Reply, QNetworkReply::downloadProgress, this, &Dialog::updateDataTransferProgress);
connect(Reply, QNetworkReply::finished, &Loop, &QEventLoop::quit);
Loop.exec();
Progress->hide();
QUrl Url(Line->text());
QFileInfo FileInfo=Url.path();
QFile File(FileInfo.fileName());
File.open(QIODevice::WriteOnly);
File.write(Reply->readAll());
delete Reply;
}
//
// url для тестовой скачки
// http ://garr.dl.sourceforge.net/project/mingw/
// MSYS/Base/msys-core/msys-1.0.11/MSYS-1.0.11.exe
//
void Dialog::updateDataTransferProgress(qint64 readBytes, qint64 totalBytes) {
Progress->setMaximum(totalBytes);
Progress->setValue(readBytes);
}