简体   繁体   English

如何创建QPixmap的半透明副本?

[英]How can I create a translucent copy of a QPixmap?

In the program I'm making, I need two images, which are exactly the same but one is translucent. 在我制作的程序中,我需要两张图像,它们完全相同,但其中一张是半透明的。 For performance reasons, I want to create two separate QPixmaps instead of using just one and setting the opacity of a QPainter . 出于性能原因,我想创建两个单独的QPixmaps而不是仅使用一个并设置QPainter的不透明度。 Is there a straightforward way to do this? 有没有简单的方法可以做到这一点?

Maybe you should read this example . 也许您应该阅读此示例 I think you will need to use the composition mode for this purpose. 我认为您将需要为此使用合成模式

No, there is no performant way to do this. 不,没有执行此操作的高性能方法。

To modify channels of a QPixmap it must be: Converted into a QImage , modified, converted back to a QPixmap . 要修改QPixmap通道,它必须是:转换为QImage ,经过修改,再转换回QPixmap Depending upon your application the round trip will probably make it simpler to just do this in the QPainter : http://www.qtcentre.org/threads/51158-setting-QPixmap-s-alpha-channel 根据您的应用程序,往返可能会使在QPainter执行此操作更简单: http : QPainter

However if you could do roll this into your startup time the round trip may be reasonable, preventing repeated conversions in QPainter . 但是,如果可以将其计入启动时间,则往返行程可能是合理的,从而避免了QPainter重复转换。

  1. Convert your QPixmap to a QImage : http://doc.qt.io/qt-5/qpixmap.html#toImage 将您的QPixmap转换为QImagehttp : //doc.qt.io/qt-5/qpixmap.html#toImage
  2. If you didn't have an alpha channel in your QPixmap you'll need to add one: http://doc.qt.io/qt-5/qimage.html#convertToFormat 如果您的QPixmap没有alpha通道,则需要添加一个: http : //doc.qt.io/qt-5/qimage.html#convertToFormat
  3. Then for each pixel in your image call setPixel : http://doc.qt.io/qt-5/qimage.html#pixel-manipulation (Note that setPixel takes a QRgb . You'll need to get the red, green, and blue channels from the pixel to be modified and use these along with your desired alpha value in qRgba : http://doc.qt.io/qt-5/qcolor.html#qRgba ) 然后,对图像中的每个像素调用setPixelhttp : QRgb (请注意, setPixel带有QRgb 。您需要获取红色,绿色,和要修改的像素中的蓝色通道,并在qRgba它们与您所需的alpha值一起使用: http : qRgba
  4. Finally you'll need to use convertFromImage : http://doc.qt.io/qt-5/qpixmap.html#convertFromImage 最后,您需要使用convertFromImagehttp : convertFromImage

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

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