Qt6 topics merged with General

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

  • 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()) {
        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?

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

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

  • Lifetime Qt Champion

    @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.

Log in to reply