Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Qt Websocket sending json
Forum Updated to NodeBB v4.3 + New Features

Qt Websocket sending json

Scheduled Pinned Locked Moved Unsolved General and Desktop
14 Posts 4 Posters 1.8k Views 1 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.
  • Christian EhrlicherC Offline
    Christian EhrlicherC Offline
    Christian Ehrlicher
    Lifetime Qt Champion
    wrote on last edited by
    #2

    And what's the acutal error? sendTextMessage() takes a QString but QJsonDocument::toJson() returns a (utf-8) encoded QByteArray so you should convert it to a QString first.

    Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
    Visit the Qt Academy at https://academy.qt.io/catalog

    K 1 Reply Last reply
    2
    • Christian EhrlicherC Christian Ehrlicher

      And what's the acutal error? sendTextMessage() takes a QString but QJsonDocument::toJson() returns a (utf-8) encoded QByteArray so you should convert it to a QString first.

      K Offline
      K Offline
      Kris Revi
      wrote on last edited by
      #3

      @Christian-Ehrlicher well not any error

      i know to send like this

      connect(ui->pushButton, &QPushButton::clicked, [=]() { socket.sendBinary("doStuff", 0); });
      

      with

      void Socket::sendBinary(const QString &bName, const QJsonValue bValue)
      {
          webSocket.sendTextMessage(QJsonDocument(QJsonObject{{bName, bValue}}).toJson(QJsonDocument::Compact));
      }
      

      but how can i send {"seg":{"fx":0}}

      1 Reply Last reply
      0
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on last edited by
        #4

        Hi,

        Exactly as @Christian-Ehrlicher wrote: make a QString out of the QByteArray generated by toJson.

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

        1 Reply Last reply
        0
        • K Offline
          K Offline
          Kris Revi
          wrote on last edited by
          #5

          like this?

          void Socket::sendBinary(const QString &bName, const QJsonValue bValue)
          {    
              QJsonObject obj;
              obj.insert("seq", "{'fx':0}");
              QJsonDocument doc(obj);
              QString strJson(doc.toJson(QJsonDocument::Compact));
          
              webSocket.sendTextMessage(strJson);
          }
          
          1 Reply Last reply
          0
          • SGaistS Offline
            SGaistS Offline
            SGaist
            Lifetime Qt Champion
            wrote on last edited by
            #6

            QString::fromUtf8 since you know that QJsonDocument::toJson returns an UTF-8 encoded document.

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

            K 1 Reply Last reply
            0
            • SGaistS SGaist

              QString::fromUtf8 since you know that QJsonDocument::toJson returns an UTF-8 encoded document.

              K Offline
              K Offline
              Kris Revi
              wrote on last edited by Kris Revi
              #7

              @SGaist am i doing something wrong?

              void Socket::sendBinary(const QString &bName, const QJsonValue bValue)
              {
                  QJsonObject obj;
                  obj.insert("seq", "{'fx':10}");
                  QJsonDocument doc(obj);
                  QString strJson(doc.toJson(QJsonDocument::Compact));
                  QString s2 = QString::fromUtf8(strJson.toUtf8().constData());
              
                  webSocket.sendTextMessage(s2);
              
                  qDebug() << s2;
              
              }
              
              JonBJ SGaistS 2 Replies Last reply
              0
              • K Kris Revi

                @SGaist am i doing something wrong?

                void Socket::sendBinary(const QString &bName, const QJsonValue bValue)
                {
                    QJsonObject obj;
                    obj.insert("seq", "{'fx':10}");
                    QJsonDocument doc(obj);
                    QString strJson(doc.toJson(QJsonDocument::Compact));
                    QString s2 = QString::fromUtf8(strJson.toUtf8().constData());
                
                    webSocket.sendTextMessage(s2);
                
                    qDebug() << s2;
                
                }
                
                JonBJ Offline
                JonBJ Offline
                JonB
                wrote on last edited by
                #8

                @Kris-Revi Why, what are you finding that is wrong?

                K 2 Replies Last reply
                0
                • JonBJ JonB

                  @Kris-Revi Why, what are you finding that is wrong?

                  K Offline
                  K Offline
                  Kris Revi
                  wrote on last edited by
                  #9

                  @JonB this is the ouput

                  "{\"seq\":\"{'fx':10}\"}"
                  

                  but on the ESP32 that controlls the Led Strip the pattern does not change

                  im using the WLED software installed on the ESP32 and im trying to make a simple App in Qt to controll it via Websocket! i can connect and i can easily change brightness by sending "bri", 0-255

                  but to change pattern i was told to send {"seg":{"fx":0}}

                  JonBJ 1 Reply Last reply
                  0
                  • JonBJ JonB

                    @Kris-Revi Why, what are you finding that is wrong?

                    K Offline
                    K Offline
                    Kris Revi
                    wrote on last edited by
                    #10

                    @JonB as you can see here

                    https://kno.wled.ge/interfaces/json-api/

                        "seg": [{
                          "start": 0,
                          "stop": 20,
                          "len": 20,
                          "col": [
                            [255, 160, 0, 0],
                            [0, 0, 0, 0],
                            [0, 0, 0, 0]
                          ],
                          "fx": 0,
                          "sx": 127,
                          "ix": 127,
                          "pal": 0,
                          "sel": true,
                          "rev": false,
                          "cln": -1
                        }]
                    
                    1 Reply Last reply
                    0
                    • K Kris Revi

                      @JonB this is the ouput

                      "{\"seq\":\"{'fx':10}\"}"
                      

                      but on the ESP32 that controlls the Led Strip the pattern does not change

                      im using the WLED software installed on the ESP32 and im trying to make a simple App in Qt to controll it via Websocket! i can connect and i can easily change brightness by sending "bri", 0-255

                      but to change pattern i was told to send {"seg":{"fx":0}}

                      JonBJ Offline
                      JonBJ Offline
                      JonB
                      wrote on last edited by
                      #11

                      @Kris-Revi said in Qt Websocket sending json:

                      "{\"seq\":\"{'fx':10}\"}"

                      {"seg":{"fx":0}}

                      First, these are similar. The debugger (if that's what you're using to view output, you do not say) with show literal "s as \".

                      Where I think you are showing a difference:

                      obj.insert("seq", "{'fx':10}");
                      

                      You are inserting a single object with the literal string "{'fx':10}" as its value. But {"seg":{"fx":0}} looks more like an outer object with object {"fx":0} as its value, so you are not constructing your JSON correctly. But that has nothing to do with websocket or how you send the JSON.

                      K 1 Reply Last reply
                      0
                      • JonBJ JonB

                        @Kris-Revi said in Qt Websocket sending json:

                        "{\"seq\":\"{'fx':10}\"}"

                        {"seg":{"fx":0}}

                        First, these are similar. The debugger (if that's what you're using to view output, you do not say) with show literal "s as \".

                        Where I think you are showing a difference:

                        obj.insert("seq", "{'fx':10}");
                        

                        You are inserting a single object with the literal string "{'fx':10}" as its value. But {"seg":{"fx":0}} looks more like an outer object with object {"fx":0} as its value, so you are not constructing your JSON correctly. But that has nothing to do with websocket or how you send the JSON.

                        K Offline
                        K Offline
                        Kris Revi
                        wrote on last edited by
                        #12

                        @JonB said in Qt Websocket sending json:

                        First, these are similar. The debugger (if that's what you're using to view output, you do not say)

                        yea the debugger (Application Output)...

                        how do i construct the json right with the outer object? i have only done the "easy" ("bri", 0-255)....

                        JonBJ 1 Reply Last reply
                        0
                        • K Kris Revi

                          @JonB said in Qt Websocket sending json:

                          First, these are similar. The debugger (if that's what you're using to view output, you do not say)

                          yea the debugger (Application Output)...

                          how do i construct the json right with the outer object? i have only done the "easy" ("bri", 0-255)....

                          JonBJ Offline
                          JonBJ Offline
                          JonB
                          wrote on last edited by
                          #13

                          @Kris-Revi
                          I haven't looked up what the available functions are, but I imagine you need something more like:

                              QJsonObject obj. obj2;
                              obj2.insert("fx", 10);
                              obj.insert("seq", obj2);
                          
                          1 Reply Last reply
                          1
                          • K Kris Revi

                            @SGaist am i doing something wrong?

                            void Socket::sendBinary(const QString &bName, const QJsonValue bValue)
                            {
                                QJsonObject obj;
                                obj.insert("seq", "{'fx':10}");
                                QJsonDocument doc(obj);
                                QString strJson(doc.toJson(QJsonDocument::Compact));
                                QString s2 = QString::fromUtf8(strJson.toUtf8().constData());
                            
                                webSocket.sendTextMessage(s2);
                            
                                qDebug() << s2;
                            
                            }
                            
                            SGaistS Offline
                            SGaistS Offline
                            SGaist
                            Lifetime Qt Champion
                            wrote on last edited by
                            #14

                            @Kris-Revi said in Qt Websocket sending json:

                            @SGaist am i doing something wrong?

                            void Socket::sendBinary(const QString &bName, const QJsonValue bValue)
                            {
                                QJsonObject obj;
                                obj.insert("seq", "{'fx':10}");
                                QJsonDocument doc(obj);
                                QString strJson(doc.toJson(QJsonDocument::Compact));
                                QString s2 = QString::fromUtf8(strJson.toUtf8().constData());
                            
                                webSocket.sendTextMessage(s2);
                            
                                qDebug() << s2;
                            
                            }
                            

                            Two things in fact:
                            First, the single quotes around fx, it's invalid.
                            Second, the triple conversion. There's no need for s2.

                            QByteArray jsonMessage(doc.toJson(QJsonDocument::Compact));
                            QString stringMessage = QString::fromUtf8(jsonMessage);
                            

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

                            1 Reply Last reply
                            1

                            • Login

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