Search code examples
c++qtqt5qpainter

Drawing an arc correctly with Qt5 painter


I am trying to draw an arc at the edge of the bounding rectangle. This is important since I want it to scale with the control. However, This leads to the edges getting clipped off when using boundingRect() as an argument for the drawArc().

QBrush brush(QColor("#007430"));
painter->setBrush(brush);
QPen pen;
pen.setStyle(Qt::SolidLine);
float lineWidth1 = 6.0;
pen.setWidthF(lineWidth1);
painter->setPen(pen);
painter->setRenderHint(QPainter::Antialiasing);
painter->drawArc(boundingRect(), 45*16, 270*16);

enter image description here

To make it work correctly, I must pass in a rectangle that is 1/2 of the pen width smaller on each side. Is there a more direct way to do this in QT without manual calculating/adjusting?

QRectF arcRect(0 + lineWidth1/2,
               0 + lineWidth1/2,
               boundingRect().width() - lineWidth1,
               boundingRect().height() - lineWidth1);

painter->drawArc(arcRect, 45*16, 270*16);

enter image description here


Solution

  • I am sorry to bring you the real answer which unfortunately is no, there is no automatic way to scale to bounding box of drawing operations with QPainter in Qt5. You will therefore have to calculate this in your own code on a per case basis.

    On the bright side, this calculation is not very hard, and by doing it yourself you are certain to maintain full control over the process.