From b99225bd3c6d3cf62b884ab665b747b40a30e4dd Mon Sep 17 00:00:00 2001 From: zty199 <46324746+zty199@users.noreply.github.com> Date: Sat, 4 Mar 2023 22:42:08 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=E5=AE=89=E8=A3=85?= =?UTF-8?q?=E4=B8=8D=E6=88=90=E5=8A=9F=E6=97=B6=EF=BC=8C=E5=85=B3=E9=97=AD?= =?UTF-8?q?=E4=B8=BB=E7=AA=97=E5=8F=A3=E4=BC=9A=E7=9B=B4=E6=8E=A5=E9=80=80?= =?UTF-8?q?=E5=87=BA=EF=BC=8C=E4=B8=94=E9=87=8D=E5=90=AF=E5=90=8E=E6=B2=A1?= =?UTF-8?q?=E6=9C=89=E8=AE=B0=E5=BD=95=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 由于安装操作在线程中异步执行,原始逻辑中仅判断下载完成后就检测是否还有后续任务,存在安装未结束就退出的情况 Log: 调整判断后续下载任务位置到槽函数中,每个任务安装完成结果信号触发后优先判断安装是否成功,再判断是否需要退出 --- src/widgets/common/downloaditem.cpp | 14 +++++------ src/widgets/common/downloaditem.h | 2 +- src/widgets/downloadlistwidget.cpp | 39 ++++++++++++++++++----------- src/widgets/downloadlistwidget.h | 1 + 4 files changed, 34 insertions(+), 22 deletions(-) diff --git a/src/widgets/common/downloaditem.cpp b/src/widgets/common/downloaditem.cpp index 8142790..ac5bbf6 100644 --- a/src/widgets/common/downloaditem.cpp +++ b/src/widgets/common/downloaditem.cpp @@ -64,9 +64,9 @@ QString DownloadItem::getName() /*************************************************************** * @brief 告知界面,准备安装 - * @param + * @param * @note 如果正在安装,返回-1 - * @Sample usage: DownloadItem::install(0); + * @Sample usage: DownloadItem::install(0); **************************************************************/ int DownloadItem::readyInstall() { @@ -110,9 +110,9 @@ void DownloadItem::setSpeed(QString s) /*************************************************************** * @brief 安装当前应用 - * @param int t, t为安装方式,可以为 0,1,2 + * @param int t, t为安装方式,可以为 0,1,2 * @note 执行这个函数时,需要已经检查是否可以安装,但该函数仍然会再检测一次! - * @Sample usage: DownloadItem::install(0); + * @Sample usage: DownloadItem::install(0); **************************************************************/ void DownloadItem::install(int t) { @@ -165,9 +165,9 @@ void DownloadItem::on_pushButton_3_clicked() /*************************************************************** * @brief 实际安装应用 - * @param int t, t为安装方式,可以为 0,1,2 + * @param int t, t为安装方式,可以为 0,1,2 * @note 备注 - * @Sample usage: slotAsyncInstall(0); + * @Sample usage: slotAsyncInstall(0); **************************************************************/ void DownloadItem::slotAsyncInstall(int t) { @@ -238,5 +238,5 @@ void DownloadItem::slotAsyncInstall(int t) ui->widget_spinner->hide(); DownloadItem::isInstall = false; - emit finished(); + emit finished(error == 0 && !haveError && !notRoot); } diff --git a/src/widgets/common/downloaditem.h b/src/widgets/common/downloaditem.h index b00c3ee..13c2d2b 100644 --- a/src/widgets/common/downloaditem.h +++ b/src/widgets/common/downloaditem.h @@ -62,7 +62,7 @@ private slots: void slotAsyncInstall(int t); signals: - void finished(); + void finished(bool success); }; #endif // DOWNLOADITEM_H diff --git a/src/widgets/downloadlistwidget.cpp b/src/widgets/downloadlistwidget.cpp index 8cd666c..89fa8ba 100644 --- a/src/widgets/downloadlistwidget.cpp +++ b/src/widgets/downloadlistwidget.cpp @@ -91,10 +91,14 @@ DownloadItem* DownloadListWidget::addItem(QString name, QString fileName, QStrin { return nullptr; } + urList.append(downloadurl); allDownload += 1; toDownload += 1; + DownloadItem *di = new DownloadItem; + connect(di, &DownloadItem::finished, this, &DownloadListWidget::slotInstallFinished, Qt::QueuedConnection); + dlist << downloadurl; downloaditemlist << di; di->setName(name); @@ -167,22 +171,9 @@ void DownloadListWidget::httpFinished() // 完成下载 { continue; } - toDownload -= 1; // 安装完以后减少待安装数目 - qDebug() << "Download: 还没有下载的数目:" << toDownload; - - if (toDownload == 0) - { - Application *app = qobject_cast(qApp); - MainWindow *mainWindow = app->mainWindow(); - if (mainWindow->isCloseWindowAnimation() == true) - { - qDebug() << "Download: 后台安装结束,退出程序"; - qApp->quit(); - } - } - downloaditemlist[nowDownload - 1]->free = true; emit downloadFinished(); + if (nowDownload < allDownload) { // 如果有排队则下载下一个 @@ -241,3 +232,23 @@ void DownloadListWidget::on_pushButton_clicked() { QDesktopServices::openUrl(QUrl("file:///tmp/spark-store", QUrl::TolerantMode)); } + +void DownloadListWidget::slotInstallFinished(bool success) +{ + // NOTE: 仅在安装成功后判断是否需要退出后台 + if (success) { + toDownload -= 1; // 安装完以后减少待安装数目 + qDebug() << "Download: 还没有下载的数目:" << toDownload; + + if (toDownload == 0) + { + Application *app = qobject_cast(qApp); + MainWindow *mainWindow = app->mainWindow(); + if (mainWindow->isCloseWindowAnimation() == true) + { + qDebug() << "Download: 后台安装结束,退出程序"; + qApp->quit(); + } + } + } +} diff --git a/src/widgets/downloadlistwidget.h b/src/widgets/downloadlistwidget.h index 09517f5..6faebc0 100644 --- a/src/widgets/downloadlistwidget.h +++ b/src/widgets/downloadlistwidget.h @@ -60,6 +60,7 @@ signals: private slots: void on_pushButton_clicked(); + void slotInstallFinished(bool success); }; #endif // DOWNLOADLISTWIDGET_H