繁体   English   中英

PySide2 使用 QProgressBar 作为信号参数

[英]PySide2 use QProgressBar as signal argument

我正在尝试解决 PySide2、QThread 和 Signal/Slot 机制的问题。

我想要实现的是更新通过信号/插槽机制作为参考传递的特定进度条。

问题是我的 ProgressBarThread 类中self.gui_connection.signal_progress_value.emit(lambda: self.progressbar, value)的调用。

程序在以 QProgressBar 和 int 值作为参数发出信号后停止,并导致 Seg Fault。 如果我只是将一个 int 值传递给信号并在我的 ProgressBarThread 中调用 emit 一切正常。

难道不能通过信号/槽机制传递对象吗?

(减少)代码示例:

GUISignal.py

from PySide2.QtCore import QObject, Signal
from PySide2.QtWidgets import QProgressBar

class GUISignal(QObject):
    signal_progress_value = Signal(QProgressBar, int)

主文件

# File: main.py
import sys
from PySide2.QtUiTools import QUiLoader
from PySide2.QtWidgets import QApplication, QPushButton
from PySide2.QtCore import QFile
from config.configManager import ConfigManager
from layout.srcConfigLayout import SrcConfigLayout
from layout.ingestLayout import IngestLayout

if __name__ == "__main__":
    app = QApplication(sys.argv)

    ui_file = QFile("main_window.ui")
    ui_file.open(QFile.ReadOnly)

    loader = QUiLoader()
    window = loader.load(ui_file)
    ui_file.close()
    window.show()

    src_config_layout = SrcConfigLayout(window)
    ingestLayout = IngestLayout(window)
    src_config_layout.load_config()
    ingestLayout.load_config()

    sys.exit(app.exec_())

摄取布局.py

from PySide2.QtWidgets import QTableWidget, QTableWidgetItem, QPushButton, QProgressBar
from PySide2.QtCore import QObject, Slot

from util.ingestManager import IngestManager
from config.configManager import ConfigManager
from util.progressBarThread import ProgressBarThread

from functools import partial

class IngestLayout(QObject):    

    def __init__(self, parent):
        super().__init__()
        self.parent = parent

    def handleIngestClicked(self, srcpath, destpath, progressbar):
        ingestManager = IngestManager()
        ingestManager.copy_to_destination(self, srcpath, destpath)
        progressThread = ProgressBarThread(srcpath, destpath, progressbar, self.parent)
        progressThread.gui_connection.signal_progress_value.connect(self.updateProgressbar)
        progressThread.start()

    @Slot(QProgressBar, int)
    def updateProgressbar(self, progressbar: QProgressBar, value: int):
        pass
        # progressbar.setValue(value)

进度条线程.py

from PySide2.QtCore import QThread
import os
import threading
import time
from signals.GUISignal import GUISignal

class ProgressBarThread(QThread):
    def __init__(self, srcpath, destpath, progressbar, parent):
        super(ProgressBarThread, self).__init__(parent)
        self.gui_connection = GUISignal()
        self.srcpath = srcpath
        self.destpath = destpath
        self.src_size = self.size(srcpath)
        self.progressbar = progressbar

    def run(self):
        self.periodically_compare_folder_size()

    def periodically_compare_folder_size(self):
        dest_size = self.size(self.destpath)
        while self.src_size > dest_size:
            value = self.calc_progress(self.src_size, dest_size)
            self.gui_connection.signal_progress_value.emit(lambda: self.progressbar, value)
            dest_size = self.size(self.destpath)
            time.sleep(2)
        else:
            self.gui_connection.signal_progress_value.emit(lambda: self.progressbar, 100)
            print(100)
            return

    def size(self, path, *, follow_symlinks=False):
        try:
            with os.scandir(path) as it:
                return sum(self.size(entry, follow_symlinks=follow_symlinks) for entry in it)
        except NotADirectoryError:
            return os.stat(path, follow_symlinks=follow_symlinks).st_size

    def calc_progress(self, src_size, dest_size):
        return dest_size / src_size * 100

问题是程序在发出带有 QProgressBar 和值作为参数的信号后停止,并导致 Seg Fault。 如果我只是将一个 int 值传递给信号并在我的 ProgressBarThread 中调用 emit 一切正常。

难道不能通过信号/槽机制传递对象吗?

信号没有必要作为 QProgressBar 日期发送,除了 GUI 不是线程安全的,另一方面也没有必要使用 lambda。

综上所述,解决方法是:

class GUISignal(QObject):
    signal_progress_value = Signal(int)
class ProgressBarThread(QThread):
    def __init__(self, srcpath, destpath, parent):
        super(ProgressBarThread, self).__init__(parent)
        self.gui_connection = GUISignal()
        self.srcpath = srcpath
        self.destpath = destpath
        self.src_size = self.size(srcpath)

    def run(self):
        self.periodically_compare_folder_size()

    def periodically_compare_folder_size(self):
        dest_size = self.size(self.destpath)
        while self.src_size > dest_size:
            value = self.calc_progress(self.src_size, dest_size)
            self.gui_connection.signal_progress_value.emit(value)
            dest_size = self.size(self.destpath)
            time.sleep(2)
        else:
            self.gui_connection.signal_progress_value.emit(100)
            print(100)
            return
    # ...
def handleIngestClicked(self, srcpath, destpath, progressbar):
    ingestManager = IngestManager()
    ingestManager.copy_to_destination(self, srcpath, destpath)
    progressThread = ProgressBarThread(srcpath, destpath, self.parent) progressThread.gui_connection.signal_progress_value.connect(progressbar.setValue)
    progressThread.start()

更新:

在我的回答的前一部分中,我将 GUI 与业务逻辑分开,因为这将具有更具可扩展性的软件。 但是 OP 似乎不希望这样,所以简单的解决方案是不要使用 lambda 方法,因为它是不必要的:

self.gui_connection.signal_progress_value.emit(self.progressbar, value)
self.gui_connection.signal_progress_value.emit(self.progressbar, 100)

已解决:问题是 QThread 类中的发射函数中的 lambda 函数。 该对象似乎不是由 lambda 函数传递的。 只使用.emit(self,progressbar, 100) is working.

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM