Details
-
Bug
-
Resolution: Unresolved
-
P3: Somewhat important
-
None
-
5.15.0
-
None
-
macOS 10.14.6, Windows 7 x64
Description
The following code does not seem to work correctly:
#include <QtWidgets> QByteArray QIconToQByteArray(const QIcon &icon) { QByteArray iconData; QDataStream iconDataStream(&iconData, QIODevice::WriteOnly); iconDataStream << icon; return iconData; } QIcon QByteArrayToQIcon(const QByteArray &iconData) { QIcon icon; QDataStream iconDataStream(iconData); iconDataStream >> icon; return icon; } int main(int argc, char *argv[]) { QApplication a(argc, argv); QIcon original = QFileIconProvider().icon(QString("main.cpp")); QIcon fixed; foreach(const QSize &size, original.availableSizes()) { fixed.addPixmap(original.pixmap(size)); } QIcon reconstitutedOriginal = QByteArrayToQIcon(QIconToQByteArray(original)); QIcon reconstitutedFixed = QByteArrayToQIcon(QIconToQByteArray(fixed)); QWidget *window = new QWidget; QPushButton *button1 = new QPushButton(original, QString(), window); QPushButton *button2 = new QPushButton(fixed, QString(), window); QPushButton *button3 = new QPushButton(reconstitutedOriginal, QString(), window); QPushButton *button4 = new QPushButton(reconstitutedFixed, QString(), window); button1->setIconSize(QSize(64, 64)); button2->setIconSize(QSize(64, 64)); button3->setIconSize(QSize(64, 64)); button4->setIconSize(QSize(64, 64)); button1->setGeometry(10, 10, 72, 72); button2->setGeometry(90, 10, 72, 72); button3->setGeometry(170, 10, 72, 72); button4->setGeometry(250, 10, 72, 72); window->resize(330, 88); window->show(); return a.exec(); }
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. Manually constructing a new QIcon with the same pixmaps does not have this issue. Other methods for constructing a QIcon that I've tested so far do not exhibit this problem either.
I've reproduced this issue in both macOS and Windows, using Qt 5.9.9, Qt 5.12.8 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.)