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 1016(素数环 深搜)
题意是说对一个长度为 n 的数环进行排列,使得相邻两数的和为素数,按从小到大的顺序依次输出. 因为是环,所以总能调整成以 1 为序列首输出.用深度优先搜索的方法即可.在判断素数时由于 n 小于 20, ...
- springBoot中实现自定义属性配置、实现异步调用、多环境配置
springBoot中其他相关: 1:springBoot中自定义参数: 1-1.自定义属性配置: 在application.properties中除了可以修改默认配置,我们还可以在这配置自定义的属性 ...
- zookeeper的安装与使用
zookeeper的安装与使用: Zookeeper简介 1.可以作为集群的管理工具使用. 2.可以集中管理配置文件. Zookeeper是一个高效的分布式协调服务,可以提供配置信息管理.命名.分布式 ...
- Android手机有的不显示Toast
解决办法一: 在手机中把该app的通知打开 可以直接设置通知权限:<uses-permission android:name="android.permission.RECEIVE_B ...
- electron-vue:Vue.js 开发 Electron 桌面应用
相信很多同学都知道 Electron 可以帮助开发人员使用前端技术开发桌面客户端应用,今天介绍的 electron-vue 框架是一套基于 Vue.js 开发 Electron 桌面应用的脚手架,该项 ...
- seleniums私房菜系列一 ---- selenium简介
一.Selenium是什么? Selenium是ThroughtWorks公司一个强大的开源Web功能测试工具系列,本系列现在主要包括以下4款: 1.Selenium Core:支持DHTML的测试案 ...
- Django之CRM项目Day3-客户展示及分页
1.展示客户 模板的查找顺序: 先找全局的templates--> 按照app的注册顺序找templates中的文件 使用admin添加数据: 创建超级用户 python manage.py ...
- vue运行说明
1.cd 到demo里面 如:cd vuedemo(项目名) 2.安装依赖: npm install 3.运行项目 npm run dev
- 5.22 css和基本选择器
1,css的三种引入方式 1,行内样式 2,内接样式 3,外接样式:链接式和导入式. HTML的缺陷: 不能够适应多种设备 要求浏览器必须智能化足够庞大 数据和显示没有分开 功能不够强大 CSS 优点 ...
- centos7图形化界面安装KVM虚拟机
一.检查kvm和libvirt 是否安装 查看内核模块中是否含有kvm lsmod | grep kvm 查看cpu是否支持虚拟化 egrep -c '(vmx|svm)' /proc/cpuinfo ...