Search code examples
qtqpixmap

how to get image file format from QPixmap?


In my program, user choice and load an image into QPixmap in some class and after some works on loaded QPixmap the QPixmap passed into an other class, in new class I want to save the passed QPixmap as file, but I don't know what's the QPixmap format!

How we can get image file format from QPixmap?


Solution

  • A pixmap is conceptually system-specific, has no format per se, and may well lose data from the image that you've loaded. Also note that the image format and file format are two different things.

    • To preserve the image format, you must use the QImage class.

    • To preserve the file format, you must explicitly use QImageReader to read the image. The file format is available through the reader's format() method. It needs to be stored separately from the image, and used when saving the image later.

    • Finally, you might wish to preserve the file's name.

    As a matter of implementation detail, with the default Qt's raster backend, a QPixmap is a thin wrapper around QImage. But your intent is that of an image, not a pixmap, thus you should use the image class.