2 years ago

#31018

test-img

eyal0931

Slot method called in the wrong thread (pyqt)

This class suppose to handle a long task in a separate thread.

Despite moving my object to a new thread, and using queued connection, the signal is handled at the main gui thread instead of self.thread. The main gui thread is the thread that did called startit of course..

This is the code:

class DoLongProcess(QObject):

    def __init__(self,task):
        QObject.__init__(self)
        self._task=task
        self._realtask=None
        self.thread = QThread()
        self.moveToThread(self.thread)
        self.thread.started.connect(self.run,Qt.QueuedConnection)

    @Slot()
    def run(self):
        self._realtask()


    def startit(self,*params):
        self._realtask= partial(self._task,*params)
        self.thread.start()

I use pyside6. I tried to to split __init__ into two methods and moving the thread from outside. Also tried without the QueuedConnection attribute (which suppose to do this exactly). None of this had an effect.

Do you know why it doesn't work as expected?

python

multithreading

qt

signals-slots

0 Answers

Your Answer

Accepted video resources