[Qt-interest] How to get an icon associated with a certain filetype using Qt?

Robert Hairgrove evorgriahr at hispeed.ch
Fri Mar 26 19:49:34 CET 2010


Oliver.Knoll at comit.ch wrote:
> Nikos Chantziaras wrote on Friday, March 26, 2010 6:13 PM:
>> The best that can be done is query the OS using the native API.
> 
> You are right, that might actually work on Linux (Unix) in case you are running Gnome or KDE. Indeed they seem to associate MIME types with applications, see http://en.wikipedia.org/wiki/File_association - see also http://ubuntuforums.org/showthread.php?t=383040 - it mentions "sudo update-mime-database /usr/share/mime/", so it seems that's where the MIME associations are stored.
> 
> But on Windows and Mac that would most likely not work, because as already mentioned Windows uses the file extension and Mac "type codes and creator codes". According to Wikipedia "more recent" versions (Mac OS X 10.x?) also support file extensions. Well, and maybe also MIME type: "Application bundles in OS X declare supported file types in their Info.plist file. [...] For example, the JPEG type is defined with a UTI of 'public.jpeg', and tagged with the extensions 'jpg' and 'jpeg', the MIME typ
> e 'image/jpeg' and the type code 'JPEG'."
> 
> So either make it platform-dependent with the proper API code (Windows?) or use "generic icons", in case you just have to deal with a few dozen MIME types.

Since KDE 4.x, both KDE and Gnome support the MIME functionality of 
freedesktop.org. Although there are other window managers available for 
Linux, I believe that nowadays 99% of those who use a GUI window manager 
at all either use KDE or Gnome.

After looking at the source code for QFileIconProvider, it seems that Qt 
does indeed handle the different platforms if you pass a file name to 
QFileInfo. Even if the file doesn't exist, the code does the "right 
thing" depending on the file name extension.

And since the file name is usually available, or can be faked (e.g. 
"xyz.jpg" should be sufficient to find the appropriate icon for JPEG 
images), I think it will work OK. Have to test this as the next step.

Thanks for your input, everyone!



More information about the Qt-interest-old mailing list