event & signals & threads
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的更多相关文章
- Threads Events QObjects
Events and the event loop Being an event-driven toolkit, events and event delivery play a central ro ...
- Working Experience - How to handle the destroyed event of UserControl
正文 问题: UserControl 如何在父窗体(程序)关闭时, 释放一些需要手动释放的资源 方法: 使用 Control.FindForm() 获取父窗体, 从而得到父窗体的 Closing/Cl ...
- OSG Qt Widget加载三维模型
graphicswindowqt.h #ifndef GRAPHICSWINDOWQT_H #define GRAPHICSWINDOWQT_H #include <QGLWidget> ...
- 基于Lease分布式系统重试服务选举
/** * Copyright (c) 2015, www.cubbery.com. All rights reserved. */ package com.cubbery.event.retry; ...
- Android之NetworkOnMainThreadException异常
看名字就应该知道,是网络请求在MainThread中产生的异常 先来看一下官网的解释: Class Overview The exception that is thrown when an appl ...
- memcached(二)事件模型源码分析
memcachedd事件模型 在memcachedd中,作者为了专注于缓存的设计,使用了libevent来开发事件模型.memcachedd的时间模型同nginx的类似,拥有一个主进行(master) ...
- Android开发新手第一要素
很多新手开发程序的时候,或者将原来跑在Android 2.X上的程序迁移到Android 3.x以上的时候经常会莫名其妙的出现崩溃(Crash).从我的经验来看,这里可能有很多原因,但是最重要也是最常 ...
- Programming with gtkmm 3
https://developer.gnome.org/gtkmm-tutorial/unstable/index.html.zh_CN 1. 序言 1.1. 本书 1.2. gtkmm 2. 安 ...
- HBase HMaster Architecture - HBase Master架构
HBase architecture follows the traditional master slave model where you have a master which takes de ...
随机推荐
- HDU 1046(最短路径 **)
题意是要在一个矩形点阵中求能从一点出发遍历所有点再回到起始点的最短路径长度. 不需要用到搜索什么的,可以走一个“梳子型”即可完成最短路径,而情况可以被分成如下两种: 一.矩形的长或宽中有偶数,则可以走 ...
- 解决svn检出后不显示图标的问题
解决svn检出后不显示图标的问题: 此文经过个人验证,可以解决TortoiseSVN图标显示异常问题: 问题出现原因:Windows Explorer Shell 支持 Overlay Icon 最多 ...
- flask请求异步执行(转载)
Flask默认是不支持非阻塞IO的,表现为: 当 请求1未完成之前,请求2是需要等待处理状态,效率非常低. 在flask中非阻塞实现可以由2种: 启用flask多线程机制 # Flask from f ...
- 使用Nginx在windows和linux上搭建集群
Nginx Nginx (engine x) 是一个高性能的HTTP和反向代理服务器,也是一个IMAP/POP3/SMTP服务器 特点:反向代理 负载均衡 动静分离… 反向代理(Reverse Pro ...
- 【bzoj 3110】[Zjoi2013]K大数查询
Description 有N个位置,M个操作.操作有两种,每次操作如果是1 a b c的形式表示在第a个位置到第b个位置,每个位置加入一个数c.如果是2 a b c形式,表示询问从第a个位置到第b个位 ...
- 测试四则运算2:Right-BICEP
n6个值得测试的具体部位,他们能够提高你的测试技巧 nRight-结果是否正确? nB-是否所有的边界条件都是正确的? nI-能查一下反向关联吗 nC-能用其他手段交叉检查一下结果吗? nE-你是否可 ...
- 唯一约束(UNIQUE_KEY)
唯一约束可以保证记录的唯一性 唯一约束的字段可以为空值(NULL) 每张数据表可以存在多个唯一约束(主键只有一个) mysql> CREATE TABLE tb7( -> id SMALL ...
- pyaudio
安装: 下载whl文件:https://github.com/intxcc/pyaudio_portaudio/releases 切换到whl文件目录,直接用pip安装 pip instal ...
- B - 签到题
计算机系统中使用的UTC时间基于原子钟,这种计时方式同“地球自转一周是24小时”的计时方式有微小的偏差.为了弥补这种偏差,我们偶尔需要增加一个“闰秒”. 最近的一次闰秒增加发生在UTC时间2016年的 ...
- 5.22 HTML 列表标签和表单标签
1,ul无序列表 标签 ul:unordered list ,就是无序列表的意思. li: listitem 列表项的意思.无序列表的每一项都是<li>. <!DOCTYPE h ...