Roson的Qt之旅#85 简单网络下载程序示例代码

2022/2/27 20:21:28

本文主要是介绍Roson的Qt之旅#85 简单网络下载程序示例代码,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

1.效果展示

 

2.头文件

#pragma once

#include <QtWidgets/QMainWindow>
#include "ui_QtWidgetsApplication1.h"
#include "QProgressDialog"

#include "QNetworkAccessManager"
#include "QNetworkReply"
#include "QNetworkRequest"
#include "QFile"

//用于下载时弹出一个进度条
class ProgressDialog : public QProgressDialog {
	Q_OBJECT

public:
	explicit ProgressDialog(const QUrl &url, QWidget *parent = Q_NULLPTR);

	public slots:
	void networkReplyProgress(qint64 bytesRead, qint64 totalBytes);
	
};


class QtWidgetsApplication1 : public QMainWindow
{
    Q_OBJECT

public:
    QtWidgetsApplication1(QWidget *parent = Q_NULLPTR);

public slots:
	void SlotStartDownload();
	void SlotHttpFinished();
	void SlotHttpReadyRead();
	void SlotCancelDownload();

private:
    Ui::QtWidgetsApplication1Class ui;

	QNetworkAccessManager *m_pNet;
	QUrl m_reqUrl;
	QNetworkReply *m_pReply;
	QFile m_downloadFile;

};

3.源文件

#include "QtWidgetsApplication1.h"


ProgressDialog::ProgressDialog(const QUrl &url, QWidget *parent /*= Q_NULLPTR*/)
{
	setWindowTitle(tr("Download Progress"));
	setWindowFlags(windowFlags() & ~Qt::WindowContextHelpButtonHint);
	setLabelText(tr("Downloading %1.").arg(url.toDisplayString()));
	setMinimum(0);
	setValue(0);
	setMinimumDuration(0);
}

void ProgressDialog::networkReplyProgress(qint64 bytesRead, qint64 totalBytes)
{
	setMaximum(totalBytes);
	setValue(bytesRead);
}

QtWidgetsApplication1::QtWidgetsApplication1(QWidget *parent)
    : QMainWindow(parent)
{
    ui.setupUi(this);
	connect(ui.pushButton,&QPushButton::clicked,this, &QtWidgetsApplication1::SlotStartDownload);
}

void QtWidgetsApplication1::SlotStartDownload()
{
	//下载链接
	m_reqUrl = QUrl("https://d13lb3tujbc8s0.cloudfront.net/onlineinstallers/qt-unified-windows-x86-4.3.0-online.exe");
	m_pNet = new QNetworkAccessManager;

	//请求下载
	m_pReply = m_pNet->get(QNetworkRequest(m_reqUrl));

	//下载完成处理
	connect(m_pReply, &QNetworkReply::finished, this, &QtWidgetsApplication1::SlotHttpFinished);

	//有下载的数据到达时
	connect(m_pReply, &QIODevice::readyRead, this, &QtWidgetsApplication1::SlotHttpReadyRead);

	//下载时的进度条
	ProgressDialog *progressDialog = new ProgressDialog(m_reqUrl, this);
	progressDialog->setAttribute(Qt::WA_DeleteOnClose);
	connect(progressDialog, &QProgressDialog::canceled, this, &QtWidgetsApplication1::SlotCancelDownload);
	connect(m_pReply, &QNetworkReply::downloadProgress, progressDialog, &ProgressDialog::networkReplyProgress);
	connect(m_pReply, &QNetworkReply::finished, progressDialog, &ProgressDialog::deleteLater);
	progressDialog->show();

	//保存下载的文件
	m_downloadFile.setFileName("D:\\qt-unified-windows-x86-4.3.0-online.exe");
	if (!m_downloadFile.open(QIODevice::WriteOnly))
	{
		return;
	}
}

void QtWidgetsApplication1::SlotHttpFinished()
{
	m_downloadFile.close();
}

void QtWidgetsApplication1::SlotHttpReadyRead()
{
	m_downloadFile.write(m_pReply->readAll());
}

void QtWidgetsApplication1::SlotCancelDownload()
{
	m_pReply->abort();
}



这篇关于Roson的Qt之旅#85 简单网络下载程序示例代码的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程