0

I have done some research on this topic . the thread at SO also caught my interest and I wanted to summarize my understanding and be corrected if I am going wrong on a certain path and also wanted to know how QueuedConnection would work.

Here is my understanding followed by the question. Signals can be connected manually to slots primarily through two different ways first way is using direct connection and the second way is queued connection. In case of a direct connection if the slot method that is attached to the signal is in the same thread then the slot method is called sequentially (as if it was just another method) however incase the slot is in a different thread from where the signal is launched then QueuedConnection would launch it when it finds it appropriate. (Now In this case I am not sure if it would launch a new thread or how it would proceed on doing that)

4

3 回答 3

7

插槽不属于任何特定线程,因为它们只是普通函数。但物体会。如果通过 将信号连接到插槽QueuedConnection,则信号发射将创建一个事件并将其发送到目标的事件队列中。Qt 将安排在内部处理该事件时调用您的插槽。

至于所有的事件,都是在对象的线程亲和的线程中处理的。moveToThread您可以通过调用目标对象来更改该线程。

于 2013-08-15T20:56:18.667 回答
1

在多线程环境中,当发送者和接收者对象在不同的​​线程中时。

Qt::QueuedConnection

  • 发射线程时发生了什么?它只是发出(内部 postevent,接收线程消息队列)并恢复发出者线程(不阻塞)。
  • 执行上述语句后,接收线程发生了什么?当控制返回到接收者线程的事件循环时调用该槽。

Qt::BlockingQueuedConnection

  • 发射线程发生了什么?它发出(内部 sendEvent,到接收者消息队列)并阻塞发出者线程,直到接收者槽返回。(阻塞)。
  • 接收线程发生了什么?当控制返回到接收者线程的事件循环时调用该槽。
于 2013-08-16T02:21:49.910 回答
0

粗略地说,对于 QueuedConnection,Qt 将为插槽所属的线程创建一个接收信号队列,并在线程可用时将它们一个一个启动,以便将它们存储在队列中(它完成它正在做的任何事情并返回到事件循环)。

不会启动新线程 - 插槽属于某个线程,因此将在该线程中执行。

于 2013-08-15T20:40:36.233 回答