Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Qt MQTT and while loop



  • Hello!

    I' m using Qt Mqtt on Windows and I have troubles to publish a message in a while loop.

    For example:

        int n=0;
        while (n<5){
            qDebug()<<Q_FUNC_INFO<<endl;
            mClient->publish(QMqttTopicName(topic.toString()),data.toByteArray());
            Sleep(20000);
            n++;
        }
    

    with this code, publishments are all made at the same time and after exiting the while loop. But this is not what I want, I would like a publishement every 2 seconds during the execution of the while loop.

    Thank you in advance for telling me how to achieve this.

    guigiidre


  • Lifetime Qt Champion

    Hi and welcome to devnet,

    You are blocking the event loop which explains what is going on.

    A QTimer would likely be a cleaner approach. Let it timeout 5 times and then stop it.



  • Great, it works, Qtimer was the solution.
    Thank you!


Log in to reply