PDA

View Full Version : QPainter::drawRoundedRect() drawing uneven corners



ComServant
20th July 2011, 00:13
http://img718.imageshack.us/img718/6559/cornerraduii.png
(Scaled x3)

I'm trying to draw a rounded rect, where the corners are rounded the same as each other. As you can see in the image above, mine keep coming out where the upper-left corner is correct, the lower-right corner is wrong, and the other two are somewhere in-between.

I'm calling the function like this:

painter->drawRoundedRect(...blah..., 7.0f, 7.0f);

Clearly I don't understand how QPainter::drawRoundedRect() works. ( http://doc.qt.nokia.com/latest/qpainter.html#drawRoundedRect )

Why are my corners uneven?

mvuori
20th July 2011, 00:31
Your corners are uneven probably because of:
* Drawing the corners having a small radius compared to the screen resolution at positions that map differently into the screen pixels
* The shapes being drawn using idealistic algebra which doesn't take the into account the problems of low resolution screens & small graphics

Of course Qt could and should do a better job here.

(You should scale screen grabs at an even ratio (2 x, 4x etc... instead of 3 x) so that they can be scaled back pixelwise exactly.)

ComServant
20th July 2011, 02:06
So is there any way around that? Buttons and things drawn by Qt don't suffer from the same problem, and their corners are drawn relatively tiny.

How can I make the corners of my rounded rects uniform?

Santosh Reddy
20th July 2011, 03:30
It is very much possible to draw a symmetrical rounded rectangle, It's strange to observe that QPainter::drawRoundedRect() algorithm to draw is not capable to do so. It draws ok, when pen width on 1, for higher pen widths it deviates. Also looks like rounded rectangle is drawn using a painter paths using qreal point, that means that all four corners are drawn individually, even though their radius is same, because of real number calculations, the final bitmap may differ.

Any way, you can try using anti-aliasing to make it look better to some extent (painter.setRenderHint(QPainter::Antialiasing);)

ComServant
20th July 2011, 03:52
Thanks, antialiasing makes it look loads better. I did notice that single-pixel pen widths looked better also, but I'd prefer a thicker border in this case.

I appreciate the help!