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

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

问题描述

谁能解释线程和事件循环的区别,以及如何在 QT 应用程序中使用它.

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 是 Qt 提供的一个类,用于控制线程的操作.将对象放入"该线程的方法是使用 moveToThread() 函数.

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.

您不应该为了在线程内运行某些代码而继承 QThread 类(使用 moveToThread 函数),继承 QThread 类的唯一原因是如果您想更改线程控件的行为.

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();

一旦您调用 start(),线程将启动并发出启动信号,您的对象将接收它并在其槽/函数 run() 中处理它.

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().

注意:当对象内的函数/槽 run() 结束时,您的线程不会结束(因此您不需要执行永远"循环).线程仅在您告诉它退出(或销毁它)时才停止,这意味着您的线程可以处于空闲状态,直到它接收到信号或事件 - 这是事件循环进入的地方 - 传入事件由事件循环内的事件循环处理QThread 类.

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.

编辑

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

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天全站免登陆