I want to know how Qt does a border when using QPainter's drawRect. The reason for this is I am trying to draw three rectangles next to each other, but I'm having trouble getting them to touch perfectly at all pen sizes.
How does Qt draw a border around a rectangle?
QPainter
's documentation for drawRect
says:
A stroked rectangle has a size of [the input rectangle] plus the pen width.
So it goes like this:
I've read this, and that tells me the width, but what about the X and Y? If I do: paint.drawRect(5,5,10,10); with a pen width of 5, it will draw a 15-pixel-wide rectangle. It will not however draw it starting at (5,5). –
Subaudition
The problem is, QT does not seem to draw the border outside of the rectangle. The pen width actually goes within the rectangle. Meaning, in my previous comment, not only does it not start at 5,5, but, it also doesn't have a width of 10. It actually draws a width of 5 and two borders of 5 as well. This totals 15, as the documentation states. My problem is I want to find a way to determine for ANY x,y,width,height,penwidth what the new x and y will be. –
Subaudition
From all this I have been able to come up with: rectWidth = initialWidth - borderWidth newWidth = borderWidth + initialWidth newX = oldX - (borderWidth/2) newY = oldY - (borderWidth/2) It seems that if borderWidth is odd, the .5 is truncated. I am looking for a way to prove this, however, currently. –
Subaudition
Well, the only part I'm still trying to make sure is correct before accepting an answer is the new X and new Y. As your drawing shows it is inputX - (penWidth/2). But, what does that mean when penWidth is 5? Some tests have shown that it truncates the decimal, but, I want to be sure. –
Subaudition
From all my tests I've yet to find anything that differs from the truncation, I will accept. –
Subaudition
I just wanted to add to the answer and address the question about truncation.
Truncation might be happening because you are using the QRect and not QRectF. QRectF gives you floating point precision. Similarly, you can use QPen::setWidthF(qreal width) to ensure your border is not getting truncated either.
© 2022 - 2024 — McMap. All rights reserved.