• 10076阅读
  • 5回复

<QThread,XSendEvent>怎样在线程间传递数据? [复制链接]

上一主题 下一主题
离线ieasylive
 

只看楼主 正序阅读 楼主  发表于: 2009-09-18
<QThread,XSendEvent>怎样在线程间传递数据?
— 本帖被 XChinux 从 General Qt Programming 移动到本区(2011-01-02) —
请问怎样在两个子线程中传递数据呢?
如果通过XSendEvent发送数据给另一个线程,那么接收的线程怎么读取发送过来的数据?
[ 此帖被ieasylive在2009-09-18 16:22重新编辑 ]
离线jetcai1900
只看该作者 5楼 发表于: 2011-03-27
谢谢
离线gene
只看该作者 4楼 发表于: 2009-12-06
不知道这个对你有没有用
Qt的signal/slot机制原理

signal/slot在底层会使用三种方式传递消息。参见QObject::connect()方法:
bool QObject::connect ( const QObject * sender, const char * signal, const QObject * receiver, const char * method, Qt::ConnectionType type = Qt::AutoCompatConnection )
最后一个参数是就是传递消息的方式了,有四个取值:

Qt::DirectConnection
When emitted, the signal is immediately delivered to the slot.
假设当前有4个slot连接到QPushButton::clicked(bool),当按钮被按下时,QT就把这4个slot按连接的时间顺序调用一遍。显然这种方式不能跨线程(传递消息)。

Qt::QueuedConnection
When emitted, the signal is queued until the event loop is able to deliver it to the slot.
假设当前有4个slot连接到QPushButton::clicked(bool),当按钮被按下时,QT就把这个signal包装成一个 QEvent,放到消息队列里。QApplication::exec()或者线程的QThread::exec()会从消息队列里取消息,然后调用 signal关联的几个slot。这种方式既可以在线程内传递消息,也可以跨线程传递消息。

Qt::BlockingQueuedConnection
Same as QueuedConnection, except that the current thread blocks until the slot has been delivered. This connection type should only be used for receivers in a different thread. Note that misuse of this type can lead to dead locks in your application.
与Qt::QueuedConnection类似,但是会阻塞等到关联的slot都被执行。这里出现了阻塞这个词,说明它是专门用来多线程间传递消息的。

Qt::AutoConnection
If the signal is emitted from the thread in which the receiving object lives, the slot is invoked directly, as with Qt::DirectConnection; otherwise the signal is queued, as with Qt::QueuedConnection.
这种连接类型根据signal和slot是否在同一个线程里自动选择Qt::DirectConnection或Qt::QueuedConnection
离线ieasylive

只看该作者 3楼 发表于: 2009-09-21
看了一些资料,XEvent的处理还是得重写QApplication的x11EventFilter函数
离线ieasylive

只看该作者 2楼 发表于: 2009-09-18
由于比较特殊,所以得用XSendEvent实现,想了解一下可不可以在子线程中接收XEvent?
离线dbzhang800

只看该作者 1楼 发表于: 2009-09-18
signals and slots!!
快速回复
限100 字节
 
上一个 下一个