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
. Is there a straightforward way to do this?
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
. 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
However if you could do roll this into your startup time the round trip may be reasonable, preventing repeated conversions in QPainter
.
QPixmap
to a QImage
:http://doc.qt.io/qt-5/qpixmap.html#toImageQPixmap
you'll need to add one: http://doc.qt.io/qt-5/qimage.html#convertToFormatsetPixel
: 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)convertFromImage
: http://doc.qt.io/qt-5/qpixmap.html#convertFromImage