Congratulations to our 2022 Qt Champions!

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){

    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.


  • 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