From 2cfe7199a2c84371ef02e3ddd6f841f28fb2776f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thorbj=C3=B8rn=20Lindeijer?= Date: Tue, 23 Jan 2024 16:04:49 +0100 Subject: [PATCH] QGtk3Theme: Fix QGtk3Interface::fileIcon By failing to set the G_FILE_ATTRIBUTE_STANDARD_ICON attribute, the "icon" returned by g_file_info_get_icon was always null and a GLib-GIO-CRITICAL warning was output to the console (at least since glib 2.76.0)[1]. After adding the necessary attribute, the code was crashing, because now a valid icon was returned, however the icon should not be freed[2], which is why I removed the "g_object_unref(icon)". Now it was no longer crashing, but the size of the icons was off. It was passing GTK_ICON_SIZE_BUTTON (4) to gtk_icon_theme_lookup_by_gicon where a size in pixels was expected. I chose 16 because that's the pixel size associated with GTK_ICON_SIZE_BUTTON[3]. Finally I noticed the returned icons had the wrong color. It seems that a GdkPixbuf uses RGBA8888 format[4]. Adding an explicit conversion to ARGB32 made the icons look correct for me. [1] https://gitlab.gnome.org/GNOME/glib/-/commit/ed8e86a7d41a0900d8fa57edc64264d04cf8135b [2] https://docs.gtk.org/gio/method.FileInfo.get_icon.html [3] https://docs.gtk.org/gtk3/enum.IconSize.html#button [4] https://docs.gtk.org/gdk-pixbuf/class.Pixbuf.html#image-data [ChangeLog][Platform Specific Changes][Linux] Fixed file icons provided by QFileIconProvider when using the gtk3 platform theme. Pick-to: 6.6 6.5 Change-Id: I312ef76ac28bc28435b756d299998485db122906 Reviewed-by: Axel Spoerl (cherry picked from commit 277d77029d7fe8f46c6ee101869dcff389426cb1) Reviewed-by: Qt Cherry-pick Bot --- src/plugins/platformthemes/gtk3/qgtk3interface.cpp | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/plugins/platformthemes/gtk3/qgtk3interface.cpp b/src/plugins/platformthemes/gtk3/qgtk3interface.cpp index 33d01825211..a35e211fbf7 100644 --- a/src/plugins/platformthemes/gtk3/qgtk3interface.cpp +++ b/src/plugins/platformthemes/gtk3/qgtk3interface.cpp @@ -296,8 +296,10 @@ QImage QGtk3Interface::qt_convert_gdk_pixbuf(GdkPixbuf *buf) const const int width = gdk_pixbuf_get_width(buf); const int height = gdk_pixbuf_get_height(buf); const int bpl = gdk_pixbuf_get_rowstride(buf); - QImage converted(data, width, height, bpl, QImage::Format_ARGB32); - return converted.copy(); // detatch to survive lifetime of buf + QImage converted(data, width, height, bpl, QImage::Format_RGBA8888); + + // convert to more optimal format and detach to survive lifetime of buf + return converted.convertToFormat(QImage::Format_ARGB32_Premultiplied); } /*! @@ -666,7 +668,7 @@ QIcon QGtk3Interface::fileIcon(const QFileInfo &fileInfo) const if (!file) return QIcon(); - GFileInfo *info = g_file_query_info (file, G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE, + GFileInfo *info = g_file_query_info (file, G_FILE_ATTRIBUTE_STANDARD_ICON, G_FILE_QUERY_INFO_NONE, nullptr, nullptr); if (!info) { g_object_unref(file); @@ -681,12 +683,11 @@ QIcon QGtk3Interface::fileIcon(const QFileInfo &fileInfo) const } GtkIconTheme *theme = gtk_icon_theme_get_default(); - GtkIconInfo *iconInfo = gtk_icon_theme_lookup_by_gicon(theme, icon, GTK_ICON_SIZE_BUTTON, + GtkIconInfo *iconInfo = gtk_icon_theme_lookup_by_gicon(theme, icon, 16, GTK_ICON_LOOKUP_FORCE_SIZE); if (!iconInfo) { g_object_unref(file); g_object_unref(info); - g_object_unref(icon); return QIcon(); } @@ -694,7 +695,6 @@ QIcon QGtk3Interface::fileIcon(const QFileInfo &fileInfo) const QImage image = qt_convert_gdk_pixbuf(buf); g_object_unref(file); g_object_unref(info); - g_object_unref(icon); g_object_unref(buf); return QIcon(QPixmap::fromImage(image)); }