rajeshma
rajeshma

Reputation: 11

Why does the read-back pixel color differ from color drawn on a QImage?

I have a problem with QPainter on QImage in Qt 5.4.

The image has Format_ARGB32. I want to set a given RGBA value on pixels in the image using a QPainter draw function and later read the value back using QImage::pixel.

Yet the value painted and the value read back are different. What am I doing wrong?

Sample code:

QImage image(100, 100, QImage::Format_ARGB32);
uint value = 0x44fa112b; //some value..
QPainter painter(&image);
painter.setCompositionMode(QPainter::CompositionMo de_Source);
QColor color(qRed(value), qGreen(value), qBlue(value), qAlpha(value));
QBrush brush(color);
painter.setBrush(brush);
painter.drawRect(0,0,image.width(), image.height());

uint value1 = image.pixel(50,50);
// value1 IS NOT EQUAL TO value. Why??

Upvotes: 1

Views: 433

Answers (2)

rajeshma
rajeshma

Reputation: 11

Problem solved!! Working properly when I tried with Qt 5.8 Looks like a bug with Qt 5.4. Thanks to all :)

Upvotes: 0

This works fine in Qt 5.7. Perhaps earlier Qt versions need the painter.end() call.

#include <QtGui>
int main(int argc, char ** argv) {
   QGuiApplication app{argc, argv};
   QImage image{100, 100, QImage::Format_ARGB32};
   auto const set = 0x44fa112b;
   QPainter painter(&image);
   painter.setCompositionMode(QPainter::CompositionMode_Source);
   painter.setBrush({{qRed(set), qGreen(set), qBlue(set), qAlpha(set)}});
   painter.drawRect(image.rect());
   if (false) painter.end(); //<< try with true here
   auto readback = image.pixel(50,50);
   qDebug() << hex << set << readback;
   Q_ASSERT(readback == set);
}

Upvotes: 0

Related Questions