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:

#include<QObject>
#include<iostream>
class Counter: public Qobject
{
    Q_OBJECT
    public:
    Counter(){m_value = 0;}
    int value() const { return m_value;}
    public slots:
    void setValue(int value);
    signals:
    void valueChanged(int newvalue);
    private:
    int m_value;
};

void Counter::setValue(int value)
{
    if(value != m_value){m_value = value;emit valueChanged(value+2);}
}

int main(int argc, char ** argv)
{
Counter a,b;
connect(&a,SINGAL(valueChanged(int)),&b,SINGAL(valueChanged(int)));
connect(&b,SINGAL(valueChanged(int)),&b,SLOT(setValue(int)));
a.setValue(33);
std::cout << "value of a :" << a.value() << std::endl;
std::cout << "value of b :" << b.value() << std::endl;
std::cin.get();
}

to compile it: qmake;make

时间: 2024-08-24 16:23:41

event & signals & threads的相关文章

Threads Events QObjects

Events and the event loop Being an event-driven toolkit, events and event delivery play a central role in Qt architecture. In this article we'll not give a comprehensive coverage about this topic; we'll instead focus on some thread-related key concep

C#学习日记24----事件(event)

事件为类和类的实例提供了向外界发送通知的能力,实现了对象与对象之间的通信,如果定义了一个事件成员,表示该类型具有 1.能够在事件中注册方法 (+=操作符实现). 2.能够在事件中注销方法(-=操作符实现). 3.当事件被触发时注册的方法会被通知(事件内部维护了一个注册方法列表).委托(Delegate)是事件(event)的载体,要定义事件就的要有委托.  有关委托的内容请点击 委托(De... www.mafengwo.cn/event/event.php?iid=4971258www.maf

android.os.NetworkOnMainThreadException 异常处理

当我试图在UI线程(MainActivity)连接网络的时候,运行时抛出异常droid.os.NetworkOnMainThreadException 安卓的官方文档说 The exception that is thrown when an application attempts to perform a networking operation on its main thread. This is only thrown for applications targeting the Ho

hazelcast Document-Config 文档翻译

配置 Hazelcast的使用可以通过XML配置和编程来实现,或者是两者共同配合使用来完成其功能: 1-配置声明 如果你创建Hazelcast实例的时候,不传递参数给Hazelcast的话(即:newHazelcastInstance(null))或者使用空的工厂方法(Hazelcast.newHazelcastInstance)来创建,Hazelcast 将会自己自动的检查两个地方来寻找Hazelcast的配置文件: §   系统属性:Hazelcast 首先会检查是否"hazelcast.c

SDL Guide 中文译版

SDL即Simple DirectMedia Layer,类似DirectX,是完整的游戏.多媒体开发包,但不同的是它跨越几乎所有的平台,有各种语言的接口,多种语言的文档,而这一切都是广大志愿者完成的.目前扩展部分还没有正式的文档,以下为核心部分文档的向导部分. 序言 关于SDL SDL为方便制作能跨跃Linux.BSD.MacOS.Win32和BeOS平台,使用本地高性能媒体接口,并且让您可以只需使用一份源代码级API而设计.SDL是相当低层的API,但使用它可以让你以极大的灵活性写出完全跨平

android.os.NetworkOnMainThreadException

android.os.NetworkOnMainThreadException 一.出现原因 我把网络读取数据的操作写进了主线程 看名字就应该知道,是网络请求在MainThread中产生的异常 二.产生原因 官网解释 Class Overview The exception that is thrown when an application attempts to perform a networking operation on its main thread. This is only t

WinDbg Command-Line Options

http://msdn.microsoft.com/zh-cn/library/ff561306(v=vs.85).aspx The WinDbg command line uses the following syntax: windbg [ -server ServerTransport | -remote ClientTransport ] [-lsrcpath ] [ -premote SmartClientTransport ] [-?] [-ee {masm|c++}] [-clin

我写的一个 Qt 显示图片的控件

Qt 中没有专门显示图片的控件,通常我们会使用QLabel来显示图片.但是QLabel 显示图片的能力还是有点弱.比如不支持图像的缩放一类的功能,使用起来不是很方便.因此我就自己写了个简单的类. 我这个类支持三种图像显示模式,我分别称之为:FIXED_SIZE, CENTRED,AUTO_ZOOM, AUTO_SIZE. FIXED_SIZE 模式下,显示的图像大小等于图像尺寸乘以缩放因子,如果控件的尺寸小于这个大小则多出的部分被裁切掉. FIX_SIZE_CENTRED模式与FIXED_SIZ

Programming with gtkmm 3

https://developer.gnome.org/gtkmm-tutorial/unstable/index.html.zh_CN 1. 序言 1.1. 本书 1.2. gtkmm 2. 安装 2.1. 依赖关系 2.2. Unix 和 Linux 2.3. Microsoft Windows 3. 基础 3.1. 简单的例子 3.2. 头文件和链接 3.3. 组件 3.4. 信号 3.5. Glib::ustring 3.6. 中间类型 3.7. 混合使用 C 和 C++ API 3.8