PDA

View Full Version : QImage scaled twice to same instance and QPainter paint abnormally



oliverliu.hz
10th February 2011, 07:41
I confused about line 15.
If commented it, the code will create a pixmap filled with many circle. That is named resultA.
If it works, the code creates a pixmap that contents is balloon + circle. That is named resultB.
Why?
I think the painter variable is not relevant with QImage instances tmpdi and image, just only relevant displayImage instance.
Can anyone give me any suggestions to explain it?
Thanks!


int main (int argc, char** argv)
{
QSize resultSize(281,377);
QSize pegSize(21,29);

QImage image;
image.load("balloon.jpg");
QImage tmpdi;
tmpdi = image.scaled(resultSize,Qt::KeepAspectRatio);
// tmpdi = image.scaled(QSize(resultSize.width()*1.5, resultSize.height()*2.0),Qt::KeepAspectRatio);

QImage displayImage= QImage(resultSize,QImage::Format_ARGB32_Premultipl ied);
QPainter painter1(&displayImage);
painter1.fillRect(QRect(0,0,resultSize.width(),res ultSize.height()), Qt::transparent);

float ratioPegX = resultSize.width() * 1.0 / pegSize.width();
float ratioPegY = resultSize.height() * 1.0 / pegSize.height();

//draw circle on it
for (int pegi = 0; pegi < pegSize.width(); pegi++)
for (int pegj = 0; pegj <pegSize.height(); pegj++)
{
QRectF rectf = QRectF( pegi * ratioPegX, pegj * ratioPegY,
ratioPegX, ratioPegY);

painter1.drawEllipse(rectf.center(), ratioPegX/2.0, ratioPegY/2.0);
}

displayImage.save("x3.png");

return 0;
}

JohannesMunk
10th February 2011, 15:08
Hi!

There is no line 15 in your code. There is no resultA and no resultB. Please rephrase your problem and question!

Johannes

oliverliu.hz
11th February 2011, 10:21
I am so sorry the line number is wrong, it should be line 10.
Thank you, Johannes.
Now I upload the test image balloon.jpg and test result image resultA.png and resultB.png.

wysota
11th February 2011, 10:33
The painter works on an object you point it to. It has no influence on other objects (be it images or anything else). I don't know if that answers your question... If not then maybe try rephrasing it.

oliverliu.hz
12th February 2011, 09:24
The painter works on an object you point it to. It has no influence on other objects (be it images or anything else).
I think so too. Just the comprehension make me confused with the code I pasted.

Let me describe it in detail.
line 10 is commented, compile ->run it will create 1st image of my uploaded files.
line 10 is NOT commented, compile->run, it will create 2nd image.

balloon.jpg in line 10 is the 3rd image.

I only scaled an image and then return itself.

Added after 8 minutes:
Another phenomenon:
if the line 8 ~ line 10 such as below.
"
QImage tmpdi;
tmpdi = image.scaled(resultSize,Qt::KeepAspectRatio);
tmpdi = image.scaled(QSize(resultSize.width()*1.5, resultSize.height()*2.0),Qt::KeepAspectRatio);
"
run it, will get 2nd image of my uploaded image files.
if it changed as
"
QImage tmpdi;
QImage tmpditmp = image.scaled(resultSize,Qt::KeepAspectRatio);
tmpdi = image.scaled(QSize(resultSize.width()*1.5, resultSize.height()*2.0),Qt::KeepAspectRatio);
"
it will create 1st image.

I do not know why it gets different result? I only do create a new instance.
I Can NOT do scale operation on same object, can I?
If I do, what happen on Painter?

or the code has some defects?

oliverliu.hz
15th February 2011, 02:35
Can one tell me the reason?