Qt window and window Icon under windows

I created a simple application icon by inserting a standard Windows resource file containing the icon. However, I would also like to use this icon in the main application window. Is there an easy way to do this? So far, it seems that the only way would be to separately load the icon containing the window icon, rather than reusing an existing icon. This seems like a terrible decision. In addition, the actual icon is embedded in my executable, I do not want to distribute it twice.

Does anyone know how to do this?

+4
source share
2 answers

Actually ... it turns out 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 ); 
+7
source

I think a post from Goz is a good match for your question. But if you want to avoid using the native Windows API (which is actually preferable, since the installation of the application icon is platform dependent), I would choose 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 (ie embedd it twice) 3) in your main file: setWindowIcon(QIcon(":/the_icon.ico")); 

This avoids API calls, and your code remains portable. The SEttign app icon, unfortunately, is different for each platform. Thus, you really should avoid native calls if you want to port code.

+5
source

All Articles