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?
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 );
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With