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?

Send QVector via socket QString?

Scheduled Pinned Locked Moved Solved General and Desktop
18 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.
  • K Offline
    K Offline
    Kris Revi
    wrote on last edited by
    #3

    @JonB well in my python version i sent it as a list (that's what i know worked with the matrix)

        def convertImage(self):
            img = Image.open(self.imagePath)
    
            # Check if img is RGBA if true convert to RGB
            img = img.convert('RGB')
    
            # Shift the fucker arround to being RGB565
            self.final = [ (r >> 3) << 11 | (g >> 2) << 5 | (b >> 3) for (r, g, b) in img.getdata() ]
    
    JonBJ 1 Reply Last reply
    0
    • K Kris Revi

      @JonB well in my python version i sent it as a list (that's what i know worked with the matrix)

          def convertImage(self):
              img = Image.open(self.imagePath)
      
              # Check if img is RGBA if true convert to RGB
              img = img.convert('RGB')
      
              # Shift the fucker arround to being RGB565
              self.final = [ (r >> 3) << 11 | (g >> 2) << 5 | (b >> 3) for (r, g, b) in img.getdata() ]
      
      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by JonB
      #4

      @Kris-Revi
      I don't see why you want to turn the data into a QString to send. It's not text. I don't see any strings in your Python, you have just an array of 16-bit values. Which is presumably what "RGB565" is. Maybe someone else understands what you want.

      Not the best of comments in your code... ;-)

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

        @JonB cause im sending both text and ints so i figured String was the best option! if you know a better way then please tell me!

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

          Hi,

          Your python code just shows a list creation not how you actually are sending data.

          As @JonB already wrote, you have to serialize your vector to string before sending it.

          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
          1
          • SGaistS SGaist

            Hi,

            Your python code just shows a list creation not how you actually are sending data.

            As @JonB already wrote, you have to serialize your vector to string before sending it.

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

            @SGaist you are actualy right! :P

            here (that's how in python)

            self.ui.BUTTON_SEND_ESP32.clicked.connect(lambda : self.socket.sendImage('pixArt', self.final))
            
            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #8

              What is self.socket ?

              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

                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