Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. How to display data from MQTT server in real-time?
Forum Updated to NodeBB v4.3 + New Features

How to display data from MQTT server in real-time?

Scheduled Pinned Locked Moved Solved QML and Qt Quick
mqtt
23 Posts 4 Posters 10.4k 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
    GrecKo
    Qt Champions 2018
    wrote on 26 Oct 2017, 17:53 last edited by GrecKo
    #21

    That looks like a job for a Q_PROPERTY and not for a signal.

    Store the message payload in a member variable of your class, let's say QString m_message.

    In your header you'd use a Q_PROPERTY(QString message READ message NOTIFY messageChanged)

    Implementation of the getter:
    QString message() { return m_message; }

    Add a signal void messageChanged(); in your header.
    And modify your exisiting onMQTT_Received to be :

    void MyDataClass::onMQTT_Received(const QMQTT::Message &message)
    {
        m_message = message.payload();
        emit messageChanged();
    }
    

    Then in your QML you can just do:

    Label {
        text: myobj.message
    }
    

    Nice and clean declarative code, no need to deal with Connections and signals in QML.

    A 1 Reply Last reply 29 Oct 2017, 18:06
    3
    • G GrecKo
      26 Oct 2017, 17:53

      That looks like a job for a Q_PROPERTY and not for a signal.

      Store the message payload in a member variable of your class, let's say QString m_message.

      In your header you'd use a Q_PROPERTY(QString message READ message NOTIFY messageChanged)

      Implementation of the getter:
      QString message() { return m_message; }

      Add a signal void messageChanged(); in your header.
      And modify your exisiting onMQTT_Received to be :

      void MyDataClass::onMQTT_Received(const QMQTT::Message &message)
      {
          m_message = message.payload();
          emit messageChanged();
      }
      

      Then in your QML you can just do:

      Label {
          text: myobj.message
      }
      

      Nice and clean declarative code, no need to deal with Connections and signals in QML.

      A Offline
      A Offline
      AntonioQt
      wrote on 29 Oct 2017, 18:06 last edited by
      #22

      Awesome @GrecKo!
      Thank you so much. It worked for me.

      This was indeed easier. I tried it earlier but couldn't succeed, but the way explained was very explanatory.
      So now I understood two exposing data to QML.

      Thank you everyone!

      1 Reply Last reply
      0
      • S Offline
        S Offline
        SGaist
        Lifetime Qt Champion
        wrote on 29 Oct 2017, 20:14 last edited by
        #23

        On a side note, the getter should be const, it doesn't modify anything in the class instance.

        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
        0

        21/23

        26 Oct 2017, 17:53

        • Login

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