c++ - 如何检查QImage是否有效?

标签 c++ qt qimage qtgui

我想知道是否有办法确定 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,你可以稍后通过以下方法检查图像有效性:

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.

关于c++ - 如何检查QImage是否有效?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20872006/

相关文章:

qt - 使用 QImage 和 OpenCV 显示图像

c++ - 为什么#ifndef __func__ 返回真?

python - 如何用两种不同的语言制作 PYQT 图形用户界面?

c++ - 固定大小的unordered_map,如何定义?

c++ - 如何将Window放入QWidget布局(Qt5)

linux - 我如何检测 Linux 中的 USB 驱动器插入?

java - 从 QBytearray 创建 QImage

pointers - 如何使用 PYQt' QImage scanline() 访问像素数据

c++ - 创建命名管道错误

C++ 从 std::exception 获取调用堆栈