QApplication::setWindowIcon works on Windows XP, b

2019-06-24 03:41发布

问题:

In a simple Qt application starting like this:

int main(int argc, char* argv[])
{
  QApplication app(argc, argv);
  app.setWindowIcon(QIcon(":/qapp/appicon.ico"));
...

we set the icon in the second line (this line and icon file in resources is the only difference between my test program and application generated by Qt wizard). This application works perfect on Windows XP, but on Windows 7 we can't see our icon in top-left corner of the window (system shows default icon instead of my appicon.ico).

Configuration of developer's computer:

  • Windows XP-SP3,
  • Qt 5.1,
  • MSVS2010-SP1.

UPDATE:

Operating system of developer is Windows XP, so we have temporary answer. When we compiled this application on computer with Windows 7, the problem disappeared (application shows the icon correctly on WinXP and Win7). So now we have to compile releases of this application on Windows 7. But it does not looks like perfect solution.

Do you have any ideas, how to solve this problem better?

回答1:

Similar question is .ico icons not showing up on Windows. Solution is simple:

  • create directory imageformats in application dir,
  • copy qico.dll in it.

Without this plugin QIcon can't read .ico files. So the problem was not in Windows 7 and Windows XP. The problem was in default paths variables on different computers.



回答2:

My guess is you've got to create a resource file windows_icon.rc with the following content:

IDI_ICON1               ICON    DISCARDABLE     "images/app-icon-48.ico"

and add this line to your app's .pro file:

RC_FILE = windows_icon.rc

This is the way you set the app icon on Windows (the one that becomes the icon of the exe-file)