0
votes

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?

1

1 Answers

0
votes

At the moment, the signal is posted to your thread's event loop which results in the execution of your handler slot in your subclassed thread instead of your "main thread" (which manages GUI components), so no gui changes appear.

You have to connect the signal UpdatedSignalQuality from your subclassed thread to the handler slot with a third parameter as Qt::QueuedConnection instead of Qt::DirectConnection then the signal will be posted in your main thread's event loop.

More details differences between QueuedConnection and DirectConection