I have a Qt project which has multiple UIs. I would like to check the wifi signal strength every 20 seconds and update which ever UI form I am in with the signal strength value. I have created a myThread class with base class QThread and reimplemented run.Here I have a timer that has a timeout of 20 seconds. The thread is started in main.cpp class.
void Thread_UpdateWifiSignal::run()
{
QTimer timer;
connect(&timer, SIGNAL(timeout()), this, SLOT(timerHit()), Qt::DirectConnection);
timer.setInterval(20000);
timer.start(); // puts one event in the threads event queue
exec();
qDebug() << "stopping timer";
timer.stop();
}
In the timehit slot I have written a code to get the wifi signal strength and then a SIGNAL is emitted with the updated value of the wifi signal strength.
void Thread_UpdateWifiSignal::timerHit()
{
QMutex mutex;
mutex.lock();
int SignalQuality = CheckWifiNetwork();
emit this->UpdatedSignalQuality(SignalQuality);
mutex.unlock();
}
int Thread_UpdateWifiSignal::CheckWifiNetwork()
{
//Code to get wifi signal strength
}
I have a short cut panel in all the UI, which has a label that displays the wifi icon. I would like to connect the signal emitted by the thread to the slot in UI class that updates the icon wifi sticks with respect to wifi signal strength.
I see that the thread is running, but though I instantiate the myThread class in UI form class, to connect the signal emitted to the slot, I notice the signal emitted is not connected to the slot.
How do I go about it?
QThread
and using slots is asking for trouble.It is important to remember that a QThread object usually lives in the thread where it was created, not in the thread that it manages. This oft-overlooked detail means that a QThread's slots will be executed in the context of its home thread, not in the context of the thread it is managing. For this reason, implementing new slots in a QThread subclass is error-prone and discouraged.
– thuga