PDA

View Full Version : alpha for pixel always returns 255



clutch12
1st May 2016, 00:43
In this code alpha is always 255 even thought the box displays in my QLabel with a transparency. Can anyone tell me why alpha is always 255?

QImage img;
img.load(":/images/images/box.png");
QColor rgba( img.pixel( 1, 1) );
qDebug() << " r: "<<rgba.red()<<" g: "<<rgba.green()<<" b: "<<rgba.blue()<<" a: "<<rgba.alpha() << endl;
qDebug() << pixmap.hasAlpha() << " : " << pixmap.hasAlphaChannel() << endl;

d_stranz
2nd May 2016, 18:31
What does img.format() return? What is the output of your qDebug() statements? And where does "pixmap" come from?

Posting your actual code would be better than posting something that won't even compile.

clutch12
4th May 2016, 18:10
img.format() returns QImage::Format_ARGB32

Sorry about not including the pixmap. I was trying to snip out the relevant section of code and forgot to clip that line. It was just sourcing the box.png in a different way and can be ignored. The box.png image is an uniform image and has an alpha of 128. The output is
r: 0 g: 128 b: 255 a: 255

I found the problem though. It appears the QColor constructor doesn't use the alpha values, at least according to this thread.
http://www.qtforum.org/article/32132/how-to-set-the-alpha-value-of-a-pixel-in-a-qimage.html

thanks.

d_stranz
4th May 2016, 22:17
It appears the QColor constructor doesn't use the alpha values

Ah, yes, forgot about that. You could use the QColor( int r, int g, int b, int a ) constructor instead, by extracting the relevant values from the QRgb returned from QImage::pixel().