Qt实现下载文件并输出进度
时间: 2024-03-17 09:47:47 浏览: 132
可以使用Qt提供的QNetworkAccessManager类来实现文件的下载,并通过QProgressDialog实现进度条的显示。
以下是一个简单的示例代码:
```cpp
#include <QtWidgets>
#include <QtNetwork>
class Downloader : public QObject {
Q_OBJECT
public:
Downloader(QObject *parent = nullptr) : QObject(parent) {}
void download(const QUrl &url) {
QNetworkRequest request(url);
reply = manager.get(request);
connect(reply, &QNetworkReply::downloadProgress, this, &Downloader::onDownloadProgress);
connect(reply, &QNetworkReply::finished, this, &Downloader::onDownloadFinished);
}
signals:
void downloadProgress(qint64 bytesReceived, qint64 bytesTotal);
void downloadFinished(QByteArray data);
private slots:
void onDownloadProgress(qint64 bytesReceived, qint64 bytesTotal) {
emit downloadProgress(bytesReceived, bytesTotal);
}
void onDownloadFinished() {
QByteArray data = reply->readAll();
reply->deleteLater();
emit downloadFinished(data);
}
private:
QNetworkAccessManager manager;
QNetworkReply *reply = nullptr;
};
class MainWindow : public QMainWindow {
Q_OBJECT
public:
MainWindow(QWidget *parent = nullptr) : QMainWindow(parent) {
urlLineEdit = new QLineEdit();
downloadButton = new QPushButton("Download");
progressBar = new QProgressBar();
statusLabel = new QLabel();
QVBoxLayout *layout = new QVBoxLayout();
layout->addWidget(urlLineEdit);
layout->addWidget(downloadButton);
layout->addWidget(progressBar);
layout->addWidget(statusLabel);
QWidget *centralWidget = new QWidget();
centralWidget->setLayout(layout);
setCentralWidget(centralWidget);
connect(downloadButton, &QPushButton::clicked, this, &MainWindow::onDownloadButtonClicked);
}
private slots:
void onDownloadButtonClicked() {
QUrl url = QUrl::fromUserInput(urlLineEdit->text());
if (!url.isValid()) {
QMessageBox::warning(this, "Error", "Invalid URL");
return;
}
downloader.download(url);
progressDialog = new QProgressDialog(this);
progressDialog->setLabelText("Downloading...");
progressDialog->setMinimum(0);
progressDialog->setMaximum(100);
progressDialog->setWindowModality(Qt::WindowModal);
progressDialog->show();
connect(&downloader, &Downloader::downloadProgress, this, &MainWindow::onDownloadProgress);
connect(&downloader, &Downloader::downloadFinished, this, &MainWindow::onDownloadFinished);
}
void onDownloadProgress(qint64 bytesReceived, qint64 bytesTotal) {
int progress = bytesReceived * 100 / bytesTotal;
progressBar->setValue(progress);
progressDialog->setValue(progress);
}
void onDownloadFinished(QByteArray data) {
statusLabel->setText("Download finished");
QMessageBox::information(this, "Info", "Download finished");
progressDialog->close();
delete progressDialog;
// TODO: handle downloaded data
}
private:
QLineEdit *urlLineEdit;
QPushButton *downloadButton;
QProgressBar *progressBar;
QLabel *statusLabel;
Downloader downloader;
QProgressDialog *progressDialog = nullptr;
};
int main(int argc, char *argv[]) {
QApplication app(argc, argv);
MainWindow mainWindow;
mainWindow.show();
return app.exec();
}
#include "main.moc"
```
在这个例子中,我们创建了一个Downloader类,它使用QNetworkAccessManager来下载文件,并通过信号和槽机制来实现进度条的更新和下载完成的处理。主窗口类MainWindow中,我们使用QLineEdit和QPushButton来获取用户输入的下载链接,并使用QProgressBar和QLabel来显示进度和状态信息。在下载按钮被点击时,我们创建一个QProgressDialog来显示下载进度,并连接Downloader类的信号和槽函数来更新进度条和处理下载完成的事件。
阅读全文