[PyQt] moveToThread() does not cope with functools.partial

Giuseppe Corbelli giuseppe.corbelli at copanitalia.com
Mon Apr 29 17:01:50 BST 2013


On 29/04/2013 14:06, Phil Thompson wrote:
>> Works correctly with pyqtSignal.
>> I'm not able to reproduce the desired behaviour with
>> QtCore.QObject.connect.
>> Any suggestion? What I'm missing?
>
> I'm not making changes to old-style connections. It's a bug if there is
> anything that you can do with old-style connections that you can't do with
> new-style connections.

Found the reason, maybe worth changing pyqtSignal too. When checking slot 
type, besides Method, Function and functools.partial might be a good idea to 
check also anything that has a __call__ method, so that you can connect 
correctly to an object instance.
As of pyqt snapshot-4.10.2-d9c6e01fe9ce you can connect to an object directly 
(always referring to the __call__ method) but the thread affinity does not work.
Minimal sample code:

#!/usr/bin/python
# -*- coding: utf-8 -*-

import sys
import functools
import logging

from PyQt4 import QtCore

logging.basicConfig(level=logging.DEBUG)

logger = logging.getLogger('blah')


class CApp(QtCore.QCoreApplication):
     mysignal = QtCore.pyqtSignal()


class CReceiver(QtCore.QObject):
     def __call__(self):
         logger.debug("__call__ %s", QtCore.QThread.currentThreadId())

     def call(self):
         logger.debug("call %s", QtCore.QThread.currentThreadId())


app = CApp(sys.argv)
app_thread = app.thread()
logger.info("Main thread ID %s", QtCore.QThread.currentThreadId())

thread = QtCore.QThread()
thread.start()
while not thread.isRunning():
     pass

a = CReceiver()
a.moveToThread(thread)

app.mysignal.connect(a, QtCore.Qt.QueuedConnection)
app.mysignal.connect(a.call, QtCore.Qt.QueuedConnection)


timer = QtCore.QTimer()
timer.setInterval(1000)
timer.timeout.connect(app.mysignal.emit)
timer.start()

app.exec_()


Get this output:
INFO:blah:Main thread ID 3075479232
DEBUG:blah:__call__ 3075479232
DEBUG:blah:call 3042327408
DEBUG:blah:__call__ 3075479232
DEBUG:blah:call 3042327408
DEBUG:blah:__call__ 3075479232
DEBUG:blah:call 3042327408

-- 
             Giuseppe Corbelli
WASP Software Engineer, Copan Italia S.p.A
Phone: +390303666318  Fax: +390302659932
E-mail: giuseppe.corbelli at copanitalia.com


More information about the PyQt mailing list