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. Distinguish between text message and image message sent from server (both encoded in qstring)

Distinguish between text message and image message sent from server (both encoded in qstring)

Scheduled Pinned Locked Moved Solved General and Desktop
21 Posts 6 Posters 7.4k Views 2 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 Offline
    SGaistS Offline
    SGaist
    Lifetime Qt Champion
    wrote on last edited by
    #8

    This is wrong on several levels:

    • Why load your image in a QImage ? You want to transmit the content of the file, no ?
    • JSON is text based, you are trying to send binary data

    If you want to send binary data in JSON you have to first convert them using e.g. base64.

    Since you are sending JSON anyway, why not have a field that states the type of the content of params rather than doing guesses ?

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

    1 Reply Last reply
    4
    • V Offline
      V Offline
      Vasudha
      wrote on last edited by
      #9

      Having a type field is a good idea.
      And yeah, I just want to transmit the content of the file. Shouldn't I be loading the image first? How else can it be done?

      jsulmJ 1 Reply Last reply
      0
      • V Vasudha

        Having a type field is a good idea.
        And yeah, I just want to transmit the content of the file. Shouldn't I be loading the image first? How else can it be done?

        jsulmJ Offline
        jsulmJ Offline
        jsulm
        Lifetime Qt Champion
        wrote on last edited by jsulm
        #10

        @Vasudha You need to encode the content of the image file (no need to use QImage, just read the file using QFile) as base64 and add this encoded string as a field in your JSON:

        {"type": "image", "data": ".PUT HERE BASE64 ENCODED IMAGE DATA"}
        

        https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply
        3
        • V Offline
          V Offline
          Vasudha
          wrote on last edited by
          #11

          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.

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

            Use QByteArray directly rather than QString, that will avoid unless conversions.

            Also your label variable only exists for the lifetime of the if block in case of type being image so it won't even be shown.

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

            V 1 Reply Last reply
            4
            • SGaistS SGaist

              Use QByteArray directly rather than QString, that will avoid unless conversions.

              Also your label variable only exists for the lifetime of the if block in case of type being image so it won't even be shown.

              V Offline
              V Offline
              Vasudha
              wrote on last edited by
              #13

              @SGaist After correcting this part too the result was same. I realize I have been mixing QML with QWidgets and that's what is causing problems. Is there any other way of receiving images without using QWidgets?

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

                Do you mean showing images ?

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

                V 1 Reply Last reply
                0
                • V Vasudha

                  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.

                  VRoninV Offline
                  VRoninV Offline
                  VRonin
                  wrote on last edited by
                  #15

                  @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();?

                  "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                  ~Napoleon Bonaparte

                  On a crusade to banish setIndexWidget() from the holy land of Qt

                  V 1 Reply Last reply
                  2
                  • SGaistS SGaist

                    Do you mean showing images ?

                    V Offline
                    V Offline
                    Vasudha
                    wrote on last edited by
                    #16

                    @SGaist yes

                    1 Reply Last reply
                    1
                    • VRoninV VRonin

                      @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();?

                      V Offline
                      V Offline
                      Vasudha
                      wrote on last edited by
                      #17

                      @VRonin yeah

                      1 Reply Last reply
                      1
                      • VRoninV Offline
                        VRoninV Offline
                        VRonin
                        wrote on last edited by VRonin
                        #18
                        
                        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));
                        

                        "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                        ~Napoleon Bonaparte

                        On a crusade to banish setIndexWidget() from the holy land of Qt

                        V 1 Reply Last reply
                        4
                        • VRoninV VRonin
                          
                          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));
                          
                          V Offline
                          V Offline
                          Vasudha
                          wrote on last edited by
                          #19

                          @VRonin This was very smart. The image is being sent successfully but how to show it (at receiving end) without using QWidgets (after decoding it)?

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

                            A QtQuick Image element ?

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

                            1 Reply Last reply
                            3
                            • V Offline
                              V Offline
                              Vasudha
                              wrote on last edited by
                              #21

                              Thank you everyone :)

                              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