关于qt的http上传和下载资源的进度条显示

qt提供的一系列接口是的qt程序在网络通信方面非常便捷,在项目开发中很多朋友会用到http协议来上传文件到服务器或者从服务器下载文件下来,这里面上传和下载的进度显示是一个提高用户体验很重要的实现,在这里简要讲一下这两个实现:

其实实现很简单,只要连接QNetworkReply类的两个信号便可以,先看文档:

上传:

void QNetworkReply::uploadProgress(qint64 bytesSent, qint64 bytesTotal)
This signal is emitted to indicate the progress of the upload part of this network request, if there's any. If there's no upload associated with this request, this signal will not be emitted.
The bytesSent parameter indicates the number of bytes uploaded, while bytesTotal indicates the total number of bytes to be uploaded. If the number of bytes to be uploaded could not be determined, bytesTotal will be -1.
The upload is finished when bytesSent is equal to bytesTotal. At that time, bytesTotal will not be -1.

下载:

void QNetworkReply::downloadProgress(qint64 bytesReceived, qint64 bytesTotal)
This signal is emitted to indicate the progress of the download part of this network request, if there's any. If there's no download associated with this request, this signal will be emitted once with 0 as the value of both bytesReceived and bytesTotal.
The bytesReceived parameter indicates the number of bytes received, while bytesTotal indicates the total number of bytes expected to be downloaded. If the number of bytes to be downloaded is not known, bytesTotal will be -1.
The download is finished when bytesReceived is equal to bytesTotal. At that time, bytesTotal will not be -1.
Note that the values of both bytesReceived and bytesTotal may be different from size(), the total number of bytes obtained through read() or readAll(), or the value of the header(ContentLengthHeader). The reason for that is that there may be protocol overhead or the data may be compressed during the download.

只要connect这两个信号到相应的槽,获得两个参数,便可以知道当前进度。其中第二个参数是当前操作文件的大小,第一个便是已上传或下载的大小

如何使用这两个信号?

大家都知道用qt的QNetworkAccessManager可以上传和下载,在调用get()和post()的时候放回一个QNetworkReply指针,我要做的就是connect这个指针的这两个信号就可以获得状态了:

QNetworkReply *reply;
QNetworkAccessManager *manager = new QNetworkAccessManager;
......

reply = manager->get(QNetworkRequest(url));
//更新进度条
connect(reply,SIGNAL(downloadProgress(qint64,qint64)),this,SLOT(updateDataReadProgress(qint64,qint64)));

有必要可以联系我要例子~

  • 2
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
Qt中实现HTTP协议的文件上传下载可以使用Qt Network模块提供的QNetworkAccessManager类来实现。对于大文件的上传下载,通常需要考虑以下几个问题: 1. 分块上传/下载:将大文件分成多个小块,分别上传/下载,可以提高传输速度,同时也能够避免一次性传输过程中出现内存不足等问题。 2. 断点续传:上传/下载过程中,如果网络中断或者其他原因导致传输中断,可以通过断点续传机制重新开始传输,避免重新传输整个文件。 3. 进度条显示上传/下载大文件需要一定的时间,为了让用户了解传输进度,需要在界面上显示传输进度条。 以下是大文件上传下载的基本步骤: 1. 创建QNetworkAccessManager对象,并连接信号finished()和uploadProgress()/downloadProgress()等信号以获取上传/下载进度。 2. 调用QNetworkAccessManager的post()或get()方法,传入要上传/下载的文件的URL,并设置请求头部信息(如Content-Length等)。 3. 将要上传/下载的大文件分成多个小块,分别上传/下载。在上传/下载过程中,可以设置QIODevice::Unbuffered标志以减少内存占用。 4. 实现断点续传机制。如果上传/下载过程中出现中断,记录当前已传输的字节数,下次从该位置开始继续传输。 5. 在界面上显示上传/下载进度条。可以通过计算当前已传输的字节数和总字节数的比例来计算进度百分比,并更新进度条显示。 以下是相关问题:
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值