Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Qt MQTT and while loop

Qt MQTT and while loop

Scheduled Pinned Locked Moved Solved General and Desktop
3 Posts 2 Posters 457 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • G Offline
    G Offline
    Guigiidre
    wrote on last edited by
    #1

    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

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      3
      • G Offline
        G Offline
        Guigiidre
        wrote on last edited by
        #3

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

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved