Qt 4.5 - Is emitting signal a function call, or a thread, and does it blocks?

后端 未结 3 1756
再見小時候
再見小時候 2021-01-31 03:03

I am not sure about the nature of the signal/slot mechanism in Qt 4.5. When a signal is emitted, is it a blocking function call or a thread? Say this

emit GrabLa         


        
3条回答
  •  生来不讨喜
    2021-01-31 03:36

    The biggest problem is that you just can't know. That is, if you're looking from the class's point of view. When you emit, you don't know what will happen:

    • If no one is connected to the signal, nothing happens
    • If someone from the same thread is connected using any type except Qt::QueuedConnection, the call will be blocking
    • If someone from the same thread is connected using Qt::QueuedConnection, the call will be non-blocking
    • If someone from a different thread is connected using Qt::DirectConnection (be very careful when you do that!) or Qt::BlockingQueuedConnection, the call will be blocking
    • If someone from a different thread is connected using Qt::AutoConnection or Qt::QueuedConnection, the call will be non-blocking

    It gets even more difficult to know what will happen if multiple objects are connected to the signal. In that case some slots might have run while others are still queued. There is, by the way, no thread involved with a non-blocking connect. There's only an event that gets posted in the event loop of the thread of the receiving object.

提交回复
热议问题