Trolltech Home | Qt-interest Home | Recent Threads | All Threads | Author | Date
All threads index page 2

Qt-interest Archive, February 2008
Bug in QPainter::drawPixmap


Message 1 in thread

Hi all,
I have found a weird behavior of QPainter::drawPixmap

Qt widgets and painting devices have a coordinate system where a lower y
value corresponds to the top.
Most engineering and math coordinates systems have a coordinate system where
low y is the bottom.

I use a "math type" coordinate system (y rises towards the top) and for this
I use the worldTransformation of the painter:

painter->scale (scale, -scale) //The y axis is inverted

Everything works fine for drawing vector objects, but not for drawing
pixmaps.
I use painter->drawPixmap(targetRect,pixmap,sourceRect) for drawing the
image.

sourceRect is set to QRect(0,0,256,256) to draw the whole image.

If targetRect (the destination rectangle in world coordinates) is
"normalized" , such as QRect(-1,-1,2,2), the pixmap is drawn top-down. This
is an expected behavior, since the target rectangle transformed to device
coordinates is top-down (with negative height)

So I want to correct this. I set targetRect to a top-down rectangle, such as
QRect(-1,1,2,-2).
Then the pixmap is not rendered properly (it looks like a single pixel row
from the source image is stretched to the destination rectangle)

So my conclusion is (I may do something wrong however...)
 - It is not possible to draw a pixmap in its original orientation on a
y-flipped worldTransform painter,
 - A solution to this consists in pre-flipping the source pixmap. However
this is more a hack than a clean solution.

Thanks

Etienne

Message 2 in thread

Etienne Sandrà wrote:
> So I want to correct this. I set targetRect to a top-down rectangle, 
> such as QRect(-1,1,2,-2).
> Then the pixmap is not rendered properly (it looks like a single pixel 
> row from the source image is stretched to the destination rectangle)

You probably want to use QRectF instead of QRect. QRect does some really 
weird things when its width or height is negative, which according to 
the Qt documentation is "for historical reasons."

    http://doc.trolltech.com/4.3/qrect.html#coordinates

Search for "historical" on that page. I had a similar problem, and 
decided to use QRectF, not because it uses floating point values, but 
because it stores x, y, width, height, as opposed to QRect, which stores 
x1, y1, x2, y2.

You can use this overload of QPainter::drawPixmap() which takes a QRectF 
instead of a QRect:

     http://doc.trolltech.com/4.3/qpainter.html#drawPixmap

Happy coding!

--Dave

--
 [ signature omitted ]