[Development] Why is QSignalSpy using Qt::DirectConnection?

Olivier Goffart olivier at woboq.com
Fri Oct 24 16:03:03 CEST 2014

On Friday 24 October 2014 15:41:20 Roland Winklmeier wrote:
> Dear list,
> quick question: I spotted the class QSignalSpy while writing unit tests for
> one of my projects. There was nothing written in the documentation, that it
> is not thread safe and had assumed it is. So I connected signals from a
> QObject running in a different thread and got several warnings (timers
> cannot be killed, etc).
> After inspecting its header file, I saw the usage of Qt::DirectConnection.
> if (!QMetaObject::connect(obj, sigIndex, this, memberOffset,
> Qt::DirectConnection, 0)).
> I wonder now if this is intended or the two occurrences can be changed to
> Qt::AutoConnection.
> if (!QMetaObject::connect(obj, sigIndex, this, memberOffset,
> Qt::AutoConnection, 0)).
> That should make the class pretty thread safe, no?
> There is even a suggestion report:
> https://bugreports.qt-project.org/browse/QTBUG-40616.
> If you guys are happy, I'll send the patch to gerrit. But before doing it,
> I wanted to check with you, if there is anything I might have missed.

I don't know if there is a reason it was explicitly set DirectConnection.

Since QSignalSpy is currently not thread safe, I guess this won't be a 
behaviour change to change it to AutoConnection.

So I'd say that it it safe to change. Please submit your change to gerrit.


Woboq - Qt services and support - http://woboq.com - http://code.woboq.org

More information about the Development mailing list