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?
QtWS25 Last Chance

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

Scheduled Pinned Locked Moved Solved QML and Qt Quick
mqtt
23 Posts 4 Posters 9.8k Views
  • 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.
  • S SGaist
    12 Oct 2017, 19:33

    Where's your QMQTT come from ?

    A Offline
    A Offline
    AntonioQt
    wrote on 13 Oct 2017, 06:50 last edited by
    #12

    @SGaist
    I got this library from here:
    https://github.com/emqtt/qmqtt/tree/master/src/mqtt

    I am using this library to receive data from cloud. This library is fulfilling the requirement - as I am getting the output in Qt as I commented above. Now I just wanted this data(output) in QML.

    1 Reply Last reply
    0
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 13 Oct 2017, 22:03 last edited by
      #13

      I'd suggest taking a look at Qt's own module for that (it's all new), you can find it here and it might make things easier to integrate with QML.

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

      A 1 Reply Last reply 16 Oct 2017, 16:43
      0
      • A AntonioQt
        12 Oct 2017, 18:10

        Sorry for late response, I was away.
        As @SGaist asked below are my code snippet:

         QObject::connect(client, SIGNAL(received(const QMQTT::Message &)), this, SLOT(onMQTT_Received(const QMQTT::Message &)));
        

        I copied from my second comment above.
        I hope I understand your question correctly.
        How can I catch this signal in QML?

        E Offline
        E Offline
        Eeli K
        wrote on 14 Oct 2017, 11:46 last edited by
        #14

        @AntonioQt "What is the signature of your signal ?" like @SGaist asked - i.e. how it is declared in C++ header? QML sees the argument by the name which is in C++ declaration, not call time name. "onMySignal:console.log(str)" should work if you have

        signals: void mySignal(QString str);
        

        but not if you have e.g.

        signals: void mySignal(QString someString); //you must use "someString" in QML
        
        A 1 Reply Last reply 16 Oct 2017, 16:49
        0
        • S SGaist
          13 Oct 2017, 22:03

          I'd suggest taking a look at Qt's own module for that (it's all new), you can find it here and it might make things easier to integrate with QML.

          A Offline
          A Offline
          AntonioQt
          wrote on 16 Oct 2017, 16:43 last edited by
          #15

          Thinking of trying this @SGaist . But not getting exact link to download the library.

          1 Reply Last reply
          1
          • E Eeli K
            14 Oct 2017, 11:46

            @AntonioQt "What is the signature of your signal ?" like @SGaist asked - i.e. how it is declared in C++ header? QML sees the argument by the name which is in C++ declaration, not call time name. "onMySignal:console.log(str)" should work if you have

            signals: void mySignal(QString str);
            

            but not if you have e.g.

            signals: void mySignal(QString someString); //you must use "someString" in QML
            
            A Offline
            A Offline
            AntonioQt
            wrote on 16 Oct 2017, 16:49 last edited by
            #16

            @Eeli-K Following the same. But not getting the output. I m trying now the latest library as @SGaist suggested.

            E 1 Reply Last reply 16 Oct 2017, 19:25
            1
            • A AntonioQt
              16 Oct 2017, 16:49

              @Eeli-K Following the same. But not getting the output. I m trying now the latest library as @SGaist suggested.

              E Offline
              E Offline
              Eeli K
              wrote on 16 Oct 2017, 19:25 last edited by Eeli K
              #17

              @AntonioQt You've got:

              //MyDataClass.cpp
              QString MyDataClass::onMQTT_Received(const QMQTT::Message &message)
              {
                  QString str;
                  str = message.payload();
                  qDebug() << str;
                  emit mySignal(str);
                  return str;
              }
              

              and if the str is seen in the debug output then the problem isn't in the mqtt library, it's in your C++ or QML code. So, provided that you have

              signals: void mySignal(QString str);
              

              in your class header, MyDataClass::onMQTT_Received as seen above,

              view.rootContext()->setContextProperty("myobj", &data);
              

              in main.cpp,

              Connections{
              target: myobj
              onMySignal: {console.log("String received! " + str)}
              }
              

              and you see the string in C++ debug output you should also see the "String received!" message plus the string in QML debug output. If not, there's something weird going on. You can also try adding

              Component.onCompleted: {console.log(myobj)}
              

              to an item in your main.qml to see if the C++ object is really recognized.

              EDIT: If you test something with debug output, please paste the real output here. Sometimes it gives an answer you don't notice and we can't tell without seeing.

              A 1 Reply Last reply 20 Oct 2017, 15:38
              1
              • S Offline
                S Offline
                SGaist
                Lifetime Qt Champion
                wrote on 16 Oct 2017, 19:28 last edited by
                #18

                Looks like the repository is not yet on code.qt.io, but you can get it here.

                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
                2
                • E Eeli K
                  16 Oct 2017, 19:25

                  @AntonioQt You've got:

                  //MyDataClass.cpp
                  QString MyDataClass::onMQTT_Received(const QMQTT::Message &message)
                  {
                      QString str;
                      str = message.payload();
                      qDebug() << str;
                      emit mySignal(str);
                      return str;
                  }
                  

                  and if the str is seen in the debug output then the problem isn't in the mqtt library, it's in your C++ or QML code. So, provided that you have

                  signals: void mySignal(QString str);
                  

                  in your class header, MyDataClass::onMQTT_Received as seen above,

                  view.rootContext()->setContextProperty("myobj", &data);
                  

                  in main.cpp,

                  Connections{
                  target: myobj
                  onMySignal: {console.log("String received! " + str)}
                  }
                  

                  and you see the string in C++ debug output you should also see the "String received!" message plus the string in QML debug output. If not, there's something weird going on. You can also try adding

                  Component.onCompleted: {console.log(myobj)}
                  

                  to an item in your main.qml to see if the C++ object is really recognized.

                  EDIT: If you test something with debug output, please paste the real output here. Sometimes it gives an answer you don't notice and we can't tell without seeing.

                  A Offline
                  A Offline
                  AntonioQt
                  wrote on 20 Oct 2017, 15:38 last edited by
                  #19

                  @Eeli-K This worked! :D

                  Thank you so much.
                  There were two issues with the code, first was resolved by the snippet you provided above.
                  The second I observed while copying the qDebug output for you guys. There was an error message :

                  file:qrc:/main.qml: No such file or directory
                  

                  Means that, no signals were being passed into "main.qml". Then I searched for the resolution and the solution was, modifying the following code in "main.cpp":

                  view.setSource (QUrl("qrc:///main.qml"));
                  

                  I did not dig much about how/why it happened?

                  The next thing I am looking for -to use the str (variable) in Qml. I just want to use this (or any other single) variable with Qml widget. Do we have any quickest way to achieve this?
                  Once again thank you very much @Eeli-K and @SGaist for your efforts and guidance.

                  1 Reply Last reply
                  0
                  • A Offline
                    A Offline
                    AntonioQt
                    wrote on 26 Oct 2017, 14:42 last edited by
                    #20

                    Solved!
                    Few things required to be added in addition what @Eeli-K had commented in his last comment. I am providing the final working snippet for future, if anybody is facing the similar issue they may refer the above conversation between experts and me.
                    Below is the final update in "main.qml" :

                    Connections {
                            target: myobj
                            onMySignal: {
                                labelStr.text = str // Set the counter to a text label
                            }
                    

                    "labelStr" is a label that has to be defined separately in QML.

                    1 Reply Last reply
                    0
                    • 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