• 5176阅读
  • 2回复

[提问]Qt槽函数的执行问题 [复制链接]

上一主题 下一主题
离线swuster_225
 
只看楼主 倒序阅读 楼主  发表于: 2012-04-24
假设我在一个类中定义了一个普通函数 funcA(),然后定义了一个槽函数slot(),简略如下
void Test::funcA()
{
   if(m_index != 2)
   {
     ----------------------------------- interrupt
     DO SOMETHINS  
   }
}

slot:
void Test::slot()
{
   m_index = 2;
}
现有一疑惑,如果恰好执行完funcA()内部的if(m_index != 2),且进入到条件语句中,就在此刻,与slot()关联的信号产生,此时程序会不会在----------------------------------- interrupt处中断,转而执行slot()函数呢?如果执行了,那么这个地方就产生了不可预知的错误
I Believe I can Fly to the Sky .
离线453413516

只看该作者 1楼 发表于: 2012-04-25
条件语句已经执行完了,回到中断处是执行下一条语句,不会回头在执行if判断
离线zzh12215
只看该作者 2楼 发表于: 2012-04-25
hehe,这里楼主 忽略了一个问题:你的程序考虑了多线程吗

1、如果不是多线程,那么这种情况不可能发生,这样的话 两个函数会串行执行
2、如果是多线程,这种情况有可能,也比较复杂,可以查看一下connect函数的相关说明,里面提到了这种情况。下面贴一下相关的部分:
enum Qt::ConnectionType

This enum describes the types of connection that can be used between signals and slots. In particular, it determines whether a particular signal is delivered to a slot immediately or queued for delivery at a later time.

Constant    Value    Description
Qt::AutoConnection    0    (default) If the signal is emitted from a different thread than the receiving object, the signal is queued, behaving as Qt::QueuedConnection. Otherwise, the slot is invoked directly, behaving as Qt::DirectConnection. The type of connection is determined when the signal is emitted.
Qt::DirectConnection    1    The slot is invoked immediately, when the signal is emitted.
Qt::QueuedConnection    2    The slot is invoked when control returns to the event loop of the receiver's thread. The slot is executed in the receiver's thread.

剩下的就不贴了,呵呵
快速回复
限100 字节
 
上一个 下一个