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. How to get a picture from QString in Qt
Forum Update on Monday, May 27th 2025

How to get a picture from QString in Qt

Scheduled Pinned Locked Moved Solved General and Desktop
23 Posts 6 Posters 2.5k Views
  • 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.
  • Christian EhrlicherC Offline
    Christian EhrlicherC Offline
    Christian Ehrlicher
    Lifetime Qt Champion
    wrote on last edited by
    #7

    And how do you transfer the data?

    Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
    Visit the Qt Academy at https://academy.qt.io/catalog

    1 Reply Last reply
    1
    • M Offline
      M Offline
      Mikeeeeee
      wrote on last edited by
      #8

      I use TcpSocket

      Gojir4G aha_1980A 2 Replies Last reply
      0
      • M Mikeeeeee

        I use TcpSocket

        Gojir4G Offline
        Gojir4G Offline
        Gojir4
        wrote on last edited by Gojir4
        #9

        @mikeeeeee Hi,

        I think it's easier if you use QDataStream.
        I never tested but it seems quite straightforward

        Write

        QDataStream out(&socket);   // serialize the data 
        out << myImage;             // serialize an image
        

        Read

        QDataStream in(&socket);    // read the data serialized from the socket
        QImage myImage;
        in >> myImage;              // extract image
        
        1 Reply Last reply
        4
        • M Mikeeeeee

          I use TcpSocket

          aha_1980A Offline
          aha_1980A Offline
          aha_1980
          Lifetime Qt Champion
          wrote on last edited by
          #10

          @mikeeeeee said in How to get a picture from QString in Qt:

          I use TcpSocket

          Then you don't need QString. You need QByteArray, probably QDataStream as @Gojir4 pointed out.

          QString imageString = QString::fromStdString(bArray.toStdString());

          Sorry, but never do that!

          QString imageString = QString::fromUtf8(bArray); is most often correct, sometimes fromLatin1() or fromLocal8Bit() might be needed too.

          But as said, your problem cannot be solved with QString.

          Regards

          Qt has to stay free or it will die.

          1 Reply Last reply
          4
          • M Offline
            M Offline
            Mikeeeeee
            wrote on last edited by
            #11

            But I pass the picture to json , and with the picture I pass additional information to the server. So I need a QString.

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

              Hi,

              As already explained by my fellows, use QByteArray. QString is for string manipulation, it's not what you are doing. You are sending data through network. Therefore you are creating a base64 QByteArray that represent your image's binary data. There's no need for any QString in between.

              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
              1
              • M Offline
                M Offline
                Mikeeeeee
                wrote on last edited by
                #13

                But as I write in Qbytearray json?
                And why this doesn't it work:

                    /*QString imageString = QString::fromLocal8Bit(bArray);
                    bArray2 = QByteArray::fromHex(imageString.toLocal8Bit());*/
                    QString imageString = QString::fromLatin1(bArray);
                    bArray2 = QByteArray::fromHex(imageString.toLatin1());
                    if (bArray != bArray2) {qDebug()<<"bArray != bArray2";}
                    else {"bArray == bArray2";}
                

                debug return: bArray != bArray2

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

                  At one point you are encoding in base64 and the other you are decoding from hex. These are two completely different things.

                  Also, QString does a conversion to UTF16. So again, the fact that JSON contains text doesn't mean that you need to use QString.

                  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
                  • M Offline
                    M Offline
                    Mikeeeeee
                    wrote on last edited by
                    #15

                    But as a json, you can write a qbytearray? And how to write the qbytearray to a string and then again in qbytearray?

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

                      Take a look at QJsonDocument, no QString there.

                      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
                      2
                      • M Offline
                        M Offline
                        Mikeeeeee
                        wrote on last edited by
                        #17

                        But I can't write qbytearray to json. And how to write the qbytearray to a string and then again in qbytearray?

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

                          You have to understand that it's the QByteArray that will contain the JSON data.

                          Again, read the documentation of QJsonDocument, the related classes and example code.

                          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
                          1
                          • M Offline
                            M Offline
                            Mikeeeeee
                            wrote on last edited by
                            #19

                            I read the documentation, it does not specify the ability to add QByteArray to json. So how can I translate QByteArray to Qstring and then to QByteArray?

                            1 Reply Last reply
                            0
                            • M Mikeeeeee

                              But I can't write qbytearray to json. And how to write the qbytearray to a string and then again in qbytearray?

                              aha_1980A Offline
                              aha_1980A Offline
                              aha_1980
                              Lifetime Qt Champion
                              wrote on last edited by
                              #20

                              @mikeeeeee Then you need to read more carefully.

                              https://doc.qt.io/qt-5/qjsondocument.html#toJson
                              https://doc.qt.io/qt-5/qjsondocument.html#fromJson

                              Qt has to stay free or it will die.

                              1 Reply Last reply
                              0
                              • M Offline
                                M Offline
                                Mikeeeeee
                                wrote on last edited by
                                #21

                                I don't see how it will help to write QByteArray to json. Again, I can write Qstring to json. It remains only to learn how to safely translate from Qstring to QByteArray .

                                J.HilkJ 1 Reply Last reply
                                0
                                • M Mikeeeeee

                                  I don't see how it will help to write QByteArray to json. Again, I can write Qstring to json. It remains only to learn how to safely translate from Qstring to QByteArray .

                                  J.HilkJ Online
                                  J.HilkJ Online
                                  J.Hilk
                                  Moderators
                                  wrote on last edited by
                                  #22

                                  @mikeeeeee do you actually want a qbytearray as a value to a key inside your son file ?


                                  Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                                  Q: What's that?
                                  A: It's blue light.
                                  Q: What does it do?
                                  A: It turns blue.

                                  1 Reply Last reply
                                  0
                                  • M Offline
                                    M Offline
                                    Mikeeeeee
                                    wrote on last edited by
                                    #23

                                    So it turns QString into a picture to translate

                                    QString AppCore::getImage(QString addressFile)
                                    {
                                        QImage myImage(addressFile);
                                    
                                        QByteArray bArray;
                                        QBuffer buffer(&bArray);
                                        buffer.open(QIODevice::WriteOnly);
                                        myImage.save(&buffer, "JPEG");
                                    
                                        QString image("data:image/jpg;base64,");
                                        image.append(QString::fromLatin1(bArray.toBase64().data()));
                                    
                                        QImage testImage(image);
                                        qDebug()<<"myImage.size() : "<<myImage.size();
                                    
                                        return image;
                                    }
                                    
                                    1 Reply Last reply
                                    0

                                    • Login

                                    • Login or register to search.
                                    • First post
                                      Last post
                                    0
                                    • Categories
                                    • Recent
                                    • Tags
                                    • Popular
                                    • Users
                                    • Groups
                                    • Search
                                    • Get Qt Extensions
                                    • Unsolved