How does Qt draw a border around a rectangle?

12,323

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:

enter image description here

Share:
12,323
Serodis
Author by

Serodis

Updated on June 24, 2022

Comments

  • Serodis
    Serodis almost 2 years

    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.

  • Serodis
    Serodis about 12 years
    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).
  • Serodis
    Serodis about 12 years
    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.
  • Serodis
    Serodis about 12 years
    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.
  • Serodis
    Serodis about 12 years
    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.
  • Serodis
    Serodis about 12 years
    From all my tests I've yet to find anything that differs from the truncation, I will accept.