Qt Application and window Icon under windows

久未见 提交于 2019-12-22 09:10:24

问题


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?


回答1:


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 );



回答2:


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.



来源:https://stackoverflow.com/questions/6523039/qt-application-and-window-icon-under-windows

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!