Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Qt Application and window Icon under windows

I have created a simple application icon by embedding a standard windows resource file containing an icon. However I would also like to use this icon on my main application window. Is there an easy way to do this? So far it seems the only way would be to seperately load an icon that contains the window icon rather than reusing the already exisiting icon. This seems like a horrible solution. Amongst other things the actual icon is embedded in my executable I don't want to have to distribute it twice.

Anyone know how to do this?

like image 494
Goz Avatar asked Dec 16 '22 12:12

Goz


2 Answers

Actually ... turns out its very very simple ...

HICON       hIcon   = (HICON)LoadImage( GetModuleHandle( nullptr ), MAKEINTRESOURCE( IDI_ICON1 ), IMAGE_ICON, 0, 0, LR_DEFAULTSIZE | LR_LOADTRANSPARENT );

setWindowIcon( QIcon( QtWin::fromWinHICON( hIcon ) ) );

::DestroyIcon( hIcon );
like image 132
Goz Avatar answered Dec 19 '22 01:12

Goz


I think the post from Goz is a good match for your question. But if you want to avoid using the native Windows API (which is actually preferrable since setting the application icon is platform dependent) I would opt for this seemingly less elegant approach:

1) in your .pro file:

   win32:RC_FILE=your_rcfile_with_icon.rc
   RESOURCES += qt_Resource_file.qrc

2) Add the same icon as in your .rc file to the qt .qrc file (i.e. embedd it twice)

3) in your main file:

   setWindowIcon(QIcon(":/the_icon.ico"));

This avoids native API calls and your code remains portable. SEttign the application icon is unfortunatly different for every platform. So you should really avoid the native calls if you want portable code.

like image 26
pokey909 Avatar answered Dec 19 '22 01:12

pokey909