betterorbest
betterorbest

Reputation: 21

QImage to QPixmap is expensive

For my application, I wanted to show images continuously. But I found that it took me around 30ms to convert qimage to qpixmap. this affected my frame rate, so I want to find more effitive way to show my image with QtGui. Which method can I use? Could someone help me?

Upvotes: 0

Views: 5751

Answers (3)

You don't have to do the conversion. Make sure that the image is in the format of the backing store, and simply drawImage on the widget. It's as fast as can be.

With the default raster backend, the QPixmap is a very thin wrapper around a QImage. As long as the image is of the correct format, the conversion is a no-op.

To obtain the format compatible with QPixmap, you can use the following code:

QImage::Format pixmapFormat() {
    static auto format = QPixmap{1,1}.toImage().format();
    return format;
}

Upvotes: 1

Tomas
Tomas

Reputation: 2210

I was facing a similar problem before and the best solution I have found is to use an OpenGL (QOpenGLWidget + glDrawPixels()) instead of converting QImage to QPixmap.

Upvotes: 0

Apin
Apin

Reputation: 2668

If you read the documentation well, everything is explained.

  • QImage is design for IO handling, and for direct pixel access and manipulation
  • QPixmap is designed and optimized for showing images on screen

If you only load image and show it (without any manipulation to your image), just load your image and cache it as QPixmap.

But if you do the manipulation, nothing much you can do. Maybe you can try to paint the image directly to QPainter of the widget. I don't know if it faster than convert image to pixmap and paint it to widget.

Upvotes: 3

Related Questions