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!
Asked
Active
Viewed 459 times
-4
-
Use Qt signals to update label texts. – hyde Mar 20 '14 at 20:34
-
ehm...yes...of course. – mav Mar 20 '14 at 21:12
1 Answers
1
You should use Qt signaling mechanism.
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)
...
};
And now You just need to connect this slot to some signal
class SomeThreadClass : QObject
{
Q_OBJECT
...
signals:
void labelUpdateRequest(QString withwhat);
};
in the constructor of the window
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.
}
Hope that helps :)

Maciej Lichoń
- 157
- 5