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
Forum Updated to NodeBB v4.3 + New Features

QML and QtMQTT publish example

Scheduled Pinned Locked Moved Solved QML and Qt Quick
11 Posts 4 Posters 2.9k Views 3 Watching
  • 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.
  • Egn1n3E Egn1n3

    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!

    Pablo J. RoginaP Offline
    Pablo J. RoginaP Offline
    Pablo J. Rogina
    wrote on 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
    • Egn1n3E Offline
      Egn1n3E Offline
      Egn1n3
      wrote on 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)

      Pablo J. RoginaP 2 Replies Last reply
      0
      • Egn1n3E Egn1n3

        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)

        Pablo J. RoginaP Offline
        Pablo J. RoginaP Offline
        Pablo J. Rogina
        wrote on 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

        Egn1n3E 1 Reply Last reply
        0
        • Pablo J. RoginaP Pablo J. Rogina

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

          Egn1n3E Offline
          Egn1n3E Offline
          Egn1n3
          wrote on 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
          • SGaistS Offline
            SGaistS Offline
            SGaist
            Lifetime Qt Champion
            wrote on 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

            Egn1n3E 1 Reply Last reply
            1
            • SGaistS SGaist

              Hi,

              Did you test your installation with the Simple Client Exemple ?

              Egn1n3E Offline
              Egn1n3E Offline
              Egn1n3
              wrote on 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
              • Egn1n3E Egn1n3

                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)

                Pablo J. RoginaP Offline
                Pablo J. RoginaP Offline
                Pablo J. Rogina
                wrote on 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
                • Egn1n3E Offline
                  Egn1n3E Offline
                  Egn1n3
                  wrote on 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
                  • Egn1n3E Offline
                    Egn1n3E Offline
                    Egn1n3
                    wrote on 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 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

                      • Login

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