Distinguish between text message and image message sent from server (both encoded in qstring)
-
Okay So I'm sending msgs like this-
QFile file(fileName); if (!file.open(QFile::ReadOnly)) { qDebug() << "Cannot open the selected file"; return; } QByteArray block; block = file.readAll(); block.toBase64(); QString message(block); QString roomID("rID"); QString type("image"); qDebug() << "base64 image- " << message; QString json = "{\"rid\": \"%1\", \"msg\": \"%2\", \"type\": \"%3\"}"; json = json.arg(roomID, message, type); sendMessage("sendMessage", QJsonDocument::fromJson(json.toUtf8()));And receiving them like-
QString type = result.value("type").toString(); QString msg = result.value("msg").toString(); if (type == "image"){ QByteArray decodedImage; QByteArray image; QPixmap pixmap; QLabel label; decodedImage.append(msg); image = QByteArray::fromBase64(decodedImage); pixmap.loadFromData(image,0,Qt::AutoColor); label.setPixmap(pixmap); label.show(); }But only "����" this is displayed instead of the image.
-
Use QByteArray directly rather than QString, that will avoid unless conversions.
Also your
labelvariable only exists for the lifetime of the if block in case of type being image so it won't even be shown. -
Use QByteArray directly rather than QString, that will avoid unless conversions.
Also your
labelvariable only exists for the lifetime of the if block in case of type being image so it won't even be shown. -
Do you mean showing images ?
-
Okay So I'm sending msgs like this-
QFile file(fileName); if (!file.open(QFile::ReadOnly)) { qDebug() << "Cannot open the selected file"; return; } QByteArray block; block = file.readAll(); block.toBase64(); QString message(block); QString roomID("rID"); QString type("image"); qDebug() << "base64 image- " << message; QString json = "{\"rid\": \"%1\", \"msg\": \"%2\", \"type\": \"%3\"}"; json = json.arg(roomID, message, type); sendMessage("sendMessage", QJsonDocument::fromJson(json.toUtf8()));And receiving them like-
QString type = result.value("type").toString(); QString msg = result.value("msg").toString(); if (type == "image"){ QByteArray decodedImage; QByteArray image; QPixmap pixmap; QLabel label; decodedImage.append(msg); image = QByteArray::fromBase64(decodedImage); pixmap.loadFromData(image,0,Qt::AutoColor); label.setPixmap(pixmap); label.show(); }But only "����" this is displayed instead of the image.
-
@Vasudha said in Distinguish between text message and image message sent from server (both encoded in qstring):
block.toBase64();
That is a const method. Do you mean
block=block.toBase64();? -
QFile file(fileName); if (!file.open(QFile::ReadOnly)) { qDebug() << "Cannot open the selected file"; return; } const QString block = QString::fromLatin1(file.readAll().toBase64()); const QString roomID("rID"); const QString type("image") QJsonObject jSonImage; jSonImage.insert("rid",roomID); jSonImage.insert("type",type); jSonImage.insert("msg",block); sendMessage("sendMessage", QJsonDocument(jSonImage)); -
QFile file(fileName); if (!file.open(QFile::ReadOnly)) { qDebug() << "Cannot open the selected file"; return; } const QString block = QString::fromLatin1(file.readAll().toBase64()); const QString roomID("rID"); const QString type("image") QJsonObject jSonImage; jSonImage.insert("rid",roomID); jSonImage.insert("type",type); jSonImage.insert("msg",block); sendMessage("sendMessage", QJsonDocument(jSonImage)); -
A QtQuick Image element ?