Results 1 to 16 of 16

Thread: Extracting larger icons

  1. #1
    Join Date
    Apr 2010
    Posts
    23
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Question Extracting larger icons

    Hi everyone,

    I'm trying to extract program icons out of exe-files. This works using the following Qt function with the path of my exe-file saved in filePath:

    Qt Code:
    1. QFileInfo *info = new QFileInfo(filePath);
    2. image = new QImage(provider->icon(*info).pixmap(128,128).toImage());
    To copy to clipboard, switch view to plain text mode 

    My problem is, that this function only extracts the smallest iocn the file containst, usually 32 x 32 pixels. Most of todays software provides icons in higher resolution up to 256 x 256, but I can't see a way to set this neither in QFileInfo nor in QFileIconProvider. I found another possibility using the windows API with the function ExtractIconEx:

    Qt Code:
    1. HICON smallIcon, bigIcon;
    2. WCHAR path;
    3. filePath.toWCharArray(&path);
    4. ExtractIconEx(&path,0,&smallIcon,&bigIcon,1);
    5. image = new QImage(QPixmap::fromWinHICON(bigIcon).toImage());
    To copy to clipboard, switch view to plain text mode 

    The compiler does't show any errors, but the QImage keeps empty. I hope anybody could help me with extracting larger program-icons out of exe-file. I've tried it in another forum before, but I didn't get any answer at all, so QtCenter appears to have much more promise.

  2. #2
    Join Date
    Apr 2010
    Posts
    23
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Extracting larger icons

    Huh, no answers either...

    I still believe, that theres a way to do this, and I hope that theres somebody out there who knows the way, please don't fail me. Just a hint how it could possibly work, just so that I can go on trying.

  3. #3
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Extracting larger icons

    I don't know about a Qt way (it seems to only return a single icon, either the small or the big depending on which is available), but on Windows you can get the icon via SHGetFileInfo() and then use the convertHIconToPixmap function in qpixmap_win.cpp (which has a big fat warning at the top of the file, so you need to either copy the code and be GPL licensed or write your own version).

  4. #4
    Join Date
    Apr 2010
    Posts
    23
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Extracting larger icons

    Thanks, that's worth a try! I'll come back as soon as I had time to try it.

  5. #5
    Join Date
    Apr 2010
    Posts
    23
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Extracting larger icons

    I've now tried using SHGetFileInfo() the following way:

    Qt Code:
    1. LPCTSTR path = (LPCTSTR)filePath.toStdString().c_str();
    2. SHFILEINFO fileInfo = {0};
    3. SHGetFileInfo(path,-1,&fileInfo,sizeof(fileInfo),SHGFI_ICON | SHGFI_LARGEICON);
    4.  
    5. *pixmap = QPixmap::fromWinHICON(fileInfo.hIcon);
    To copy to clipboard, switch view to plain text mode 

    That's the way I would think it should work, but it doesn't. The code onse again compiles error-less but as soon as debugger runs through the function I get SIGSEGV, operating system signal inside of QPixmap::fromHIcon().
    I didn't quite get, what you meant with
    so you need to either copy the code and be GPL licensed or write your own version
    . Any further advice?

    Thanks a lot!

  6. #6
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Extracting larger icons

    Since you are using Qt you should be passing a unicode string to SHGetFileInfo, not a LPCTSTR:
    Qt Code:
    1. SHGetFileInfo(QDir::toNativeSeparators(fileName).utf16(), ...
    To copy to clipboard, switch view to plain text mode 
    Secondly, I prefer to grab a handle to the system icon list and then use SYSICONINDEX param to SHGetFileInfo(), but it's your choice.

  7. #7
    Join Date
    Apr 2010
    Posts
    23
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Extracting larger icons

    Thanks for your reply, but the code you suggested doesn't seem to work. SHGetFileInfo(), as MSDN says, requires LPCTSTR/ const char*, but your code produces const ushort*:
    Qt Code:
    1. QDir::toNativeSeparators(filePath).utf16()
    To copy to clipboard, switch view to plain text mode 
    These are not compatible and a direct cast won't work either. I really don't know what to do, as just about any modern application wich does a such task is capable of extracting larger icons, I'd never thought that it could be that difficult.

  8. #8
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Extracting larger icons

    If you are using Qt, then you will be using the Unicode libraries, so SHGetFileInfo will be aliased to SHGetFileInfoW, which although the prototype will be LPCTSTR, you can (and should) use a Unicode string provided by utf16()

    Let me dig out an example from one of my apps... [structure and error checking removed]

    Qt Code:
    1. HIMAGELIST ImageList;
    2. HICON WinIcon;
    3.  
    4. ImageList = (HIMAGELIST) SHGetFileInfo((const WCHAR *)QDir::toNativeSeparators(ee->filePath).utf16(), 0, &FileInfo, sizeof(SHFILEINFO), SHGFI_LARGEICON|SHGFI_SYSICONINDEX);
    5.  
    6. WinIcon = ImageList_GetIcon(ImageList, FileInfo.iIcon, ILD_NORMAL);
    To copy to clipboard, switch view to plain text mode 
    Last edited by squidge; 6th May 2010 at 08:18.

  9. The following user says thank you to squidge for this useful post:

    MisterIKS (6th May 2010)

  10. #9
    Join Date
    Apr 2010
    Posts
    23
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Extracting larger icons

    Thank you so much for that example, I'm confident of it to work fine, but sorry for getting on your nerves, but what do I need to include in order to get this to work? I Included "windows.h" which works fine for SHFILEINFO and HICON but doesn't provide HIMAGELIST.I've googled for it and the second result was this thread itself.

    I'm looking forward to finally getting this to work after endless weeks of trying Once again, thank you!

  11. #10
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Extracting larger icons

    Try CommCtrl.h

  12. #11
    Join Date
    Apr 2010
    Posts
    23
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Extracting larger icons

    The output window throughs out the following error:
    undefined reference to `ImageList_GetIcon@12`
    collect2: ld returned 1 exit status
    I've neither seen the first line, nor the second line in this context. "windows.h" and "commctrl.h" are included, I can't see a solution...

  13. #12
    Join Date
    May 2010
    Location
    Germany
    Posts
    3
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Extracting larger icons

    Hi, I am having the same problem. But I know that the WinAPI Method
    Qt Code:
    1. SHGetImageList()
    To copy to clipboard, switch view to plain text mode 
    could do the job. The problem I have is that SHGetImageList is not found in my QtCreator although I include "shellapi.h" which is required according to msdn. Does anybody has some hints for me to get this function working?

  14. #13
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Extracting larger icons

    SHGetImageList is part of Windows SDK 'shellapi.h' header file. Last time I looked it was not supported in MinGW version of 'shellapi.h'.

  15. #14
    Join Date
    May 2010
    Location
    Germany
    Posts
    3
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Extracting larger icons

    Ok, as mingw is opensource is there any way to include this function into the mingw 'shellapi.h' version?

  16. #15
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Extracting larger icons

    Depends if MinGW has a linker library for it. Why not copy the definition from the WinSDK and see?

    Or perhaps a newer/different version of MinGW already has the function?

  17. #16
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Extracting larger icons

    Quote Originally Posted by MisterIKS View Post
    Qt Code:
    1. HICON smallIcon, bigIcon;
    2. WCHAR path;
    3. filePath.toWCharArray(&path);
    4. ExtractIconEx(&path,0,&smallIcon,&bigIcon,1);
    5. image = new QImage(QPixmap::fromWinHICON(bigIcon).toImage());
    To copy to clipboard, switch view to plain text mode 
    This looks wrong. Your path variable is a single WCHAR (i.e. a single wide char) and not a pointer to an array of them. From the docs of QString::toWCharArray()
    int QString::toWCharArray ( wchar_t * array ) const
    Fills the array with the data contained in this QString object. The array is encoded in utf16 on platforms where wchar_t is 2 bytes wide (e.g. windows) and in ucs4 on platforms where wchar_t is 4 bytes wide (most Unix systems).

    array has to be allocated by the caller and contain enough space to hold the complete string (allocating the array with the same length as the string is always sufficient).

    returns the actual length of the string in array.

    Note: This function does not append a null character to the array.
    I have not tried this (Windows code goes poorly on Linux) but something like:
    Qt Code:
    1. HICON smallIcon, bigIcon;
    2. WCHAR path[1024];
    3. // ^^^^^ there is probably some actual size limit you can use
    4. // or you can dynamically allocate the right number based on the filePath string length
    5. int numChars = filePath.toWCharArray(path);
    6. path[numChars] = '\0'; // make sure of Nul termination
    7. ExtractIconEx(path,0,&smallIcon,&bigIcon,1);
    8. image = new QImage(QPixmap::fromWinHICON(bigIcon).toImage());
    To copy to clipboard, switch view to plain text mode 
    is probably closer.

Similar Threads

  1. Combining QT4 with larger C project
    By thuswa in forum Qt Programming
    Replies: 1
    Last Post: 10th July 2009, 01:46
  2. how can I make the Icon larger
    By duduqq in forum Qt Programming
    Replies: 1
    Last Post: 8th July 2008, 02:28
  3. Extracting text from QDomNodes
    By Matt Smith in forum Qt Programming
    Replies: 3
    Last Post: 25th February 2007, 20:27
  4. How to get larger radio buttons on XP?
    By Ben.Hines in forum Qt Programming
    Replies: 9
    Last Post: 24th April 2006, 19:00
  5. Is possible to get QStyle::SP_FileIcon larger?
    By Dark_Tower in forum Newbie
    Replies: 1
    Last Post: 5th April 2006, 13:31

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.