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. QML and QtMQTT publish example
QtWS25 Last Chance

QML and QtMQTT publish example

Scheduled Pinned Locked Moved Solved QML and Qt Quick
11 Posts 4 Posters 2.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.
  • E Offline
    E Offline
    Egn1n3
    wrote on 13 Jun 2018, 10:15 last edited by
    #1

    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!

    P 1 Reply Last reply 13 Jun 2018, 12:16
    0
    • E Egn1n3
      13 Jun 2018, 10:15

      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!

      P Offline
      P Offline
      Pablo J. Rogina
      wrote on 13 Jun 2018, 12:16 last edited by Pablo J. Rogina
      #2

      @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;
      }
      ...
      

      Upvote the answer(s) that helped you solve the issue
      Use "Topic Tools" button to mark your post as Solved
      Add screenshots via postimage.org
      Don't ask support requests via chat/PM. Please use the forum so others can benefit from the solution in the future

      1 Reply Last reply
      1
      • E Offline
        E Offline
        Egn1n3
        wrote on 13 Jun 2018, 19:34 last edited by
        #3

        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)

        P 2 Replies Last reply 13 Jun 2018, 19:44
        0
        • E Egn1n3
          13 Jun 2018, 19:34

          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)

          P Offline
          P Offline
          Pablo J. Rogina
          wrote on 13 Jun 2018, 19:44 last edited by
          #4

          @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?

          Upvote the answer(s) that helped you solve the issue
          Use "Topic Tools" button to mark your post as Solved
          Add screenshots via postimage.org
          Don't ask support requests via chat/PM. Please use the forum so others can benefit from the solution in the future

          E 1 Reply Last reply 13 Jun 2018, 19:55
          0
          • P Pablo J. Rogina
            13 Jun 2018, 19:44

            @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?

            E Offline
            E Offline
            Egn1n3
            wrote on 13 Jun 2018, 19:55 last edited by
            #5

            @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"));

            1 Reply Last reply
            0
            • S Offline
              S Offline
              SGaist
              Lifetime Qt Champion
              wrote on 13 Jun 2018, 20:15 last edited by
              #6

              Hi,

              Did you test your installation with the Simple Client Exemple ?

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

              E 1 Reply Last reply 14 Jun 2018, 11:26
              1
              • S SGaist
                13 Jun 2018, 20:15

                Hi,

                Did you test your installation with the Simple Client Exemple ?

                E Offline
                E Offline
                Egn1n3
                wrote on 14 Jun 2018, 11:26 last edited by
                #7

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

                1 Reply Last reply
                0
                • E Egn1n3
                  13 Jun 2018, 19:34

                  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)

                  P Offline
                  P Offline
                  Pablo J. Rogina
                  wrote on 18 Jun 2018, 18:28 last edited by Pablo J. Rogina
                  #8

                  @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

                  Upvote the answer(s) that helped you solve the issue
                  Use "Topic Tools" button to mark your post as Solved
                  Add screenshots via postimage.org
                  Don't ask support requests via chat/PM. Please use the forum so others can benefit from the solution in the future

                  1 Reply Last reply
                  2
                  • E Offline
                    E Offline
                    Egn1n3
                    wrote on 19 Jun 2018, 06:32 last edited by Egn1n3
                    #9

                    @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!

                    1 Reply Last reply
                    0
                    • E Offline
                      E Offline
                      Egn1n3
                      wrote on 21 Jun 2018, 19:16 last edited by
                      #10

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

                      1 Reply Last reply
                      1
                      • M Offline
                        M Offline
                        mkalinow
                        wrote on 22 Jun 2018, 13:49 last edited by
                        #11

                        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

                        1 Reply Last reply
                        3

                        5/11

                        13 Jun 2018, 19:55

                        • Login

                        • Login or register to search.
                        5 out of 11
                        • First post
                          5/11
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • Users
                        • Groups
                        • Search
                        • Get Qt Extensions
                        • Unsolved