简体   繁体   English

从QImage到QPixmap是昂贵的

[英]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. 但是我发现将qimage转换为qpixmap花了大约30ms。 this affected my frame rate, so I want to find more effitive way to show my image with QtGui. 这影响了我的帧速率,所以我想找到一种更有效的方法来用QtGui显示图像。 Which method can I use? 我可以使用哪种方法? Could someone help me? 有人可以帮我吗?

If you read the documentation well, everything is explained. 如果您正确阅读了文档,则说明了所有内容。

  • QImage is design for IO handling, and for direct pixel access and manipulation QImage是为IO处理以及直接像素访问和操纵而设计的
  • QPixmap is designed and optimized for showing images on screen QPixmap是为在屏幕上显示图像而设计和优化的

If you only load image and show it (without any manipulation to your image), just load your image and cache it as QPixmap. 如果仅加载图像并显示它(不对图像进行任何操作),则只需加载图像并将其缓存为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. 也许您可以尝试将图像直接绘制到小部件的QPainter。 I don't know if it faster than convert image to pixmap and paint it to widget. 我不知道它是否比将图像转换为pixmap并将其绘制为小部件更快。

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. 确保图像采用后备存储格式,只需在小部件上使用drawImage即可。 It's as fast as can be. 尽可能快。

With the default raster backend, the QPixmap is a very thin wrapper around a QImage . 使用默认的栅格后端, QPixmap是围绕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: 要获得与QPixmap兼容的格式,可以使用以下代码:

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

之前我也遇到过类似的问题,发现的最佳解决方案是使用OpenGLQOpenGLWidget + glDrawPixels() )而不是将QImage转换为QPixmap

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

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