QT中https文件下载,进度条实现

1.首先在.pro文件中添加支持 

QT       += network

DownLoadFileHttps.h
#ifndef DOWNLOADFILEHTTPS_H
#define DOWNLOADFILEHTTPS_H

#include <memory>
#include <QObject>
#include <QUrl>
#include <QFile>
#include <QDir>
#include <QPointer>
#include <QApplication>
#include <QNetworkReply>
#include <QNetworkAccessManager>
#include <QStandardPaths>
#include "mainwindow.h"

class MainWindow;
class DownLoadFileHttps : public QObject
{
    Q_OBJECT
public:
    // 参数1=下载链接 参数2=保存的路径 参数3=父窗口地址
    explicit DownLoadFileHttps(const QString& downloadUrl, const QString& savePath, QObject* parent = nullptr);
    //析构,不解释
    ~DownLoadFileHttps();

    bool startDownload();  // 开始下载文件
    void cancelDownload(); // 取消下载文件
Q_SIGNALS:
    // 下载进度信号
    void sigProgress(qint64 bytesRead, qint64 totalBytes, qreal progress);
    // 下载完成信号
    void sigDownloadFinished();

private Q_SLOTS:
    // QNetworkReply::finished对应的槽函数
    void httpFinished();
    // QIODevice::readyRead对应的槽函数
    void httpReadyRead();
    // QNetworkReply::downloadProgress对应的槽函数
    void networkReplyProgress(qint64 bytesRead, qint64 totalBytes);

private:
    void startRequest(const QUrl& requestedUrl);
    std::unique_ptr<QFile> openFileForWrite(const QString& fileName);

private:
    // 保存构造时传入的下载url
    QString m_downloadUrl;
    // 保存构造时传入的保存路径
    QString m_savePath;
    // 默认文件名称
    const QString defaultFileName = "QConnect.exe";
    QUrl url;
    QNetworkAccessManager qnam;
    QPointer<QNetworkReply> reply;
    std::unique_ptr<QFile> m_fileptr;
    bool httpRequestAborted;

    MainWindow *m_mainwindow;
};

#endif // DOWNLOADFILEHTTPS_H

DownLoadFileHttps.cpp
#include "DownLoadFileHttps.h"

DownLoadFileHttps::DownLoadFileHttps(const QString& downloadUrl, const QString& savePath, QObject* parent)
    : QObject(parent)
{
    m_downloadUrl = downloadUrl;
    m_savePath    = savePath;
    m_mainwindow = (MainWindow*)parent;
}
DownLoadFileHttps::~DownLoadFileHttps()
{

}
bool DownLoadFileHttps::startDownload()
{
    const QUrl newUrl = QUrl::fromUserInput(m_downloadUrl);

    if (!newUrl.isValid()) {
        qDebug() << QString("Download failed: %1.").arg(reply->errorString());
        return false;
    }
    QString svrloadFile = QStandardPaths::writableLocation(QStandardPaths::DownloadLocation);
    QString fileName = newUrl.fileName();
    //判断文件名称是否正确
    if (fileName.isEmpty())
    {
        fileName = defaultFileName;
    }
    //判断传有路径,没有则默认路径
    if (m_savePath.isEmpty())
    {
        m_savePath = svrloadFile;
    }
    //判断路径是否存在,不存在创建
    if (!QFileInfo(m_savePath).isDir())
    {
        QDir dir;
        dir.mkpath(m_savePath);
    }
    fileName.prepend(m_savePath + '/');
    if (QFile::exists(fileName))//判断是否存在此文件
    {
        QFile::remove(fileName);
    }
    m_fileptr = openFileForWrite(fileName);
    if (!m_fileptr)
        return false;

    startRequest(newUrl);

    return true;
}
void DownLoadFileHttps::cancelDownload()
{
    httpRequestAborted = true;
    reply->abort();
}
void DownLoadFileHttps::httpFinished()
{
    QFileInfo fi;
    if (m_fileptr) {
        fi.setFile(m_fileptr->fileName());
        m_fileptr->close();
        m_fileptr.reset();
    }

    if (httpRequestAborted) {
        return;
    }

    if (reply->error()) {
        QFile::remove(fi.absoluteFilePath());
        qDebug() << QString("Download failed: %1.").arg(reply->errorString());
        return;
    }

    const QVariant redirectionTarget = reply->attribute(QNetworkRequest::RedirectionTargetAttribute);

    if (!redirectionTarget.isNull()) {
        const QUrl redirectedUrl = url.resolved(redirectionTarget.toUrl());
        m_fileptr = openFileForWrite(fi.absoluteFilePath());
        if (!m_fileptr)
        {
            return;
        }
        startRequest(redirectedUrl);
        return;
    }

    Q_EMIT sigDownloadFinished();

    Q_EMIT m_mainwindow->sigDownloadFinished();
}

void DownLoadFileHttps::httpReadyRead()
{
    if (m_fileptr)
        m_fileptr->write(reply->readAll());
}
void DownLoadFileHttps::networkReplyProgress(qint64 bytesRead, qint64 totalBytes)
{
    qreal progress = qreal(bytesRead) / qreal(totalBytes);
    Q_EMIT sigProgress(bytesRead, totalBytes, progress);
    qDebug() << QString::number(progress * 100, 'f', 2) << "%    "
        << bytesRead / (1024 * 1024) << "MB" << "/" << totalBytes / (1024 * 1024) << "MB";
    emit m_mainwindow->sigProgress(bytesRead, totalBytes, progress);
}
void DownLoadFileHttps::startRequest(const QUrl& requestedUrl)
{
    try {
        url = requestedUrl;
        httpRequestAborted = false;
        QSslConfiguration config = QSslConfiguration::defaultConfiguration();
        config.setProtocol(QSsl::AnyProtocol);
        config.setPeerVerifyMode(QSslSocket::VerifyNone);
        QNetworkRequest request = QNetworkRequest(url);
        request.setSslConfiguration(config);
        reply = qnam.get(request);

        connect(reply, &QNetworkReply::finished, this, &DownLoadFileHttps::httpFinished);
        connect(reply, &QIODevice::readyRead, this, &DownLoadFileHttps::httpReadyRead);
        connect(reply, &QNetworkReply::downloadProgress, this, &DownLoadFileHttps::networkReplyProgress);
        //qDebug() << QString(tr("Downloading %1...").arg(url.toString()));
    }
    catch (...)
    {
        return ;
    }

}
std::unique_ptr<QFile> DownLoadFileHttps::openFileForWrite(const QString& fileName)
{
    std::unique_ptr<QFile> file(new QFile(fileName));
    if (!file->open(QIODevice::WriteOnly))
    {
        qDebug() << QString("Unable to save the file %1: %2.")
            .arg(QDir::toNativeSeparators(fileName), file->errorString());
        return nullptr;
    }
    return file;
}

 mainwindow.ui

<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>MainWindow</class>
 <widget class="QMainWindow" name="MainWindow">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>523</width>
    <height>367</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>MainWindow</string>
  </property>
  <widget class="QWidget" name="centralwidget">
   <widget class="QWidget" name="horizontalLayoutWidget">
    <property name="geometry">
     <rect>
      <x>40</x>
      <y>40</y>
      <width>411</width>
      <height>41</height>
     </rect>
    </property>
    <layout class="QHBoxLayout" name="horizontalLayout">
     <item>
      <widget class="QLabel" name="label">
       <property name="text">
        <string>下载Url:</string>
       </property>
      </widget>
     </item>
     <item>
      <widget class="QLineEdit" name="lineEdit"/>
     </item>
     <item>
      <widget class="QPushButton" name="pushButton">
       <property name="text">
        <string>下载</string>
       </property>
      </widget>
     </item>
    </layout>
   </widget>
   <widget class="QProgressBar" name="progressBar">
    <property name="geometry">
     <rect>
      <x>40</x>
      <y>120</y>
      <width>431</width>
      <height>23</height>
     </rect>
    </property>
    <property name="value">
     <number>0</number>
    </property>
   </widget>
   <widget class="QLabel" name="label_2">
    <property name="geometry">
     <rect>
      <x>40</x>
      <y>170</y>
      <width>54</width>
      <height>12</height>
     </rect>
    </property>
    <property name="text">
     <string>日志信息:</string>
    </property>
   </widget>
   <widget class="QLabel" name="label_3">
    <property name="geometry">
     <rect>
      <x>110</x>
      <y>170</y>
      <width>54</width>
      <height>12</height>
     </rect>
    </property>
    <property name="text">
     <string/>
    </property>
   </widget>
  </widget>
  <widget class="QMenuBar" name="menubar">
   <property name="geometry">
    <rect>
     <x>0</x>
     <y>0</y>
     <width>523</width>
     <height>23</height>
    </rect>
   </property>
  </widget>
  <widget class="QStatusBar" name="statusbar"/>
 </widget>
 <resources/>
 <connections/>
</ui>
MainWindow调用例子:

MainWindow.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QStandardPaths>
#include "DownLoadFileHttps.h"
MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    connect(this,SIGNAL(sigProgress(qint64, qint64, qreal)),this,SLOT(sltProgress(qint64, qint64, qreal)));
    connect(this,SIGNAL(sigDownloadFinished()),this,SLOT(sltDownloadFinished()));
}

MainWindow::~MainWindow()
{
    delete ui;
}


void MainWindow::on_pushButton_clicked()
{
    QString svrloadFile = QStandardPaths::writableLocation(QStandardPaths::DownloadLocation);
    DownLoadFileHttps* dT;
    dT = new DownLoadFileHttps(ui->lineEdit->text(), svrloadFile,this);
    dT->startDownload();
}
void MainWindow::sltProgress(qint64 bytesRead, qint64 totalBytes, qreal progress)
{
    ui->progressBar->setValue(progress*100);
    QString title = QString::number(progress*100,'f',2)+"%  "+QString::number(bytesRead/(1024*1024),'f',2)+"MB/"+QString::number(totalBytes/(1024*1024),'f',2)+"MB";
    ui->label_3->setText(title);
}
void MainWindow::sltDownloadFinished()
{
    ui->label_3->setText(tr("下载完成:"));
}

MainWindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>

QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    MainWindow(QWidget *parent = nullptr);
    ~MainWindow();
signals:
    void sigProgress(qint64 bytesRead, qint64 totalBytes, qreal progress);  // 下载进度信号
    void sigDownloadFinished();  // 下载完成信号
private slots:
    void on_pushButton_clicked();
    void sltProgress(qint64 bytesRead, qint64 totalBytes, qreal progress);  // 下载进度信号
    void sltDownloadFinished();  // 下载完成信号
private:
    Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H

 

  • 4
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: qt多线程文件拷贝进度条程序的下载,在这里我将简单介绍一下实现的思路与步骤。 首先,在使用Qt进行多线程文件拷贝与进度显示之前,我们需要引入Qt的相关头文件,例如QFile、QThread等。 接下来,我们需要创建一个自定义的线程类,继承自QThread。在这个线程类,我们需要重写run()函数,在run()函数实现文件的拷贝操作。具体的拷贝操作可以使用QFile类的copy()函数。 为了实现进度条的显示,我们可以通过信号与槽机制来实现。我们可以在自定义的线程类定义一个信号,用来传递文件拷贝的进度。可以使用QFile类的size()函数获取文件的总大小,然后在每次拷贝一个块的时候,发送一个信号,参数为已拷贝的字节数,通过计算已拷贝字节数和文件总大小的比例,来计算拷贝的进度。 在主线程或主窗口类,我们需要创建一个进度条控件用于显示拷贝的进度。然后连接自定义的线程的信号与主线程的槽函数,以更新进度条的进度。 在点击开始拷贝按钮的时候,我们需要创建一个自定义的线程对象,并调用start()函数启动线程。然后在槽函数处理接收到的信号,更新进度条的进度。 以上就是实现qt多线程文件拷贝进度条程序的简要步骤。通过多线程的方式进行文件拷贝,可以使程序在拷贝较大文件时不会出现界面卡顿的情况,同时通过进度条可以直观地显示拷贝的进度。 ### 回答2: 对于Qt多线程文件拷贝进度条程序的下载,我们可以采用如下步骤: 1. 首先,在Qt创建一个新的工程,添加一个进度条控件和一个按钮控件。 2. 在主线程点击按钮事件的槽函数,创建一个新的子线程,并传递需要拷贝的文件路径和目标路径作为参数。 3. 在子线程,使用Qt文件操作类QFile进行文件的拷贝。通过打开源文件,创建目标文件并复制源文件内容到目标文件实现文件拷贝的功能。 4. 在拷贝文件的过程,可以在子线程实时更新进度条的值,并通过信号和槽机制将进度条的值传递给主线程。 5. 在主线程,接收并处理来自子线程的进度条值变化的信号,实时更新界面上的进度条的显示。 6. 当文件拷贝完成后,子线程发送一个完成信号给主线程,主线程处理该信号,提示文件拷贝已完成。 7. 最后,通过Qt的信号和槽机制,将按钮的点击事件和文件拷贝的相关操作进行连接,使得点击按钮时可以启动文件拷贝进度条程序的下载。 通过以上步骤,我们可以实现一个基于Qt多线程、带有进度条文件拷贝程序。用户可以通过点击界面上的按钮来触发拷贝过程,并实时显示拷贝的进度。这样可以提高文件拷贝的效率,并提供更好的用户体验。 ### 回答3: Qt是一种跨平台的C++应用程序开发框架,它提供了丰富的功能库和工具,可以帮助开发者快速构建各种类型的应用程序。在Qt,我们可以使用多线程来实现文件拷贝功能,并在界面上显示一个进度条,以便用户能够清楚地了解文件拷贝的进度。 首先,我们需要创建一个继承自QThread的自定义类,用于执行文件拷贝的操作。在这个类,我们可以重写run()函数,在函数执行拷贝文件的逻辑。在拷贝文件的过程,我们可以通过信号机制来实时发送拷贝进度给主线程。 在主线程,我们可以创建一个进度条控件,并与自定义的文件拷贝线程进行连接。当文件拷贝线程发送进度信号时,我们可以更新进度条的值,让用户能够实时观察到文件拷贝的进度。 同时,我们还可以提供一些用户交互的功能,比如添加文件、选择目标文件夹等。这些功能可以通过Qt的信号与槽机制来实现,用户可以触发相应的信号,然后我们在槽函数执行相应的操作,比如选择文件,选择目标文件夹等。 除此之外,为了防止文件拷贝操作过程出现的各种异常情况,我们还可以在文件拷贝线程进行错误处理和异常捕获,并通过信号与槽机制向主线程发送错误信息,以便及时通知用户。 总而言之,通过Qt多线程和信号与槽机制,我们可以简单地实现一个多线程文件拷贝进度条程序的下载功能。这样用户可以在界面上直观地看到文件拷贝的进度,并能够通过界面交互实现文件选择和目标文件夹选择等功能。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值