简体   繁体   English

从非Qt线程或ouside Qt主事件循环发出Qt信号,为4.5

[英]emit Qt signal from non Qt Thread or ouside Qt main event loop with at 4.5

I'm calling a emit signal1() from a non Qt thread. 我从非Qt线程调用emit signal1() By non Qt thread I mean not from the GUI Event Loop and not from any QThread run() method or any QThread own event loop. 非Qt线程我的意思不是来自GUI事件循环而不是来自任何QThread run()方法或任何QThread自己的事件循环。

It is simply a pthread (pthread_create()) that calls a method of a QObject which emits signals. 它只是一个pthread(pthread_create()),它调用一个发出信号的QObject方法。

ex: 例如:

MyQbject: public QObject
{
...
void emitBunchOfSignals()
{
 emit signal1();
 emit signal2();
 ...
}
...
}

the "run" method of my pthread which has a pointer to a MyObject instance (instance that was created within the main Qt GUI thread context NOT the pthread) calls the emitBunchOfSignals() methods. 我的pthread的“run”方法,它有一个指向MyObject实例的指针(在主Qt GUI线程上下文而不是pthread中创建的实例)调用emitBunchOfSignals()方法。

Before Qt 4.5 that was nasty. 在Qt 4.5之前,这是令人讨厌的。 Now, does Qt 4.5 handle this ? 现在,Qt 4.5会处理这个吗? Does it call qApp->PostEvent() or something so the signal is emitted within the Qt GUI Thread (and thus the slot as well) ? 它是否调用qApp->PostEvent()或其他东西,以便在Qt GUI线程内发出信号(因此也是插槽)?

thanks 谢谢

What you need to make sure is that you use a queued connection to a from threads, as Qt cannot autmatically sense which object that belong to which thread ("thread affinity" is the term used in the documentation). 您需要确保使用与线程的排队连接,因为Qt无法自动检测哪个对象属于哪个线程(“线程关联”是文档中使用的术语)。 You do this when connecting: 您在连接时执行以下操作:

connect(src, SIGNAL(signal-signature), dest, SLOT(slot-signature), Qt::QueuedConnection);

That will result in the signal being put on the event loop of the destination, and the slot being called when its thread is running (ie its event loop). 这将导致信号被放置在目标的事件循环上,并且当其线程正在运行时(即其事件循环)调用该槽。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM