The Event System
http://doc.qt.io/qt-4.8/eventsandfilters.html

Each thread can have its own event loop. The initial thread starts its event loops using QCoreApplication::exec(); 
other threads can start an event loop using QThread::exec().

Qt signals (QueuedConnection and DirectConnection) -- answer from Jacob Robbins
http://stackoverflow.com/questions/15051553/qt-signals-queuedconnection-and-directconnection

You won't see much of a difference unless you're working with objects having different thread affinities.
Let's say you have QObjects A and B and they're both attached to different threads. A has a signal called somethingChanged() and B has a slot called handleChange(). If you use a direct connection:
  connect( A, SIGNAL(somethingChanged()), B, SLOT(handleChange()), Qt::DirectConnection );
the method handleChange() will actually run in the A's thread. Basically, it's as if emitting the signal calls the slot method "directly".
If B::handleChange() isn't thread-safe, this can cause some (difficult to locate) bugs. At the very least, you're missing out on the benefits of the extra thread.

If you change the connection method to Qt::QueuedConnection (or, in this case, let Qt decide which method to use), Assuming B's thread is running an event loop, emitting the signal will post an event to B's event loop. 
The event loop queues the event, and eventually invokes the slot method whenever control returns to it (it being the event loop).
This makes it pretty easy to deal with communication between/among threads in Qt (again, assuming your threads are running their own local event loops).
You don't have to worry about locks, etc. because the event loop serializes the slot invocations.

Note: If you don't know how to change a QObject's thread affinity, look into QObject::moveToThread.

It does make a difference if you specify a queued connection - even for two objects on the same thread.
The event is still posted to the thread's event loop. So, the method call is still asynchronous, meaning it can be delayed in unpredictable ways
(depending on any other events the loop may need to process).
However, if you don't specify a connection method, the direct method is automatically used for connections between objects on the same thread (at least it is in Qt 4.8).

Threads and QObjects
http://doc.qt.io/qt-4.8/threads-qobject.html

Signals & Slots
http://doc.qt.io/qt-4.8/signalsandslots.html

(DirectConnection)Execution of the code following the emit statement will occur once all slots have returned.
The situation is slightly different when using queued connections; in such a case, the code following the emit
keyword will continue immediately, and the slots will be executed later.

example:

// file :signal.h
#include <QObject>
class Counter: public QObject
{
Q_OBJECT
public:
Counter(){m_value=;}
int value() const{return m_value;}
public slots:
void setValue(int value);
signals:
void valueChanged(int value);
private:
int m_value;
};
// file : signal.cpp
#include "signal.h"
#include <iostream>
void Counter::setValue(int value)
{
if(value!=m_value)
{
m_value = value;
emit valueChanged(value);
}
} int main(int argc, char **argv)
{
Counter a,b;
QObject::connect(&a, SIGNAL(valueChanged(int)),&b,SIGNAL(valueChanged(int)));
QObject::connect(&b, SIGNAL(valueChanged(int)),&b,SLOT(setValue(int)));
a.setValue();
std::cout << "value of a :" << a.value() << std::endl;
std::cout << "value of b :" << b.value() << std::endl;
std::cin.get();
}
~

to compile them: qmake -project; qmake; make

note: the declaration of class Counter should put in header file, for MOC will only anlyse header files and create corresponding moc file

event & signals & threads的更多相关文章

  1. Threads Events QObjects

    Events and the event loop Being an event-driven toolkit, events and event delivery play a central ro ...

  2. Working Experience - How to handle the destroyed event of UserControl

    正文 问题: UserControl 如何在父窗体(程序)关闭时, 释放一些需要手动释放的资源 方法: 使用 Control.FindForm() 获取父窗体, 从而得到父窗体的 Closing/Cl ...

  3. OSG Qt Widget加载三维模型

    graphicswindowqt.h #ifndef GRAPHICSWINDOWQT_H #define GRAPHICSWINDOWQT_H #include <QGLWidget> ...

  4. 基于Lease分布式系统重试服务选举

    /** * Copyright (c) 2015, www.cubbery.com. All rights reserved. */ package com.cubbery.event.retry; ...

  5. Android之NetworkOnMainThreadException异常

    看名字就应该知道,是网络请求在MainThread中产生的异常 先来看一下官网的解释: Class Overview The exception that is thrown when an appl ...

  6. memcached(二)事件模型源码分析

    memcachedd事件模型 在memcachedd中,作者为了专注于缓存的设计,使用了libevent来开发事件模型.memcachedd的时间模型同nginx的类似,拥有一个主进行(master) ...

  7. Android开发新手第一要素

    很多新手开发程序的时候,或者将原来跑在Android 2.X上的程序迁移到Android 3.x以上的时候经常会莫名其妙的出现崩溃(Crash).从我的经验来看,这里可能有很多原因,但是最重要也是最常 ...

  8. Programming with gtkmm 3

      https://developer.gnome.org/gtkmm-tutorial/unstable/index.html.zh_CN 1. 序言 1.1. 本书 1.2. gtkmm 2. 安 ...

  9. HBase HMaster Architecture - HBase Master架构

    HBase architecture follows the traditional master slave model where you have a master which takes de ...

随机推荐

  1. MySQL忘记root密码的解决办法

    # 1.停掉MySQL进程 [root@standby ~]# /etc/init.d/mysqld stop Shutting down MySQL... SUCCESS! [root@standb ...

  2. angularjs路由path方式实现原理探究

    angularjs路由 https://angular.io/guide/router 通过URL解释, 来定位客户端生成的浏览器端视图. 你可绑定路由到页面的链接上, 当用户点击链接, 可以浏览到相 ...

  3. 二十三、Linux 进程与信号---进程链和进程扇、守护进程和孤儿进程以及僵尸进程

    23.1 进程链和进程扇 23.1.1 概念 进程链:一个父进程构建出一个子进程,子进程再构建出子子进程,子子进程构建出子子子进程.... 这种就为进程链 进程扇:一个父进程构建出多个子进程,子进程都 ...

  4. 课堂测试——jsp登录界面设计

    实现结果:在login.jsp页面提交用户名和密码(可以验证是否为空),点击登录跳转到loginResult.jsp页面进行验证并显示结果 JSP + JDBC + MySQL login.jsp 设 ...

  5. Tomcat清理日志文件无法立即释放磁盘空间

    1 自己删除了Tomcat的日志文件,但是依然显示磁盘百分百占用 进入Tomcat目录显示日志已经删除 查询磁盘空间依旧百分百占用 2 自己杀死Tomcat进程然后重启,成功释放空间 3 原因,通过网 ...

  6. mysql 原理 ~ 索引通说

    简介: 来说说索引吧目的:为了加快数据库的数据查找速度,索引应用而生基础知识基本定义  1 遍历 所谓遍历二叉树,就是按一定的规则和顺序走遍二叉树的所有结点,使每一个结点都被访问一次,而且只被访问一次 ...

  7. Setup Objective UI with UMG

    创建UI蓝图控件 拖入一个文本框 新建一个Actor,继承自FPSHUD 创建控件,并显示到界面上 新建一个Actor,继承FPSGameMode 将属性里的HUD更改为之前创建的 在世界设置中,将G ...

  8. 微信小程序滚动Tab选项卡:左右可滑动切换

    最终效果如上.问题: 1.tab标题总共8个,所以一屏无法全部显示. 2.tab内容区左右滑动切换时,tab标题随即做标记(active). 3.当active的标题不在当前屏显示时,要使其能显示到当 ...

  9. SQLServer语法常用总结

    1. 有时候查看SQL的时候表名或者字段名需要加[],这是因为有时候你的表名或者字段名正好与sqlserver的保留字段重了 比如:有一个user表,直接select会报错 select * from ...

  10. launch 文件的写法

    1. launch文件的写法 ❀标签          ☺<node> 启动一个节点          ☺ <param> 设置参数服务器的参数          ☺ < ...