Solved Send QVector via socket QString?
-
@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))
-
What is self.socket ?
-
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 ? -
-
Technically there's no direct equivalent. However you can build a QString with a simple loop for example. Maybe std::transform or QTextStream.
-
@SGaist
? Why isn't const QByteArray QJsonDocument::toJson(QJsonDocument::JsonFormat format) the closest to what OP wants forjson.dumps
? -
@JonB said in Send QVector via socket QString?:
const QByteArray QJsonDocument::toJson(QJsonDocument::JsonFormat format)
how do i use that?
-
You don't have to use QVector.
To make it easy for json, I would change the code toQJsonArray 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);
-
@Bonnie omg that is sexy! 😅
-
@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 :)
-
@JonB yea i see that :) but he also made my
sendCommand
sexy in the process xD -
@Kris-Revi
LOL. If that's what floats your "sexy" boat, that is good :D The point anyway is that it is viaQJsonDocument::toJson()
that you can transform your data into something suitable to send.