Uploaded image for project: 'Qt'
  1. Qt
  2. QTBUG-138530

QImageReader could not determine the size of certain image formats

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Unresolved
    • Icon: P2: Important P2: Important
    • None
    • 6.9.0, 6.9.1
    • Image formats
    • None
    • All

      QImageReader couldn't get the picture's size of certain format.
      Sample Code:
      [main.cpp]

      #include <QApplication>
      #include <QFileDialog>
      #include <QString>
      #include <QByteArray>
      #include <QStringList>
      #include <QImageReader>
      #include <QMessageBox>
      #include <QPixmap>
      
      using namespace Qt::Literals::StringLiterals;
      
      int main(int argc, char *argv[])
      {
          QApplication a(argc, argv);
          
          QStringList mimeTypeFilters;
          const QByteArrayList supportedMimeTypes{ QImageReader::supportedMimeTypes() };
          for (const QByteArray &mimeTypeName : supportedMimeTypes)
              mimeTypeFilters.append(mimeTypeName);
      
          mimeTypeFilters.sort();
      
          QFileDialog fdlg;
          QImageReader imgReader;
          
          fdlg.setWindowTitle(u"Open a picture..."_s);
          fdlg.setDirectory(u"."_s);
          fdlg.setMimeTypeFilters(mimeTypeFilters);
          fdlg.setFileMode(QFileDialog::ExistingFile);
          fdlg.exec();
          
          imgReader.setFileName(fdlg.selectedFiles()[0]);
          imgReader.setAutoTransform(true);
      
          if (imgReader.canRead()) {
              QPixmap pixmap{ QPixmap::fromImageReader(&imgReader) };    // If this is added, the result will be incorrect.
              QMessageBox::information(nullptr, u"..."_s, 
                                       u"Width: "_s + QString::number(imgReader.size().width()) + u", Height: "_s + QString::number(imgReader.size().height()),
                                       QMessageBox::Close);
          }
                                           
          return a.exec();
      }
      

      I have found PNG and BMP handlers have that problem. In detail, we cannot do reading operations before calling size(). If I call size() immediately after canRead(), the result is correct; if I have called read() or sth like QPixmap::fromImageReader() before the call to size(), the result is invalid. However for other handlers like JPG, there's no such restriction.

        No reviews matched the request. Check your Options in the drop-down menu of this sections header.

            sgaist Samuel Gaist
            zxunge Grit Clef
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:

                There is 1 open Gerrit change