How deep can signals and slots go? A few fundamental questions about signals and slots...



  • I have a general question about signals and slots and the way that they are handled. In order for signals to be processed, I assume they need to be attached to a specific thread so that a message loop can register the fact they they have been fired (emitted) and the corresponding slot can be called.

    So how deep can they go?
    Must all signals and slots be "connected" in the base application/loop? Or can one create a signal/slot connection in objects created on the fly?
    If they can be created on the fly in objects other than the object containing the main message loop, how does that connection manager to be embedded in the message loop?

    Mainly... I am trying to understand what things might go wrong when using signals and slots and to make sure that all my signals make it to an event loop somewhere...



  • Signals and slots are not assigned to threads. QObjects that have signals and slots have so called thread affinity. A QObject is by default assigned to the thread it was created in. You can control that using QObject::moveToThread(...).

    Connections don't require active loop at connection time (when you call connect). A loop in the target thread is required at the emission-time (when you call emit someSignal() ) of "queued connections":http://qt-project.org/doc/qt-5/qt.html#ConnectionType-enum. Connections can be created and destroyed anywhere and at any time and don't require any parent object to be made (QObject::connect() is a static method).

    Connections can be made for objects in different threads. There are different types of connection (optionally controlled by the last param of connect() ).

    More on how threading, event loops, and connections work together "can be found here":http://qt-project.org/doc/qt-5/threads-qobject.html and "here":http://qt-project.org/doc/qt-5/qobject.html#thread-affinity



  • Thanks! I appreciate the info.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.