[Solved] Permanent Threads in QT


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 🙂

0

solved Permanent Threads in QT