Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Solved QIcons from QFileIconProvider can't be written and read from a stream

    General and Desktop
    3
    5
    118
    Loading More Posts
    • 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.
    • Guy Gizmo
      Guy Gizmo last edited by

      Unless I'm missing something, the following code does not seem to work correctly:

      #include <QDebug>
      #include <QApplication>
      #include <QIcon>
      #include <QFileIconProvider>
      #include <QByteArray>
      #include <QDataStream>
      #include <QFileInfo>
      
      static QByteArray QIconToQByteArray(const QIcon &icon) {
          QByteArray iconData;
          QDataStream iconDataStream(&iconData, QIODevice::WriteOnly);
          iconDataStream << icon;
          return iconData;
      }
      
      int main(int argc, char *argv[]) {
          QApplication a(argc, argv);
          QIcon original = QFileIconProvider().icon(QFileInfo("/System/Library/CoreServices/Finder.app"));
          QIcon fixed;
          
          foreach(const QSize &size, original.availableSizes()) {
              fixed.addPixmap(original.pixmap(size));
          }
          
          qDebug() << "original          :" << original;
          qDebug() << "original converted:" << QIconToQByteArray(original).size();
          qDebug() << "fixed converted   :" << QIconToQByteArray(fixed).size();
          return 0;
      }
      
      

      When I run this on macOS, the output I get is:

      original          : QIcon(availableSizes[normal,Off]=(QSize(32, 32), QSize(64, 64), QSize(128, 128), QSize(256, 256), QSize(512, 512)),cacheKey=0x100000000)
      original converted: 42
      fixed converted   : 123141
      

      Note that once original is converted to a QByteArray, that QByteArray does not have a correct size. It is in fact not properly converting original to a QByteArray at all.

      However, this is not the case for fixed. For whatever reason, manually constructing a new QIcon with the same pixmaps does not have this issue. Other methods for constructing a QIcon do not exhibit this problem either.

      I've reproduced this issue in both macOS and Windows, using both Qt 5.9.9 and Qt 5.15.0. (To run this code in Windows, simply replace the path passed into the QFileInfo constructor with the path of any file on disk.)

      Is this a bug with QFileIconProvider? Or is there something I'm missing about the QIcon it returns, and there's something I need to do to be able to convert it to and from a QByteArray?

      1 Reply Last reply Reply Quote 0
      • B
        Bonnie last edited by Bonnie

        Yes, I think it seems to be a bug of the file icon engine in qpa plugin.

        1 Reply Last reply Reply Quote 1
        • Guy Gizmo
          Guy Gizmo last edited by

          I've submitted a bug report. Fortunately I've already found a workaround for the issue.

          Christian Ehrlicher 1 Reply Last reply Reply Quote 0
          • Christian Ehrlicher
            Christian Ehrlicher Lifetime Qt Champion @Guy Gizmo last edited by

            @Guy-Gizmo said in QIcons from QFileIconProvider can't be written and read from a stream:

            I've submitted a bug report.

            Please post the bug number here for reference.

            Qt has to stay free or it will die.

            Guy Gizmo 1 Reply Last reply Reply Quote 0
            • Guy Gizmo
              Guy Gizmo @Christian Ehrlicher last edited by

              @Christian-Ehrlicher https://bugreports.qt.io/browse/QTBUG-84743

              1 Reply Last reply Reply Quote 1
              • First post
                Last post