[英]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
重复转换。
QPixmap
to a QImage
: http://doc.qt.io/qt-5/qpixmap.html#toImage 将您的QPixmap
转换为QImage
: http : //doc.qt.io/qt-5/qpixmap.html#toImage 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 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 ) 然后,对图像中的每个像素调用setPixel
: http : QRgb
(请注意, setPixel
带有QRgb
。您需要获取红色,绿色,和要修改的像素中的蓝色通道,并在qRgba
它们与您所需的alpha值一起使用: http : qRgba
) convertFromImage
: http://doc.qt.io/qt-5/qpixmap.html#convertFromImage 最后,您需要使用convertFromImage
: http : convertFromImage
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.