Discussion:
[Interest] How to publish a message in QML with Qtmqtt
Egg Nine
2018-06-17 19:19:24 UTC
Permalink
Hi all,

I am trying to publish an mqtt message using Qt QML following this example:
https://doc.qt.io/QtMQTT/qtmqtt-quicksubscription-example.html

However, this example only shows how to subscribe to topics while I want to
publish.
I tried to call "publish" from QML like can see in code below but nothing
is sent while console.log shows a -1.

Any thoughts what I am doing wrong? Or does someone has an example?
I can see in my broker log that a connection is made successfully.

MqttClient {
id: mqttclient
port: 1883
hostname: "localhost"
}

Button {
id: connect
text: qsTr("Connect")
onClicked: {
console.log(mqttclient.connectToHost())
}
}

Button {
id: publish
text: qsTr("publish")
onClicked: {
console.log(mqttclient.publish("mytopic","mytestmessage"))
}
}

Thanks!
Maurice Kalinowski
2018-06-19 07:11:09 UTC
Permalink
Hi,

You seem to be perfectly right that publishing does not work out of the box. This is mostly because of a failing auto-conversion from QString to QMqttTopicName.

The easiest workaround is to apply attached patch, which adds a publish using a QString and do the conversion manually. Feel free to create a bugreport, so that we can take this into consideration when adding a proper qml api for this module.

BR,
Maurice Kalinowski

Von: Interest <interest-bounces+maurice.kalinowski=***@qt-project.org> Im Auftrag von Egg Nine
Gesendet: Sunday, June 17, 2018 9:19 PM
An: ***@qt-project.org
Betreff: [Interest] How to publish a message in QML with Qtmqtt

Hi all,

I am trying to publish an mqtt message using Qt QML following this example:
https://doc.qt.io/QtMQTT/qtmqtt-quicksubscription-example.html

However, this example only shows how to subscribe to topics while I want to publish.
I tried to call "publish" from QML like can see in code below but nothing is sent while console.log shows a -1.

Any thoughts what I am doing wrong? Or does someone has an example?
I can see in my broker log that a connection is made successfully.

MqttClient {
id: mqttclient
port: 1883
hostname: "localhost"
}

Button {
id: connect
text: qsTr("Connect")
onClicked: {
console.log(mqttclient.connectToHost())
}
}

Button {
id: publish
text: qsTr("publish")
onClicked: {
console.log(mqttclient.publish("mytopic","mytestmessage"))
}
}

Thanks!
Egg Nine
2018-06-21 15:31:46 UTC
Permalink
Thanks Maurice. I can confirm that the patch does work.
I created a bug report: https://bugreports.qt.io/browse/QTBUG-69066
Post by Maurice Kalinowski
Hi,
You seem to be perfectly right that publishing does not work out of the
box. This is mostly because of a failing auto-conversion from QString to
QMqttTopicName.
The easiest workaround is to apply attached patch, which adds a publish
using a QString and do the conversion manually. Feel free to create a
bugreport, so that we can take this into consideration when adding a proper
qml api for this module.
BR,
Maurice Kalinowski
*Im Auftrag von *Egg Nine
*Gesendet:* Sunday, June 17, 2018 9:19 PM
*Betreff:* [Interest] How to publish a message in QML with Qtmqtt
Hi all,
https://doc.qt.io/QtMQTT/qtmqtt-quicksubscription-example.html
However, this example only shows how to subscribe to topics while I want to publish.
I tried to call "publish" from QML like can see in code below but nothing
is sent while console.log shows a -1.
Any thoughts what I am doing wrong? Or does someone has an example?
I can see in my broker log that a connection is made successfully.
MqttClient {
id: mqttclient
port: 1883
hostname: "localhost"
}
Button {
id: connect
text: qsTr("Connect")
onClicked: {
console.log(mqttclient.connectToHost())
}
}
Button {
id: publish
text: qsTr("publish")
onClicked: {
console.log(mqttclient.publish("mytopic","mytestmessage"))
}
}
Thanks!
Loading...