我想知道是否有办法确定 QImage
是否有效。我将该图像显示为 QLabel
中的像素图,有时当图像无效时。它不会显示在 QLabel
中,然后。
有时无效的原因是图像是从外部数据加载的,有时数据可能会损坏。
因此,我想知道是否有可能真正确定 QImage
是否有效。
最佳答案
可以从数据中查看图片加载的返回值,因为是boolean返回值,加载不成功则为false。
为方便起见,以下是内联文档的相关部分:
bool QImage::load(const QString & fileName, const char * format = 0)
Loads an image from the file with the given fileName. Returns true if the image was successfully loaded; otherwise invalidates the image and returns false.
如果您碰巧从文件或其他设备加载,您甚至可以使用 QImageReader
。那有一个 dedicated error enumeration微调。您也可以按原样查询 errorString()。
话虽如此,如果由于某种原因你想在加载不成功的情况下继续使用 QImage,你可以稍后通过以下方法检查图像有效性:
Returns true if it is a null image, otherwise returns false.
A null image has all parameters set to zero and no allocated data.
关于c++ - 如何检查QImage是否有效?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20872006/