PDA

View Full Version : QPixmap scaling question



eric
20th November 2007, 15:44
Hello.
I create two QPixmaps: one is loaded from a bitmap file, the other one is drawn. The first one can be rescaled with scaled(), the second one can not. Do you know why? Thank you!

This scaling works


QPixmap pm(600,450);
pm.load("image.jpg");
pm = pm.scaled(300,225); //this works fine
pixmapLabel->setPixmap(pm);


This scaling won't work:


QPixmap pm(600,450);
QPainter p(&pm);
QPen pen(Qt::red, 2);
p.setPen(pen);
p.drawLine(0, 0, 600, 450);
pm = pm.scaled(300,225);//this has no effect. why?
pixmapLabel->setPixmap(pm);

marcel
20th November 2007, 15:53
Come on... :) You draw a diagonal line, scale it to half and expect to see a difference? The line is the same(diagonal). Try drawing something else.

eric
20th November 2007, 15:57
Thanks Marcel, but whatever I draw I don't see the difference (the picture is not getting smaller when I scale()) - the overall size of the pixmap is unchanged. There must be something else wrong. I've tested this with all kinds of other shapes, too.

marcel
20th November 2007, 16:53
What does pm.size() returns after scaling? It should return (300, 225). If so, then the scaling is successful.

eric
20th November 2007, 17:31
Hi Marcel. Thanks for helping me.

The scaling does not take place because the 1) pixmap is displayed in original size, and
2) the size as returned by height() and width() has not changed. I put the code below.



QPixmap pm(600,450);
QPainter p(&pm);
QPen pen(Qt::red, 2);
p.setPen(pen);
p.drawLine(0, 0, 600, 450);
pm = pm.scaled(300,225);

xLabel->setNum(pm.width());
yLabel->setNum(pm.height());
pixmapLabel->setPixmap(pm);


P.S. I'm drawing a straight line here in order to be able to easily tell cropping from rescaling. If cropping takes place, the line is no longer perfect diagonal. That's why I chose to draw a line in this test code.

marcel
20th November 2007, 17:34
I think this might be because the painter is still opened on the pixmap when you scale it.
Try calling p.end() before scaling the pixmap.

eric
20th November 2007, 17:40
Marcel, you ROCK!

That solved my problem.
Thank you.