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. Issues with loadFromData function in QImage
QtWS25 Last Chance

Issues with loadFromData function in QImage

Scheduled Pinned Locked Moved Solved General and Desktop
qimageqpixmaptaglibimageissue
14 Posts 5 Posters 2.0k 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.
  • N Offline
    N Offline
    Nick Redwill
    wrote on 2 Jan 2022, 19:28 last edited by Nick Redwill 1 Feb 2022, 19:31
    #1

    Hello, programmers!

    I am using taglib to read music file cover image and that load it into QImage. Here is my code:

    QImage Widget::getCover (QString filename) {
        QImage cover;
    
        MPEG::File * f = new MPEG::File(filename.toStdWString().c_str());
        ID3v2::Tag * id3v2tag = f->ID3v2Tag();
        auto frameList = id3v2tag->frameList("APIC");
    
        if (id3v2tag && !frameList.isEmpty())
        {
            TagLib::ID3v2::AttachedPictureFrame * frame =
                    static_cast<TagLib::ID3v2::AttachedPictureFrame *>(frameList.front());
    
            qDebug() << "Data: " << frame->picture().size();
            if (cover.loadFromData((const uchar *)frame->picture().data(), frame->picture().size())) {
                qDebug() << "Image loaded!";
            }
    
            qDebug() << cover.size();
        }
        else
        {
            qDebug() << "id3v2 not present";
        }
    
        delete f;
    
        return cover;
    }
    

    Anything works fine (only with MP3 files). But when i put this code into another project, it stops working. I checked that all the code runs perfectly, the data from the file is read without problems and has the same content. Only the loadFromData function does not work as expected and returns false.

    What could be the problem? Thanks in advance! (Sorry for bad english)

    K 1 Reply Last reply 2 Jan 2022, 19:53
    0
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 2 Jan 2022, 19:53 last edited by
      #2

      Hi and welcome to devnet,

      One thing you could do is check that you get the same data with both your application.

      Another thing I would do is call picture() only once, store the returned value and work with it rather than call it multiple times. Two successive calls are not guaranteed to return the same object.

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

      N 1 Reply Last reply 2 Jan 2022, 20:24
      1
      • N Nick Redwill
        2 Jan 2022, 19:28

        Hello, programmers!

        I am using taglib to read music file cover image and that load it into QImage. Here is my code:

        QImage Widget::getCover (QString filename) {
            QImage cover;
        
            MPEG::File * f = new MPEG::File(filename.toStdWString().c_str());
            ID3v2::Tag * id3v2tag = f->ID3v2Tag();
            auto frameList = id3v2tag->frameList("APIC");
        
            if (id3v2tag && !frameList.isEmpty())
            {
                TagLib::ID3v2::AttachedPictureFrame * frame =
                        static_cast<TagLib::ID3v2::AttachedPictureFrame *>(frameList.front());
        
                qDebug() << "Data: " << frame->picture().size();
                if (cover.loadFromData((const uchar *)frame->picture().data(), frame->picture().size())) {
                    qDebug() << "Image loaded!";
                }
        
                qDebug() << cover.size();
            }
            else
            {
                qDebug() << "id3v2 not present";
            }
        
            delete f;
        
            return cover;
        }
        

        Anything works fine (only with MP3 files). But when i put this code into another project, it stops working. I checked that all the code runs perfectly, the data from the file is read without problems and has the same content. Only the loadFromData function does not work as expected and returns false.

        What could be the problem? Thanks in advance! (Sorry for bad english)

        K Offline
        K Offline
        kshegunov
        Moderators
        wrote on 2 Jan 2022, 19:53 last edited by kshegunov 1 Feb 2022, 20:11
        #3

        @Nick-Redwill said in Issues with loadFromData function in QImage:

        filename.toStdWString().c_str()
        

        This doesn't look quite right.

        PS.

        Nor does this:
        frame->picture().data()

        Read and abide by the Qt Code of Conduct

        N 1 Reply Last reply 2 Jan 2022, 20:37
        2
        • S SGaist
          2 Jan 2022, 19:53

          Hi and welcome to devnet,

          One thing you could do is check that you get the same data with both your application.

          Another thing I would do is call picture() only once, store the returned value and work with it rather than call it multiple times. Two successive calls are not guaranteed to return the same object.

          N Offline
          N Offline
          Nick Redwill
          wrote on 2 Jan 2022, 20:24 last edited by
          #4

          @SGaist I checked, in both applications data and its size absolutely the same. Also, as i already said, in the first app all works fine, meanwhile in second project same code doesn't work. Debugging shows me, that problem in loadFromData function.

          1 Reply Last reply
          0
          • K kshegunov
            2 Jan 2022, 19:53

            @Nick-Redwill said in Issues with loadFromData function in QImage:

            filename.toStdWString().c_str()
            

            This doesn't look quite right.

            PS.

            Nor does this:
            frame->picture().data()

            N Offline
            N Offline
            Nick Redwill
            wrote on 2 Jan 2022, 20:37 last edited by
            #5

            @kshegunov said in Issues with loadFromData function in QImage:

            This doesn't look quite right.

            This code works fine. Its just convert qstring to wstring and then to wchar_t *. This part works identical in both applications.

            The problem occurs on the line with loadFromData function.

            K 1 Reply Last reply 2 Jan 2022, 20:41
            0
            • N Nick Redwill
              2 Jan 2022, 20:37

              @kshegunov said in Issues with loadFromData function in QImage:

              This doesn't look quite right.

              This code works fine. Its just convert qstring to wstring and then to wchar_t *. This part works identical in both applications.

              The problem occurs on the line with loadFromData function.

              K Offline
              K Offline
              kshegunov
              Moderators
              wrote on 2 Jan 2022, 20:41 last edited by
              #6

              @Nick-Redwill said in Issues with loadFromData function in QImage:

              Its just convert qstring to wstring and then to wchar_t *

              No it's not. After the conversion from QString to wstring you're taking a pointer to a temporary.

              Read and abide by the Qt Code of Conduct

              N 1 Reply Last reply 2 Jan 2022, 21:07
              2
              • K kshegunov
                2 Jan 2022, 20:41

                @Nick-Redwill said in Issues with loadFromData function in QImage:

                Its just convert qstring to wstring and then to wchar_t *

                No it's not. After the conversion from QString to wstring you're taking a pointer to a temporary.

                N Offline
                N Offline
                Nick Redwill
                wrote on 2 Jan 2022, 21:07 last edited by
                #7

                @kshegunov said in Issues with loadFromData function in QImage:

                No it's not. After the conversion from QString to wstring you're taking a pointer to a temporary.

                Okay, I'll fix this later. It doesn't matter now. The problem is in another part of the code.

                1 Reply Last reply
                0
                • N Offline
                  N Offline
                  Nick Redwill
                  wrote on 2 Jan 2022, 21:48 last edited by Nick Redwill 1 Feb 2022, 21:50
                  #8

                  I also noticed, that in application where code doesn't work occurs warning:

                  libpng warning: ICCP: known incorrect sRGB profile
                  

                  Its occurs right after loadFromData function call.

                  J 1 Reply Last reply 2 Jan 2022, 21:53
                  0
                  • N Nick Redwill
                    2 Jan 2022, 21:48

                    I also noticed, that in application where code doesn't work occurs warning:

                    libpng warning: ICCP: known incorrect sRGB profile
                    

                    Its occurs right after loadFromData function call.

                    J Offline
                    J Offline
                    JonB
                    wrote on 2 Jan 2022, 21:53 last edited by
                    #9

                    @Nick-Redwill Google libpng warning: ICCP: known incorrect sRGB profile

                    1 Reply Last reply
                    1
                    • N Offline
                      N Offline
                      Nick Redwill
                      wrote on 3 Jan 2022, 15:27 last edited by
                      #10
                      QByteArray bytes (pic.data(), pic.size());
                      QFile file ("test.png");
                      file.open(QIODevice::WriteOnly);
                      file.write(bytes);
                      file.close();
                      
                      if (coverImg.load("test.png")) {
                           qDebug() << "Image loaded!";
                      } else {
                           qDebug() << "Error image!";
                      }
                      

                      I changed the code to this and it also doesn't work in one project and works in another. Moreover, the file in both projects is created correctly (i can open it using the windows image viewer). The libpng warning is still displayed.

                      C 1 Reply Last reply 3 Jan 2022, 17:07
                      0
                      • N Offline
                        N Offline
                        Nick Redwill
                        wrote on 3 Jan 2022, 15:44 last edited by
                        #11

                        Why does QImage behave differently in different projects?

                        1 Reply Last reply
                        0
                        • N Nick Redwill
                          3 Jan 2022, 15:27
                          QByteArray bytes (pic.data(), pic.size());
                          QFile file ("test.png");
                          file.open(QIODevice::WriteOnly);
                          file.write(bytes);
                          file.close();
                          
                          if (coverImg.load("test.png")) {
                               qDebug() << "Image loaded!";
                          } else {
                               qDebug() << "Error image!";
                          }
                          

                          I changed the code to this and it also doesn't work in one project and works in another. Moreover, the file in both projects is created correctly (i can open it using the windows image viewer). The libpng warning is still displayed.

                          C Offline
                          C Offline
                          Christian Ehrlicher
                          Lifetime Qt Champion
                          wrote on 3 Jan 2022, 17:07 last edited by
                          #12

                          @Nick-Redwill said in Issues with loadFromData function in QImage:

                          I changed the code to this and it also doesn't work in one project and works in another.

                          What means 'does not work'.
                          Please proper error description...

                          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
                          • N Offline
                            N Offline
                            Nick Redwill
                            wrote on 3 Jan 2022, 18:18 last edited by
                            #13

                            I solved the problem! I just needed to add the imageformats folder with dll files to the exe file folder.

                            C 1 Reply Last reply 3 Jan 2022, 18:21
                            0
                            • N Nick Redwill
                              3 Jan 2022, 18:18

                              I solved the problem! I just needed to add the imageformats folder with dll files to the exe file folder.

                              C Offline
                              C Offline
                              Christian Ehrlicher
                              Lifetime Qt Champion
                              wrote on 3 Jan 2022, 18:21 last edited by
                              #14

                              @Nick-Redwill This is what windeployqt is for...

                              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
                              2

                              5/14

                              2 Jan 2022, 20:37

                              9 unread
                              • Login

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