本文介绍了从非Qt线程或抛出Qt主事件循环以4.5发出Qt信号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从非Qt线程调用emit signal1().非Qt线程是指不是来自GUI事件循环,也不是来自任何QThread run()方法或任何QThread自己的事件循环.

I'm calling a emit signal1() from a non Qt thread.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.

它只是一个pthread(pthread_create()),它调用发出信号的QObject的方法.

It is simply a pthread (pthread_create()) that calls a method of a QObject which emits signals.

例如:

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

我的pthread的运行"方法具有指向MyObject实例的指针(该实例是在主Qt GUI线程上下文中创建的,而不是在pthread中创建的),它调用emitBunchOfSignals()方法.

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.

在Qt 4.5之前令人讨厌.现在,Qt 4.5可以处理吗?它会调用qApp->PostEvent()还是某种方式使信号在Qt GUI线程(以及插槽)内发出?

Before Qt 4.5 that was nasty. Now, does Qt 4.5 handle this ?Does it call qApp->PostEvent() or something so the signal is emitted within the Qt GUI Thread (and thus the slot as well) ?

谢谢

推荐答案

您需要确保使用的是从线程到a的排队连接,因为Qt无法自动感知属于哪个线程的对象(线程"亲和力"是文档中使用的术语).在连接时执行此操作:

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). 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 (i.e. its event loop).

这篇关于从非Qt线程或抛出Qt主事件循环以4.5发出Qt信号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-05 07:19