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. Send QVector via socket QString?
Qt 6.11 is out! See what's new in the release blog

Send QVector via socket QString?

Scheduled Pinned Locked Moved Solved General and Desktop
18 Posts 4 Posters 3.5k 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.
  • SGaistS SGaist

    What is self.socket ?

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

    @SGaist

    self.socket = Socket(host="matrix32")
    self.socket.Connect()
    

    MySocket.py

    from PyQt5 import QtCore, QtWebSockets
    from PyQt5.QtCore import QUrl
    import json
    
    class Socket:
    
        def __init__(self, *args, **kwargs):
            self.client = QtWebSockets.QWebSocket('', QtWebSockets.QWebSocketProtocol.Version13, None)
            self.client.error.connect(self.on_error)
            self.hostAdr = kwargs['host']
    
        def Connect(self):
            self.client.open(QUrl(f"ws://{self.hostAdr}:80"))
    
        def sendMessage(self, bName, bValue):
            self.client.sendTextMessage('{"' + bName + '": "' + str(bValue) + '"}')
    
        def sendImage(self, bName, bValue):
            self.client.sendTextMessage(json.dumps({bName: bValue}))
    
        def on_error(self, error_code):
            print('error code: {}'.format(error_code))
            print(self.client.errorString())
    
        def close(self):
            self.client.close()
    

    i see here that i actualy json.dumps what is the equal for Qt ?

    JonBJ 1 Reply Last reply
    0
    • K Kris Revi

      @SGaist

      self.socket = Socket(host="matrix32")
      self.socket.Connect()
      

      MySocket.py

      from PyQt5 import QtCore, QtWebSockets
      from PyQt5.QtCore import QUrl
      import json
      
      class Socket:
      
          def __init__(self, *args, **kwargs):
              self.client = QtWebSockets.QWebSocket('', QtWebSockets.QWebSocketProtocol.Version13, None)
              self.client.error.connect(self.on_error)
              self.hostAdr = kwargs['host']
      
          def Connect(self):
              self.client.open(QUrl(f"ws://{self.hostAdr}:80"))
      
          def sendMessage(self, bName, bValue):
              self.client.sendTextMessage('{"' + bName + '": "' + str(bValue) + '"}')
      
          def sendImage(self, bName, bValue):
              self.client.sendTextMessage(json.dumps({bName: bValue}))
      
          def on_error(self, error_code):
              print('error code: {}'.format(error_code))
              print(self.client.errorString())
      
          def close(self):
              self.client.close()
      

      i see here that i actualy json.dumps what is the equal for Qt ?

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

      @Kris-Revi
      const QByteArray QJsonDocument::toJson(QJsonDocument::JsonFormat format)

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

        Technically there's no direct equivalent. However you can build a QString with a simple loop for example. Maybe std::transform or QTextStream.

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

        JonBJ 1 Reply Last reply
        0
        • SGaistS SGaist

          Technically there's no direct equivalent. However you can build a QString with a simple loop for example. Maybe std::transform or QTextStream.

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

          @SGaist
          ? Why isn't const QByteArray QJsonDocument::toJson(QJsonDocument::JsonFormat format) the closest to what OP wants for json.dumps?

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

            @JonB said in Send QVector via socket QString?:

            const QByteArray QJsonDocument::toJson(QJsonDocument::JsonFormat format)

            how do i use that?

            JonBJ 1 Reply Last reply
            0
            • B Offline
              B Offline
              Bonnie
              wrote on last edited by Bonnie
              #14

              You don't have to use QVector.
              To make it easy for json, I would change the code to

              QJsonArray RGB565;
              for(int y = 0; y < img2.height(); y++) {
                  const quint16 *line = reinterpret_cast<const quint16*>(img2.constScanLine(y));
                  for(int x = 0; x < img2.width(); x++)
                      RGB565 << *(line++);
              }
              

              Meanwhile your send command can also be changed to more json-friendly

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

              So then you can call the command directly

              socket.sendCommandStrip(QString("pixArt"), RGB565);
              //you can also pass QString as the value
              socket.sendCommandStrip(QString("otherName"), QString("otherValue"));
              //or number
              socket.sendCommandStrip(QString("anotherName"), 100);
              
              K 1 Reply Last reply
              3
              • B Bonnie

                You don't have to use QVector.
                To make it easy for json, I would change the code to

                QJsonArray RGB565;
                for(int y = 0; y < img2.height(); y++) {
                    const quint16 *line = reinterpret_cast<const quint16*>(img2.constScanLine(y));
                    for(int x = 0; x < img2.width(); x++)
                        RGB565 << *(line++);
                }
                

                Meanwhile your send command can also be changed to more json-friendly

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

                So then you can call the command directly

                socket.sendCommandStrip(QString("pixArt"), RGB565);
                //you can also pass QString as the value
                socket.sendCommandStrip(QString("otherName"), QString("otherValue"));
                //or number
                socket.sendCommandStrip(QString("anotherName"), 100);
                
                K Offline
                K Offline
                Kris Revi
                wrote on last edited by
                #15

                @Bonnie omg that is sexy! 😅

                1 Reply Last reply
                0
                • K Kris Revi

                  @JonB said in Send QVector via socket QString?:

                  const QByteArray QJsonDocument::toJson(QJsonDocument::JsonFormat format)

                  how do i use that?

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

                  @Kris-Revi said in Send QVector via socket QString?:

                  const QByteArray QJsonDocument::toJson(QJsonDocument::JsonFormat format)

                  how do i use that?

                  Note how that is what @Bonnie is using in his example :)

                  K 1 Reply Last reply
                  0
                  • JonBJ JonB

                    @Kris-Revi said in Send QVector via socket QString?:

                    const QByteArray QJsonDocument::toJson(QJsonDocument::JsonFormat format)

                    how do i use that?

                    Note how that is what @Bonnie is using in his example :)

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

                    @JonB yea i see that :) but he also made my sendCommand sexy in the process xD

                    JonBJ 1 Reply Last reply
                    0
                    • K Kris Revi

                      @JonB yea i see that :) but he also made my sendCommand sexy in the process xD

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

                      @Kris-Revi
                      LOL. If that's what floats your "sexy" boat, that is good :D The point anyway is that it is via QJsonDocument::toJson() that you can transform your data into something suitable to send.

                      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