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. [SOLVED] How to show image from byte array ?
QtWS25 Last Chance

[SOLVED] How to show image from byte array ?

Scheduled Pinned Locked Moved General and Desktop
8 Posts 3 Posters 16.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.
  • L Offline
    L Offline
    looterhunter
    wrote on last edited by looterhunter
    #1

    hi everyone ,
    I want to get byte data from Textedit, and convert to JPG format to show image on Label
    (or another one can recommend me what component is good show image ?)

    My GUI interface like below:
    GUI

    My code like this,

    
    void MainWindow::on_pushButton_4_clicked()
    {
        QString txt = ui->textEdit->toPlainText();
        QByteArray *data = new QByteArray();
        *data = txt.toUtf8();
        QPixmap *mpixmap = new QPixmap();
        mpixmap->loadFromData(*data,"JPG");
    
        ui->label->setPixmap(*mpixmap);
        //ui->label->show();
    
    }
    

    what's going on, why my code not to show image , where's wrong?
    thank you in advance
    Best regards

    1 Reply Last reply
    0
    • jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Why do you use so many pointers?
      Try it like this:

      QByteArray data = txt.toUtf8();
      QPixmap mpixmap;
      mpixmap.loadFromData(data,"JPG");
      ui->label->setPixmap(mpixmap);
      

      And check what mpixmap.loadFromData(data,"JPG"); returns. If it returns false then it could not load your data.

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

      1 Reply Last reply
      0
      • jsulmJ Offline
        jsulmJ Offline
        jsulm
        Lifetime Qt Champion
        wrote on last edited by
        #3

        One more note: what you have in your text edit is text! Even if you call toUtf8() it is still text represented as byte array. What you need to do is to convert the hex-codes in your text editor into bytes. For example: FF -> is a byte with value 255

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

        L 1 Reply Last reply
        0
        • jsulmJ jsulm

          One more note: what you have in your text edit is text! Even if you call toUtf8() it is still text represented as byte array. What you need to do is to convert the hex-codes in your text editor into bytes. For example: FF -> is a byte with value 255

          L Offline
          L Offline
          looterhunter
          wrote on last edited by
          #4

          @jsulm
          hi dear jsulm,
          it's still not show up image, I think you are right , it just a string not a hex.

          the single string to hex is below:

          QString str= "FF";
          bool ok= false;
          uint nHex = sValue.toUInt(&ok,16);
          

          but how i do it , convert string(array) to hex(array).
          I think the QPixmap::loadFromData need binary format data to show up image.

          1 Reply Last reply
          0
          • jsulmJ Offline
            jsulmJ Offline
            jsulm
            Lifetime Qt Champion
            wrote on last edited by
            #5

            You should do something like this (from documentation):

            QByteArray ba;
            ba.resize(5);
            ba[0] = 0x3c;
            ba[1] = 0xb8;
            ba[2] = 0x64;
            ba[3] = 0x18;
            ba[4] = 0xca;
            

            So, you iterate over the string, take always two characters and convert them from hex to quint8 and put that quint8 into the byte array.

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

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

              Hi and welcome to devnet,

              Out of curiosity, how do you know that your data in your textEdit is a valid JPEG image ?

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

              L 1 Reply Last reply
              0
              • jsulmJ jsulm

                You should do something like this (from documentation):

                QByteArray ba;
                ba.resize(5);
                ba[0] = 0x3c;
                ba[1] = 0xb8;
                ba[2] = 0x64;
                ba[3] = 0x18;
                ba[4] = 0xca;
                

                So, you iterate over the string, take always two characters and convert them from hex to quint8 and put that quint8 into the byte array.

                L Offline
                L Offline
                looterhunter
                wrote on last edited by
                #7

                @jsulm
                hi dear jsulm,
                I finished it, it can work now.
                your tips is help me a lot

                thank you vary much a lot :)

                the flow is below :
                step 1: get string from textedit
                step 2: change QString to QByteArray
                step 3: transform QByteArray string data to hex data
                step 4: using QPixmap component , and put hex data into the QPixmap
                step 5: show up on label compoent

                1 Reply Last reply
                0
                • SGaistS SGaist

                  Hi and welcome to devnet,

                  Out of curiosity, how do you know that your data in your textEdit is a valid JPEG image ?

                  L Offline
                  L Offline
                  looterhunter
                  wrote on last edited by
                  #8

                  @SGaist
                  I hava rs232 camera , I just copy data form serial port terminal
                  copy data , and paste to textedit

                  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