线程& Qt应用程序中的事件循环 [英] Threads & Event loop in the Qt application

查看:441
本文介绍了线程& Qt应用程序中的事件循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何人都可以解释Threads和Event循环中的差异,如何在QT应用程序中使用它。



在哪里可以使用移动线程并完成线程类?

解决方案

每个线程处理自己的事件循环,通常不需要担心这一点 - 它会为你照顾,除非你有一个特定的原因意味着被遗弃。



QThread是一个由Qt提供的类,用于控制线程的操作。将对象放入该线程的方法是使用moveToThread()函数。



您不应该继承QThread类以在线程中运行一些代码(使用moveToThread函数),继承QThread类的唯一原因是如果要更改线程控件的行为。



以下是获取的基本步骤一个线程内运行的对象:

  MyObj * myObj = new MyObj(0); // 0 =没有父对象如果你的对象继承QObject 
QThread * thread = new QThread;
myObj-> moveToThread(thread);
QObject :: connect(thread,SIGNAL(started()),myObj,SLOT(run()));
thread-> start();

一旦你调用start()线程将启动并发出启动信号,你的对象将会收到它并且在其插槽/函数run()中处理它。



注意:当你的对象中的函数/插槽run()结束时,你的线程不会结束(所以你做不需要做一个永远循环)。线程只有当您告诉它退出(或销毁它)时才会停止,这意味着您的线程可以空闲,直到它收到信号或事件 - 这是事件循环进入的地方 - 传入事件由内部事件循环处理QThread类。



注意:此代码也是一个代码片段 - 它不处理线程的关闭,还有其他模板位的代码,你可以使用这个。



修改



所以事件由事件处理队列(类似于鼠标点击事件的所有基本类型QEvent) - 系统使用更多的一些事件可能触发信号(例如onClicked)。信号和插槽是一种不同的机制,用户可以在其中使用connect()函数在插槽中处理这些机制。这是一个更好的解释,然后我可以想出:
看到这里


Can anyone explain difference in the Threads and Event loop, and how can I use this in the QT application.

where can I use move to the thread and complete thread class.?

解决方案

Each thread processes its own event loop, you normally do not need to worry about this - its taken care of for you and unless you have a specific reason to its meant to be left alone.

QThread is a class provided by Qt for you to use to control the operation of a thread. The method of "putting" object into that thread is to use the moveToThread() function.

You should not inherit the QThread class in order to run some code inside a thread (use the moveToThread function), the only reason to inherit the QThread class is if you want to change the behaviour of the thread control.

Below are the basic steps to get an object running inside a thread:

MyObj *myObj = new MyObj(0); // 0 = no parent if your object inherits QObject
QThread* thread = new QThread;
myObj->moveToThread(thread);
QObject::connect(thread, SIGNAL(started()), myObj, SLOT(run()));
thread->start();

Once you call start() the thread will start and emit the started signal, your object will receive it and process it in its slot/function run().

Note: your thread does not end when the function/slot run() inside your object ends (so you do not need to do a "forever" loop). The thread only stops when you tell it to quit (or destroy it), this means your thread can be idle until it receives a signal or event - this is where the event loop comes in - incoming events are handled by the event loop within the QThread class.

Note: also this code is a snippet - it does not deal with the shutting down of the thread, there are other "template" bits of code that you can use for this.

Edit

So events are handled by the event queue (things like mouse click events all of base type QEvent) - used more by the system where some events may trigger signals (onClicked for example). Signals and slots are a different mechanism that is used more by the user where you handle these in your slots using the connect() function. Here is a better explanation then I could come up with: see here

这篇关于线程& Qt应用程序中的事件循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆