QML and QtMQTT publish example



  • Hi all,

    I found a very good example of making QtMQTT available in QML: https://doc.qt.io/QtMQTT/qtmqtt-quicksubscription-example.html
    However, this examples only shows how to subscribe and not how to publish.

    Would anyone know if there is an example out somewhere that describes how to use QtMQTT in QML to publish messages?

    Thanks!



  • @Egn1n3 spoiler alert: not that I've tried it myself...
    from this blog (see code snippet there for publishing data) and the example in your post, I imagine that you need to use the publish() method instead of or in addition to subscribe() method.

    Pseudo-code for your QmlMqttClient class I assume you already have:

    ...
    qint32 QmlMqttClient::publish(const QString &topic, const QByteArray &message, quint8 qos, bool retain)
    {
        auto id = QMqttClient::publish(topic, message, qos, retain);
        return id;
    }
    ...
    


  • Thanks.

    I extended the example with the publish call but nothing is published.
    Not sure if the arguments I provide are correct?

    MqttClient {
        id: client
        hostname: hostnameField.text
        port: portField.text
    }
    

    client.publish("mytopic", "testmessage",0,false)



  • @Egn1n3 said in QML and QtMQTT publish example:

    but nothing is published.

    How do you know?
    What broker are you using to connect to?
    Have you tried using a retained message?



  • @Pablo-J.-Rogina I subscribed to "mytopic" on my mosquitto broker and I enabled debugging on the broker too. Nothing....
    The publish function returns a -1 which indicates an error.

    I am wondering if I need to register something like you need to do with subscription:

    qmlRegisterUncreatableType<QmlMqttSubscription>("MqttClient", 1, 0, "MqttSubscription", QLatin1String("Subscriptions are read-only"));


  • Lifetime Qt Champion

    Hi,

    Did you test your installation with the Simple Client Exemple ?



  • @SGaist I did and Simple Client Example works perfect.
    I'll try to do some more tests....



  • @Egn1n3 I did some test on using QmlMqttClient example and I made it work only when I provided a publish() method with slightly changed parameters, so to avoid QML -> C++ interaction with a QByteArray (as expected for param message). So in C++ I made a Q_INVOKABLE method like this (pseudo-code):

    qint32 QmlMqttClient::publish(const QString &topic, const QString &message, int qos, bool retain)
    {
        auto id = publish(topic, message.toUtf8(), (quint8)qos, retain);
        return id;
    }
    

    which works fine with QML and then inside it calls the original publish() method with the proper type of parameters



  • @Pablo-J.-Rogina said in QML and QtMQTT publish example:

    qint32 QmlMqttClient::publish(const QString &topic, const QString &message, int qos, bool retain)
    {
    auto id = publish(topic, message.toUtf8(), (quint8)qos, retain);
    return id;
    }

    Thanks Pablo, this is finally working. Thanks!

    I had to change the method name from publish to publishme. If I didn't the program crashed.

    This is what I did:
    Added to qmlmqttclient.h:

    Q_INVOKABLE qint32 publishme(const QString &topic, const QString &message, int qos, bool retain);

    qmlmqttclient.h:
    class QmlMqttClient : public QMqttClient
    {
    Q_OBJECT
    public:
    QmlMqttClient(QObject *parent = nullptr);

    Q_INVOKABLE QmlMqttSubscription *subscribe(const QString &topic);
    Q_INVOKABLE qint32 publishme(const QString &topic, const QString &message, int qos, bool retain);
    

    private:
    Q_DISABLE_COPY(QmlMqttClient)
    };

    And to qmlmqttclient.cpp I added my new publishme method:

    qint32 QmlMqttClient::publishme(const QString &topic, const QString &message, int qos, bool retain)
    {
    auto id = publish(topic, message.toUtf8(), (quint8)qos, retain);
    return id;
    }

    From main.qml:
    Button {
    id: publish

        text: qsTr("Publish")
        onClicked: {
            
            if (client.state==MqttClient.Connected){
                result=client.publishme("mytopic","mymessage",0,false)
                console.log(result)
             } else {
                 console.log("Client is not connected")
            }
        }
    }
    

    Not 100% sure if I did this all correctly, but it seems to work.

    Thanks again!



  • Some last remarks: it seems this is a bug as indicated by Maurice Kalinowski:
    https://bugreports.qt.io/browse/QTBUG-69066



  • Hi,

    this has also been discussed on the qt-interest mailing list. Here is the link, also with some solution and patches how to get it working.http://lists.qt-project.org/pipermail/interest/2018-June/030309.html


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.