QT中的永久线程



我是QT新手…我没时间了。我有一个带有3个标签的GUI,必须每10秒从3个不同的线程(永久性的,调用3个不同的方法)更新。做这个最好的方法是什么?提前感谢!

应该使用Qt信号机制。

class QWindow : QMainWindow
{
//this macro is important for QMake to let the meta programming mechanism know 
//this class uses Qt signalling
Q_OBJECT 
slots:
void updateLabel(QString withWhat)
...
};

现在你只需要把这个插槽连接到一些信号

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));
...
}

现在正好在线程的某个点:

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

希望有帮助

相关内容

  • 没有找到相关文章

最新更新