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

Gordon Schumacher whiplash at pobox.com
Wed Mar 24 16:15:07 CET 2010


On 3/24/2010 3:57 AM, Oliver Knoll <Oliver.Knoll at comit.ch> wrote:
> And does it really have to be the icon of the application which can
> deal with that MIME type? Or would "generic" icons be sufficient?
> Note that on Windows the "application registration" is really based on the extension, such as *.jpg, so in theory there could be dozens of applications on a computer which could deal with e.g. MIME "image/jpeg", but only one which is actually registered to open *.jpg files (e.g. the Windows Image Preview). So just having the MIME type wouldn't help you anyway, if you really wanted to have the (or all) icon(s) of the registered application(s).
>   

Ah, if only it were that simple...!

Having spent some time in Explorer shell land, let me say that just
about any task that you think should be easy is fraught with unexpected
gotchas...

That said, I will offer you this code:

#include <shlwapi.h>

void getIcon(TCHAR* myFileName)
{
   ITEMIDLIST* destPidl = NULL;
   SFGAOF      bogus;
   // Look at SHGetFileInfo
(http://msdn.microsoft.com/en-us/library/bb762179%28VS.85%29.aspx)
   // for more information on these flags
   DWORD       attrs = FILE_ATTRIBUTE_NORMAL;
   UINT        flags = SHGFI_OPENICON | SHGFI_SYSICONINDEX |
SHGFI_SHELLICONSIZE;
   SHFILEINFO  sfi = {0};

   // SHParseDisplayName is at:
http://msdn.microsoft.com/en-us/library/bb762236%28VS.85%29.aspx
   HRESULT hr = SHParseDisplayName(myFileName, NULL, &destPidl, 0, &bogus);
   if (SUCCEEDED(hr))
   {
      flags |= SHGFI_PIDL;
      SHGetFileInfo((LPCTSTR) destPidl, attrs, &sfi, sizeof(sfi), flags);
   }
   else
   {
      flags |= SHGFI_USEFILEATTRIBUTES;
      // Do whatever you need to do here to determine this...
      if (thisFileNameIsADirectory)
         attrs |= FILE_ATTRIBUTE_DIRECTORY;
      SHGetFileInfo(myFileName, attrs, &sfi, sizeof(sfi), flags);
   }
}





More information about the Qt-interest-old mailing list