How to display data from MQTT server in real-time?
Solved
QML and Qt Quick
-
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.