QIcon::addFile() delay-load icons when no size is given

Delay-load the icon within QPixmapEngine::addFile() when no size is
given and the format supports it (e.g. png) - this speeds up QIcon
creation as the whole image is parsed later.

Fixes: QTBUG-59621
Change-Id: If165ca97c4b91f68a7d98f57de711390de060012
Reviewed-by: Volker Hilsheimer <volker.hilsheimer@qt.io>
(cherry picked from commit a843db6e2d80a99be38c2b66aac92cb912613bd6)
Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
This commit is contained in:
Christian Ehrlicher 2024-05-27 21:03:53 +02:00 committed by Qt Cherry-pick Bot
parent c8d8cfa333
commit c8fbdb5c38

View File

@ -40,6 +40,9 @@ public:
ImageReader(const QString &fileName) : m_reader(fileName), m_atEnd(false) { }
QByteArray format() const { return m_reader.format(); }
bool supportsReadSize() const { return m_reader.supportsOption(QImageIOHandler::Size); }
QSize size() const { return m_reader.size(); }
bool jumpToNextImage() { return m_reader.jumpToNextImage(); }
bool read(QImage *image)
{
@ -422,8 +425,14 @@ void QPixmapIconEngine::addFile(const QString &fileName, const QSize &size, QIco
QImage image;
if (format != "ico") {
if (ignoreSize) { // No size specified: Add all images.
while (imageReader.read(&image))
pixmaps += QPixmapIconEngineEntry(abs, image, mode, state);
if (imageReader.supportsReadSize()) {
do {
pixmaps += QPixmapIconEngineEntry(abs, imageReader.size(), mode, state);
} while (imageReader.jumpToNextImage());
} else {
while (imageReader.read(&image))
pixmaps += QPixmapIconEngineEntry(abs, image, mode, state);
}
} else {
// Try to match size. If that fails, add a placeholder with the filename and empty pixmap for the size.
while (imageReader.read(&image) && image.size() != size) {}