简体   繁体   English

如何检查 QImage 是否有效?

[英]How to check if QImage is valid?

I wanted to know if there was a way to determine if a QImage is valid.我想知道是否有办法确定QImage是否有效。 I am displaying that image as a pixmap in QLabel and sometimes when the image is not valid.我在QLabel将该图像显示为像素图,有时当图像无效时。 It is not displayed in the QLabel , then.那么它不会显示在QLabel

The reason for not being valid sometimes is that the image is loaded from external data and that data can be corrupted at times.有时无效的原因是图像是从外部数据加载的,并且数据有时会损坏。

Thereby, I wished to know if it is possible to actually determine whether a QImage is valid.因此,我想知道是否可以实际确定QImage是否有效。

You can check the return value of the image loading from the data since it is a boolean return value, and it will be false when the loading was unsuccessful.您可以从数据中查看图像加载的返回值,因为它是一个布尔返回值,加载不成功时返回false。

Here is the relevant part of the documentation inline for your convenience:为方便起见,以下是内联文档的相关部分:

bool QImage::load(const QString & fileName, const char * format = 0) 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;如果图像加载成功,则返回 true; otherwise invalidates the image and returns false.否则会使图像无效并返回 false。

You could even use QImageReader if you happen to load from file or other devices.如果您碰巧从文件或其他设备加载,您甚至可以使用QImageReader That has a dedicated error enumeration for fine tuning.它具有用于微调的 专用错误枚举 You could also query the errorString() as is.您还可以按原样查询 errorString()。

That being said, if for some reason you wanna proceed with the QImage despite that the loading was unsuccessful, you can check the image validity later by the following method:话虽如此,如果出于某种原因,尽管加载不成功,您仍想继续使用 QImage,您可以稍后通过以下方法检查图像有效性:

bool QImage::isNull() const bool QImage::isNull() const

Returns true if it is a null image, otherwise returns false.如果是空图像则返回真,否则返回假。

A null image has all parameters set to zero and no allocated data.空图像的所有参数都设置为零,并且没有分配数据。

如果加载图像失败,它将不包含任何数据,因此您可以使用以下命令进行检查:

image.isNull()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM