Issue when loading an ico from a Qt resource file

2020-02-14 08:27发布

问题:

I am using Qt Designer 4.8.4 and I include two files in the QMainWindow resource file: a .ico file and a .gif file. When loading from code using:

QPixmap p;
p.load(":/MyApp/media/logo.gif");  // does work
p.load(":/MyApp/media/logo.ico");  // does not work

The gif file works, but not the ico file. Is there any reason? I am using the ico file as the window icon and it is not showing when running the application.

And, YES, I am successfully compiling the qrc file since the GIF file is working.

Cheers,

回答1:

*.ico is not supported.

By default, Qt supports the following formats:

Format Description Qt's support

BMP Windows Bitmap Read/write

GIF Graphic Interchange Format (optional) Read

JPG Joint Photographic Experts Group Read/write

JPEG Joint Photographic Experts Group Read/write

PNG Portable Network Graphics Read/write

PBM Portable Bitmap Read

PGM Portable Graymap Read

PPM Portable Pixmap Read/write

TIFF Tagged Image File Format Read/write

XBM X11 Bitmap Read/write

XPM X11 Pixmap Read/write



回答2:

Because QPixmap Cant read .ico files simply, take a look at documentation QPixmap if you want to know more about formats supported



回答3:

you can convert it's format to Qt supported formats.



回答4:

The accepted answer here is incorrect, or as mentioned in the comments, at least misleading. Qt has come with a plugin for handling ICO format since sometime in version 4.4. Here's a similar question from the same era.

For deployment, one needs to copy the plugins/imageformats/[lib]qico.[dll|so] file from the Qt installation used for building with to within their executable's directory. Put it in a subfolder, like so: <your_executable>/imageformats/qico.dll. If testing a debug build, the d suffix version of the lib is needed instead (qicod.dll).

(This newer question has been marked as a duplicate of this, so now 2 questions point to the wrong answer. Which is why I'm essentially repeating my post here.)



回答5:

Guessing from your followup on the already chosen answer, you may want to have that ICO as the appwindow icon. That is possible, and also is perhaps why Qt Designer is capable of displaying it (trying to guess your intent). However, having it so doesn't involve any QPixmap coding, it's purely configuration affair. Described here (and yes, #worksforme) :

http://doc.qt.digia.com/qt/appicon.html