QPainter: painted color (brush) and pixel value (read after the draw) are different?
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??
Re: QPainter: painted color (brush) and pixel value (read after the draw) are differ
Did you test this sample code ? What value you got back ?
Re: QPainter: painted color (brush) and pixel value (read after the draw) are differ
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.
Re: QPainter: painted color (brush) and pixel value (read after the draw) are differ
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?
Re: QPainter: painted color (brush) and pixel value (read after the draw) are differ
Quote:
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).
Quote:
(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?
Re: QPainter: painted color (brush) and pixel value (read after the draw) are differ
Thank you all..
Posting the actual code with result..
Code:
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDebug>
#include <QPainter>
MainWindow
::MainWindow(QWidget *parent
) : ui(new Ui::MainWindow)
{
ui->setupUi(this);
image.fill(0);
uint value = 0x44fa112b; //some value..
painter.
setCompositionMode(QPainter::CompositionMode_Source);
QColor color
(qRed
(value
), qGreen
(value
), qBlue
(value
), qAlpha
(value
));
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"
Re: QPainter: painted color (brush) and pixel value (read after the draw) are differ
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,
_
Re: QPainter: painted color (brush) and pixel value (read after the draw) are differ
Problem solved!!
Working properly when I tried with Qt 5.8
Looks like a bug with Qt 5.4.
Thanks to all :)
Re: QPainter: painted color (brush) and pixel value (read after the draw) are differ
Quote:
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.