一个疑惑的问题:QObject::killTimer: Timers cannot be stopped from another thread

来源:互联网 发布:js contents 编辑:程序博客网 时间:2024/05/21 19:32

QObject的connect函数有几种连接方式,

    a) DirectConnection,信号发送后槽函数立即执行,由sender的所在线程执行;

    b) QueuedConnection,信号发送后返回,相关槽函数由receiver所在的线程在返回到事件循环后执行;

    c)默认使用的是Qt::AutoConnection,当sender和receiver在同一个线程内时,采用DirectConnection的方式,当sender和receiver在不同的线程时,采用QueuedConnection的方式。

 

MySerialPort::MySerialPort()

    :m_pThread(newQThread()),

      m_pCom(newQSerialPort())

{

    m_pCom->moveToThread(m_pThread);

    this->moveToThread(m_pThread);

    m_pThread->start();

 

    connect(m_pCom,&QSerialPort::readyRead,this,&MySerialPort::slotDataReady);

connect(this,&MySerialPort::sigSetCOM,this,&MySerialPort::slotSetCOM);

 

//下面的slot在sender(即resendCheckTimer)所在线程执行

    connect(&resendCheckTimer,SIGNAL(timeout()),this,SLOT(slotResendProcess()),Qt::DirectConnection);

}

 

voidMySerialPort::slotResendProcess()

{

    resendCheckTimer.stop();

}

1.  第一种情况即上述方式

 

打印信息:

main thread------------------------- QThread(0x11918c0)

MySerialPort::slotDataReady()------------------: 0x3084

MySerialPort::slotResendProcess----------------:0x2f20

2.第二种情况

改为:

connect(&resendCheckTimer,SIGNAL(timeout()),this,SLOT(slotResendProcess()));//slot默认会在receiver(即this)所在线程执行

 

打印信息:

main thread------------------------- QThread(0x126918c0)

MySerialPort::slotDataReady()------------------: 0x2db8 //this所在线程

MySerialPort::slotResendProcess----------------:0x2db8 //this所在线程

QObject::killTimer: Timers cannot be stopped from another thread 

注意:最下面一条警告,执行resendCheckTimer.stop()报错了,说明在第一种情况下(Qt::DirectConnection),槽函数会在receiver(即this)所在线程跟sender(即resendCheckTimer)所在线程不一致,且sender所在线程和主线程、次线程都不是一个线程,有点不明白了。。。。

阅读全文
0 0
原创粉丝点击