PDA

View Full Version : QPainter: painted color (brush) and pixel value (read after the draw) are different?



rajeshma
17th February 2017, 09:49
Hi all,
I have a problem with QPainter. (Qt 5.4)

I have an Image in Format_ARGB32
I want to print a given Rgba (uint) value on to this using QPainter draw function and later fetch the value using image.pixel(x,y).
But the value painted and the value got from the pixel are different !!. What could be the problem.
KIndly help.

The sample code is like this.
..............
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??

Santosh Reddy
17th February 2017, 12:07
Did you test this sample code ? What value you got back ?

rajeshma
17th February 2017, 15:42
Yes. I have tried (not exactly this one). But most of the time there is one bit difference in red value, green value or both the values. I have tried with different renderHints also. Does not make any difference.

high_flyer
17th February 2017, 22:17
do the values only differ in one bit?
I am surprised they are at all close!
QPainter::drawRect() does not fill the rectangle, so position 50,50 (middle of your rect) must be different than the outline which QPainter drew with drawRect() - or am I missing something in your post?

d_stranz
18th February 2017, 02:49
QPainter::draw() does not fill the rectangle

There is no draw() method in QPainter. And if you set a QBrush, it uses it in the drawRect() method to fill the rectangle. (And the current QPen, if any, to draw the outline).


(not exactly this one)

Meaning that the code in the post is probably not the actual code exhibiting the problem, so who knows what else might be going on?

rajeshma
18th February 2017, 05:28
Thank you all..
Posting the actual code with result..


#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDebug>
#include <QPainter>

MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);

QImage image(100, 100, QImage::Format_ARGB32);
image.fill(0);
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);

qDebug() << "RESULT : value = " << QString::number(value,16) << "value1 = " << QString::number(value1,16);
}

MainWindow::~MainWindow()
{
delete ui;
}

And the result is......
RESULT : value = "44fa112b" value1 = "44fb1329"

anda_skoa
18th February 2017, 12:43
If you want to fill the image with a specific color, there is QImage::fill().

Also:

* have you verified the QColor value? Is the rgba() result the same as the input
* if not, have you tried constructing the QColor directly from the value?

Cheers,
_

rajeshma
18th February 2017, 14:12
Problem solved!!
Working properly when I tried with Qt 5.8
Looks like a bug with Qt 5.4.

Thanks to all :)

high_flyer
18th February 2017, 19:57
There is no draw() method in QPainter. And if you set a QBrush, it uses it in the drawRect() method to fill the rectangle. (And the current QPen, if any, to draw the outline).
the draw() was a typo, thanks.
Ah, and indeed, in the docs it says drawRect() will use the brush as well.