QT中的永久线程 [英] Permanent Threads in QT

查看:102
本文介绍了QT中的永久线程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是QT的新手...我还没来得及.我有一个带有3个标签的GUI,必须每10秒从3个不同的线程(永久性,调用3个不同的方法)进行更新.做到这一点的最佳方法是什么?预先感谢!

I'm new in QT...and i haven't time. I have a GUI with 3 labels that must be update from 3 different threads (permanents, that invoke 3 different methods) every 10 secs. What is the best way to make this? thanks in advance!

推荐答案

您应使用Qt 现在您只需要将此插槽连接到某些信号

And now You just need to connect this slot to some signal

class SomeThreadClass : QObject
{
Q_OBJECT
...
signals:
void labelUpdateRequest(QString withwhat);
};

在窗口的构造函数中

QWindow::QWindow(QWidget* parent) : QMainWindow(parent)
{
m_someThread = new SomeThreadClass();

//in old style Qt, now there's a mechanism for compile time check
//don't use pointers, you need to free them at some point and there might be many receivers
//that might use it 
connect(m_someThread, SIGNAL(labelUpdateRequest(QString)), this, SLOT(updateLabel(QString));
...
}

现在就在线程中的某个时刻:

now just at some point in the thread:

SomeThreadClass::someMethod()
{
//do something...
emit labelUpdateRequest(QString("Welcome to cartmanland!"));
//this will be received by all classes that call connect to this class.
}

希望有帮助:)

这篇关于QT中的永久线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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