Hi @damada
I’m not yet got thru your code…
Quick look, I suspect instance of data_signal is not there same as you can expect, but not sure…
For now, here is simple working example:
from PySide6.QtCore import Qt, Signal, QRunnable, QThreadPool
from PySide6.QtWidgets import QApplication, QLabel, QVBoxLayout, QPushButton, QWidget
import time
class WorkerTask(QRunnable):
def __init__(self, signal):
super().__init__()
self.signal = signal
def run(self):
for i in range(5):
time.sleep(1)
data = {"count": i, "message": f"Emitted data #{i}"}
self.signal.emit(data)
class MainWindow(QWidget):
data_signal = Signal(dict)
def __init__(self):
super().__init__()
self.setWindowTitle("QThreadPool Example")
self.resize(400, 200)
self.label = QLabel("Press the button to start")
self.label.setAlignment(Qt.AlignmentFlag.AlignHCenter | Qt.AlignmentFlag.AlignVCenter)
self.label.setWordWrap(True)
self.button = QPushButton("Start Worker")
self.button.clicked.connect(self.start_worker)
layout = QVBoxLayout()
layout.addWidget(self.label)
layout.addWidget(self.button)
self.setLayout(layout)
self.thread_pool = QThreadPool()
self.data_signal.connect(self.handle_data)
def start_worker(self):
self.button.setEnabled(False)
worker = WorkerTask(self.data_signal)
self.thread_pool.start(worker)
def handle_data(self, data):
self.label.setText(f"Received: {data['message']} (count: {data['count']})")
if data["count"] == 4:
self.button.setEnabled(True)
if __name__ == "__main__":
app = QApplication([])
window = MainWindow()
window.show()
app.exec()