How to display data from MQTT server in real-time?
-
@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
-
@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.
-
Looks like the repository is not yet on code.qt.io, but you can get it here.
-
@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. -
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.
-
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 exisitingonMQTT_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. -
On a side note, the getter should be const, it doesn't modify anything in the class instance.