PDA

View Full Version : Detecting QIcon failed load from file



spraff
7th January 2011, 22:08
Hello.

I am constructing QIcon and passing in a string file name.

How do I detect if this has failed? It seems like if I deliberately construct it with garbage the icon is simply empty but I can't see anything in http://doc.qt.nokia.com/latest/qicon-members.html that describes success/failure of this load.

Any ideas? (Other than asking QFile, it might be nice to know if the file was not an icon, for example)

high_flyer
8th January 2011, 14:54
You can ask isNull() to know if loading was successful.

ComServant
7th July 2013, 00:54
Whether that used to be true I don't know, but it doesn't currently work. :(
The v5.1 documentation (http://qt-project.org/doc/qt-5.1/qtgui/qicon.html#isNull) says:

An icon is empty if it has neither a pixmap nor a filename.
Note: Even a non-null icon might not be able to create valid pixmaps, eg. if the file does not exist or cannot be read.

Likewise, with addFile() (http://qt-project.org/doc/qt-5.1/qtgui/qicon.html#addFile) it says:

Note: When you add a non-empty filename to a QIcon, the icon becomes non-null, even if the file doesn't exist or points to a corrupt file.

Not the ideal solution, but this seems to work:

icon.pixmap(QSize(64,64)).isNull()

It attempts to use the icon to retrieve or generate a pixmap of the size we're planning to use. It returns a null pixmap if the QIcon can't provide/generate such a image.

Looking at the Qt sourcecode, I can't see any other way to detect the a failure.