我是QT的新手......我没有时间。我有一个带有3个标签的GUI,必须每隔10秒从3个不同的线程(永久物,调用3种不同的方法)进行更新。最好的方法是什么?提前谢谢!
答案 0 :(得分:1)
您应该使用Qt signaling机制。
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.
}
希望有所帮助:)