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. QPixmap::load has wrong about the image data.
Forum Updated to NodeBB v4.3 + New Features

QPixmap::load has wrong about the image data.

Scheduled Pinned Locked Moved Solved General and Desktop
6 Posts 4 Posters 6.3k Views 4 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.
  • joeQJ Offline
    joeQJ Offline
    joeQ
    wrote on last edited by joeQ
    #1

    I have one image file, filename is 72036.jpg, but when i use the bash command file 72036.jpg to see

    $ file 72036.jpg
    72036.jpg: PNG image data, 640 x 960, 8-bit/color RGB, non-interlaced
    

    Note file name is 72036.jpg but the image data is PNG.

    when I use the QPixmap::load(filename) the function return false;
    when I use the QPixmap::load(filename, "png") the function return true;

    I saw the QPixmap manual

    bool QPixmap::load(const QString &fileName, const char *format = Q_NULLPTR, Qt::ImageConversionFlags flags = Qt::AutoColor)
    Loads a pixmap from the file with the given fileName. Returns true if the pixmap was successfully loaded; otherwise invalidates the pixmap and returns false.
    The loader attempts to read the pixmap using the specified format. If the format is not specified (which is the default), the loader probes the file for a header to guess the file format.
    The file name can either refer to an actual file on disk or to one of the application's embedded resources. See the Resource System overview for details on how to embed pixmaps and other resource files in the application's executable.

    The loader attempts to read the pixmap using the specified format. If the format is not specified (which is the default), the loader probes the file for a header to guess the file format.

    I think Qt guess wrong!

    But, I Confused, when i saw the following result.

    void Fun()
    {
        /** some code at here  */
        QPixmap pixmap1,pixmap2;
        QString filename = "72036.jpg";
        if( !pixmap1.load( filename ) ) ///< return false
        {
        }
        if( !pixmap2.load( filename) )  ///< return false
        {
        }
    
        ...
    }
    
    
    void Fun1()
    {
        /** some code at here  */
        QPixmap pixmap1,pixmap2;
        QString filename = "72036.jpg";
        if( !pixmap1.load( filename, "png" ) ) ///< return true; I get it.
        {
        }
        if( !pixmap2.load( filename) )  ///< it also return true ?  I confused! Qt is intelligent ?
        {
        }
    
        ...
    }
    

    My Qt version is 5.7.0.

    About this case, and How to do ?

    Just do it!

    JKSHJ 1 Reply Last reply
    0
    • joeQJ joeQ

      I have one image file, filename is 72036.jpg, but when i use the bash command file 72036.jpg to see

      $ file 72036.jpg
      72036.jpg: PNG image data, 640 x 960, 8-bit/color RGB, non-interlaced
      

      Note file name is 72036.jpg but the image data is PNG.

      when I use the QPixmap::load(filename) the function return false;
      when I use the QPixmap::load(filename, "png") the function return true;

      I saw the QPixmap manual

      bool QPixmap::load(const QString &fileName, const char *format = Q_NULLPTR, Qt::ImageConversionFlags flags = Qt::AutoColor)
      Loads a pixmap from the file with the given fileName. Returns true if the pixmap was successfully loaded; otherwise invalidates the pixmap and returns false.
      The loader attempts to read the pixmap using the specified format. If the format is not specified (which is the default), the loader probes the file for a header to guess the file format.
      The file name can either refer to an actual file on disk or to one of the application's embedded resources. See the Resource System overview for details on how to embed pixmaps and other resource files in the application's executable.

      The loader attempts to read the pixmap using the specified format. If the format is not specified (which is the default), the loader probes the file for a header to guess the file format.

      I think Qt guess wrong!

      But, I Confused, when i saw the following result.

      void Fun()
      {
          /** some code at here  */
          QPixmap pixmap1,pixmap2;
          QString filename = "72036.jpg";
          if( !pixmap1.load( filename ) ) ///< return false
          {
          }
          if( !pixmap2.load( filename) )  ///< return false
          {
          }
      
          ...
      }
      
      
      void Fun1()
      {
          /** some code at here  */
          QPixmap pixmap1,pixmap2;
          QString filename = "72036.jpg";
          if( !pixmap1.load( filename, "png" ) ) ///< return true; I get it.
          {
          }
          if( !pixmap2.load( filename) )  ///< it also return true ?  I confused! Qt is intelligent ?
          {
          }
      
          ...
      }
      

      My Qt version is 5.7.0.

      About this case, and How to do ?

      JKSHJ Offline
      JKSHJ Offline
      JKSH
      Moderators
      wrote on last edited by JKSH
      #2

      @joeQ said in QPixmap::load has wrong about the image data.:

      I saw the QPixmap manual
      ...
      The loader attempts to read the pixmap using the specified format. If the format is not specified (which is the default), the loader probes the file for a header to guess the file format.

      I think Qt guess wrong!

      The documentation is wrong.

      See the Qt source code at https://code.woboq.org/qt5/qtbase/src/gui/image/qpixmap.cpp.html#_ZN7QPixmap4loadERK7QStringPKc6QFlagsIN2Qt19ImageConversionFlagEE

      If the format is not specified, Qt first uses the file extension to guess the format. Your extension is ".jpg", so Qt thinks it is a JPEG file.

      But, I Confused, when i saw the following result.

          QPixmap pixmap1,pixmap2;
          QString filename = "72036.jpg";
          if( !pixmap1.load( filename, "png" ) ) ///< return true; I get it.
          {
          }
          if( !pixmap2.load( filename) )  ///< it also return true ?  I confused! Qt is intelligent ?
          {
          }
      

      The data is cached.

      pixmap1 already loaded the file successfully, so pixmap2 doesn't need to re-load the file. Instead, pixmap2 shares the same data as pixmap1. You can also see this in the Qt source code.

      Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

      joeQJ kshegunovK 2 Replies Last reply
      3
      • JKSHJ JKSH

        @joeQ said in QPixmap::load has wrong about the image data.:

        I saw the QPixmap manual
        ...
        The loader attempts to read the pixmap using the specified format. If the format is not specified (which is the default), the loader probes the file for a header to guess the file format.

        I think Qt guess wrong!

        The documentation is wrong.

        See the Qt source code at https://code.woboq.org/qt5/qtbase/src/gui/image/qpixmap.cpp.html#_ZN7QPixmap4loadERK7QStringPKc6QFlagsIN2Qt19ImageConversionFlagEE

        If the format is not specified, Qt first uses the file extension to guess the format. Your extension is ".jpg", so Qt thinks it is a JPEG file.

        But, I Confused, when i saw the following result.

            QPixmap pixmap1,pixmap2;
            QString filename = "72036.jpg";
            if( !pixmap1.load( filename, "png" ) ) ///< return true; I get it.
            {
            }
            if( !pixmap2.load( filename) )  ///< it also return true ?  I confused! Qt is intelligent ?
            {
            }
        

        The data is cached.

        pixmap1 already loaded the file successfully, so pixmap2 doesn't need to re-load the file. Instead, pixmap2 shares the same data as pixmap1. You can also see this in the Qt source code.

        joeQJ Offline
        joeQJ Offline
        joeQ
        wrote on last edited by joeQ
        #3

        @JKSH First, Very very very Thank you.

        I want to used the following way to save my problem. but has some questions.

        QList<QByteArray> QImageReader::supportedImageFormats()

        qDebug:("bmp", "cur", "dds", "gif", "icns", "ico", "jpeg", "jpg", "pbm", "pgm", "png", "ppm", "svg", "svgz", "tga", "tif", "tiff", "wbmp", "webp", "xbm", "xpm")

         
        void Fun()
        {
            int i = 0;
            QPixmap pixmap;
            QString str, qsToolTip, qsFilePath="72036.jpg";
            QList<QByteArray> ltFmts;
        
            const char *pfmt;
            bool bloadOk = false;
        
            bloadOk = pixmap.load(qsFilePath,);
            if( !bloadOk )
            {
                ltFmts = QImageReader::supportedImageFormats(); ///< get the qt support image fotmat
                //qDebug() << ltFmts;
                i=0;
                do{
                    ///< try every image format
                    bloadOk = false;
                    pfmt = ltFmts.at(i).constData();
                    bloadOk = pixmap.load( qsFilePath, pfmt); ///< when pfmt = "icns", has the Assert
                    i++;
                }while(!bloadOk && i < ltFmts.count() );
            }
        
            if( !bloadOk )
            {
                QMessageBox::warning(NULL, tr(""), str, QMessageBox::Yes);
            }
        }
        
        

        Assert

        parseIconEntryInfo(): Failed, OSType doesn't match: "?PNG"
        ASSERT failure in QVector<T>::at: "index out of range", file c:\Users\qt\work\install\include/QtCore/qvector.h, line 425
        Invalid parameter passed to C runtime function.
        Invalid parameter passed to C runtime function.

        where is better way?

        Just do it!

        1 Reply Last reply
        0
        • JKSHJ JKSH

          @joeQ said in QPixmap::load has wrong about the image data.:

          I saw the QPixmap manual
          ...
          The loader attempts to read the pixmap using the specified format. If the format is not specified (which is the default), the loader probes the file for a header to guess the file format.

          I think Qt guess wrong!

          The documentation is wrong.

          See the Qt source code at https://code.woboq.org/qt5/qtbase/src/gui/image/qpixmap.cpp.html#_ZN7QPixmap4loadERK7QStringPKc6QFlagsIN2Qt19ImageConversionFlagEE

          If the format is not specified, Qt first uses the file extension to guess the format. Your extension is ".jpg", so Qt thinks it is a JPEG file.

          But, I Confused, when i saw the following result.

              QPixmap pixmap1,pixmap2;
              QString filename = "72036.jpg";
              if( !pixmap1.load( filename, "png" ) ) ///< return true; I get it.
              {
              }
              if( !pixmap2.load( filename) )  ///< it also return true ?  I confused! Qt is intelligent ?
              {
              }
          

          The data is cached.

          pixmap1 already loaded the file successfully, so pixmap2 doesn't need to re-load the file. Instead, pixmap2 shares the same data as pixmap1. You can also see this in the Qt source code.

          kshegunovK Offline
          kshegunovK Offline
          kshegunov
          Moderators
          wrote on last edited by kshegunov
          #4

          @JKSH said in QPixmap::load has wrong about the image data.:

          The documentation is wrong.

          We really need to fix this.

          Read and abide by the Qt Code of Conduct

          1 Reply Last reply
          0
          • G Offline
            G Offline
            Gerd
            wrote on last edited by
            #5

            Hi,
            you could load your pixmap this way:

               QPixmap p;
               QImageReader r(filename);
               r.setDecideFormatFromContent(true);
               QImage i = r.read();
               if (!i.isNull())
                  p = QPixmap::fromImage(i);
            
            
            joeQJ 1 Reply Last reply
            4
            • G Gerd

              Hi,
              you could load your pixmap this way:

                 QPixmap p;
                 QImageReader r(filename);
                 r.setDecideFormatFromContent(true);
                 QImage i = r.read();
                 if (!i.isNull())
                    p = QPixmap::fromImage(i);
              
              
              joeQJ Offline
              joeQJ Offline
              joeQ
              wrote on last edited by joeQ
              #6

              @Gerd @JKSH
              It is Ok! Thank you. Best wish for you.

              Just do it!

              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