From 5c393595d779b63b2143bd7d4ead54d906366777 Mon Sep 17 00:00:00 2001 From: KenanZhu <3471685733@qq.com> Date: Sat, 21 Mar 2026 01:53:22 +0800 Subject: [PATCH] =?UTF-8?q?fix(ALWebDriverDownloadDialog):=20=E9=87=8D?= =?UTF-8?q?=E5=91=BD=E5=90=8D=E4=BF=A1=E5=8F=B7=E9=81=BF=E5=85=8D=E4=B8=8E?= =?UTF-8?q?=20QThread=20=E5=86=85=E7=BD=AE=E4=BF=A1=E5=8F=B7=E5=86=B2?= =?UTF-8?q?=E7=AA=81=E5=B9=B6=E6=94=B9=E8=BF=9B=E7=BA=BF=E7=A8=8B=E7=94=9F?= =?UTF-8?q?=E5=91=BD=E5=91=A8=E6=9C=9F=E7=AE=A1=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/gui/ALWebDriverDownloadDialog.py | 52 +++++++++++++++++++--------- 1 file changed, 35 insertions(+), 17 deletions(-) diff --git a/src/gui/ALWebDriverDownloadDialog.py b/src/gui/ALWebDriverDownloadDialog.py index 6f34702..0891603 100644 --- a/src/gui/ALWebDriverDownloadDialog.py +++ b/src/gui/ALWebDriverDownloadDialog.py @@ -35,9 +35,9 @@ class DownloadWorker(QThread): """ progress = Signal(float, int, float, str) - finished = Signal(object, str) - error = Signal(str) - cancelled = Signal() + downloadFinished = Signal(object, str) + downloadError = Signal(str) + downloadCancelled = Signal() def __init__( self, @@ -66,7 +66,7 @@ class DownloadWorker(QThread): ): try: if self.__cancelled: - self.cancelled.emit() + self.downloadCancelled.emit() return self.__driver_path = self.__driver_manager.installDriver( self.__driver_info, @@ -74,15 +74,15 @@ class DownloadWorker(QThread): cancel_event=self.__cancel_event ) if self.__cancelled: - self.cancelled.emit() + self.downloadCancelled.emit() return if self.__driver_path: - self.finished.emit(self.__driver_path, "") + self.downloadFinished.emit(self.__driver_path, "") else: - self.error.emit("下载失败: 未返回有效路径") + self.downloadError.emit("下载失败: 未返回有效路径") except Exception as e: if not self.__cancelled: - self.error.emit(f"下载失败: {str(e)}") + self.downloadError.emit(f"下载失败: {str(e)}") def onProgress( self, @@ -97,6 +97,20 @@ class DownloadWorker(QThread): if not self.__cancelled: self.progress.emit(downloaded, total, speed, message) + def stop( + self + ): + """ + Cancel and wait for the thread to finish. + Must only be called from the main thread. + """ + + self.cancel() + if not self.isFinished(): + if not self.wait(5000): + self.terminate() + self.wait() + class ALWebDriverDownloadDialog(QDialog): @@ -328,9 +342,10 @@ class ALWebDriverDownloadDialog(QDialog): self.ProgressText.setText("正在下载驱动...") self.__download_thread = DownloadWorker(self.__driver_manager, driver_info) self.__download_thread.progress.connect(self.onDownloadProgress) - self.__download_thread.finished.connect(self.onDownloadFinished) - self.__download_thread.error.connect(self.onDownloadError) - self.__download_thread.cancelled.connect(self.onDownloadCancelled) + self.__download_thread.downloadFinished.connect(self.onDownloadFinished) + self.__download_thread.downloadError.connect(self.onDownloadError) + self.__download_thread.downloadCancelled.connect(self.onDownloadCancelled) + self.__download_thread.finished.connect(self.__onThreadFinished) self.__download_thread.start() @Slot() @@ -363,7 +378,6 @@ class ALWebDriverDownloadDialog(QDialog): if 0 <= index < len(self.__driver_infos): driver_info = self.__driver_infos[index] self.updateDriverInfoDisplay(driver_info) - self.__download_thread = None self.ConfirmButton.setEnabled(True) self.DownloadButton.setEnabled(False) self.RefreshButton.setEnabled(True) @@ -389,9 +403,6 @@ class ALWebDriverDownloadDialog(QDialog): self ): - if self.__download_thread: - self.__download_thread.wait(3000) - self.__download_thread = None index = self.DriverComboBox.currentIndex() if 0 <= index < len(self.__driver_infos): driver_info = self.__driver_infos[index] @@ -458,13 +469,20 @@ class ALWebDriverDownloadDialog(QDialog): if reply == QMessageBox.StandardButton.No: event.ignore() return - self.__download_thread.cancel() - self.__download_thread.wait(5000) + self.__download_thread.stop() if not self.__confirmed: self.__selected_driver_info = None event.accept() super().closeEvent(event) + def __onThreadFinished( + self + ): + + if self.__download_thread: + self.__download_thread.deleteLater() + self.__download_thread = None + def getSelectedDriverInfo( self