From 33ea9ee0650361f9dc552cc935bb6439197872a2 Mon Sep 17 00:00:00 2001 From: uniartisan Date: Sat, 18 Feb 2023 20:06:36 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E5=A4=9A=E4=B8=AA=E5=BA=94=E7=94=A8?= =?UTF-8?q?=E5=AE=89=E8=A3=85=E5=8F=AF=E8=83=BD=E4=BC=9A=E5=87=BA=E7=8E=B0?= =?UTF-8?q?=E6=9F=90=E4=B8=80=E4=B8=AA=E5=BA=94=E7=94=A8=E6=B2=A1=E6=9C=89?= =?UTF-8?q?=E5=AE=89=E8=A3=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/widgets/common/downloaditem.cpp | 29 ++++++++++++- src/widgets/common/downloaditem.h | 2 +- src/widgets/downloadlistwidget.cpp | 67 ++++++++++++++++------------- 3 files changed, 65 insertions(+), 33 deletions(-) diff --git a/src/widgets/common/downloaditem.cpp b/src/widgets/common/downloaditem.cpp index 8aceda5..8ea7d56 100644 --- a/src/widgets/common/downloaditem.cpp +++ b/src/widgets/common/downloaditem.cpp @@ -61,8 +61,21 @@ QString DownloadItem::getName() return ui->label_filename->text(); } -void DownloadItem::readyInstall() + +/*************************************************************** + * @brief 告知界面,准备安装 + * @param + * @note 如果正在安装,返回-1 + * @Sample usage: DownloadItem::install(0); + **************************************************************/ +int DownloadItem::readyInstall() { + // 检查是否正在安装,如果是返回错误 -1 + if (isInstall) + { + return -1; + } + if (!close) { ui->progressBar->hide(); @@ -70,7 +83,9 @@ void DownloadItem::readyInstall() ui->pushButton_install->show(); DownloadItem::install(0); ui->pushButton_2->hide(); + return 1; } + return 0; } void DownloadItem::setFileName(QString fileName) @@ -93,6 +108,12 @@ void DownloadItem::setSpeed(QString s) speed = s; } +/*************************************************************** + * @brief 安装当前应用 + * @param int t, t为安装方式,可以为 0,1,2 + * @note 备注 + * @Sample usage: DownloadItem::install(0); + **************************************************************/ void DownloadItem::install(int t) { if (!isInstall) @@ -142,6 +163,12 @@ void DownloadItem::on_pushButton_3_clicked() output_w->show(); } +/*************************************************************** + * @brief 实际安装应用 + * @param int t, t为安装方式,可以为 0,1,2 + * @note 备注 + * @Sample usage: slotAsyncInstall(0); + **************************************************************/ void DownloadItem::slotAsyncInstall(int t) { QProcess installer; diff --git a/src/widgets/common/downloaditem.h b/src/widgets/common/downloaditem.h index 05c9a3d..b00c3ee 100644 --- a/src/widgets/common/downloaditem.h +++ b/src/widgets/common/downloaditem.h @@ -34,7 +34,7 @@ public: void setMax(qint64); void setName(QString); QString getName(); - void readyInstall(); + int readyInstall(); void setFileName(QString); void seticon(const QPixmap); diff --git a/src/widgets/downloadlistwidget.cpp b/src/widgets/downloadlistwidget.cpp index 135a0f9..07d7046 100644 --- a/src/widgets/downloadlistwidget.cpp +++ b/src/widgets/downloadlistwidget.cpp @@ -3,6 +3,8 @@ #include #include #include +#include + DownloadListWidget::DownloadListWidget(QWidget *parent) : DBlurEffectWidget(parent), ui(new Ui::DownloadListWidget) { @@ -65,20 +67,6 @@ DownloadListWidget::~DownloadListWidget() void DownloadListWidget::clearItem() { - // QListWidgetItem *item = nullptr; - // while ((item = ui->listWidget->takeItem(0)) != nullptr) - // { - // QWidget *card = ui->listWidget->itemWidget(item); - // if (card) - // { - // card->deleteLater(); - // card = nullptr; - // } - // delete item; - // item = nullptr; - // } - - // ui->listWidget->vScrollBar->scrollTop(); ui->listWidget->clear(); } @@ -142,30 +130,47 @@ void DownloadListWidget::startRequest(QUrl url, QString fileName) downloadController->startDownload(url.toString()); } + +/*************************************************************** + * @brief 下载列表完成下载的回调函数 + * @param + * @note 如果正在安装,则在新开的线程空间中等待上一个安装完 + * @Sample usage: + **************************************************************/ void DownloadListWidget::httpFinished() // 完成下载 { isdownload = false; isBusy = false; - downloaditemlist[nowDownload - 1]->readyInstall(); - downloaditemlist[nowDownload - 1]->free = true; - emit downloadFinished(); - if (nowDownload < allDownload) + + QtConcurrent::run([=]() { - // 如果有排队则下载下一个 - qDebug() << "切换下一个下载..."; - nowDownload += 1; - while (downloaditemlist[nowDownload - 1]->close) + while (downloaditemlist[nowDownload - 1]->readyInstall() == -1) { - nowDownload += 1; - if (nowDownload >= allDownload) - { - nowDownload = allDownload; - return; - } + continue; } - QString fileName = downloaditemlist[nowDownload - 1]->getName(); - startRequest(urList.at(nowDownload - 1), fileName); - } + + downloaditemlist[nowDownload - 1]->free = true; + emit downloadFinished(); + if (nowDownload < allDownload) + { + // 如果有排队则下载下一个 + qDebug() << "切换下一个下载..."; + nowDownload += 1; + while (downloaditemlist[nowDownload - 1]->close) + { + nowDownload += 1; + if (nowDownload >= allDownload) + { + nowDownload = allDownload; + return; + } + } + QString fileName = downloaditemlist[nowDownload - 1]->getName(); + startRequest(urList.at(nowDownload - 1), fileName); + } + }); + + } void DownloadListWidget::updateDataReadProgress(QString speedInfo, qint64 bytesRead, qint64 totalBytes)