Send QVector via socket QString?
-
so i have this
QVector<quint16> RGB565; // quint16 = 16bit 565 | quint32 = 32bit 888 RGB565.reserve(img2.width() * img2.height()); 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++); }
i need to be able to send this
RGB565
via socket that is sending with QStringsocket.sendCommandStrip(QString("pixArt"), QString(RGB565));
void Socket::sendCommandStrip(const QString &bName, const QString bValue) { m_webSocket.sendTextMessage(QStringLiteral("{ '%0' : '%1' }").arg(bName).arg(bValue)); }
-
@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() ]
-
@Kris-Revi
I don't see why you want to turn the data into aQString
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... ;-)
-
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.
-
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);
-
@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 :)