Oleksandr Verhun
Oleksandr Verhun

Reputation: 854

QPainterPath painting arc

I'm new to Qt and I have a problem painting an arc with QPainterPath. When I'm painting it, also it paints a line to some end of the rectangular.Why it is painting some line?enter image description here

This is my code:

QRectF rect( m_height / 2 - 100.0 / 2, m_width / 2 - 100.0 / 2, 100.0, 100.0 );
path.moveTo( m_width / 2, m_height / 2 );
path.arcTo( rect, 90, - 180 );
QPainter painter( this );
painter.drawPath( path );

I was planning to draw something like this: enter image description here

Code to this:

// figure coords
    QRectF rectangle1( m_x * m_scale, m_y * m_scale, m_width * m_scale, m_height * m_scale );
int startAngle1 = 90 * 16 ;
int spanAngle1 = 180 * 16;

path.arcTo( rectangle1, startAngle1, spanAngle1 );

QRectF rectangle2( ( m_x + 170.0 ) * m_scale, m_y * m_scale, m_width * m_scale, m_height * m_scale );
int startAngle2 = 90 * 16;
int spanAngle2 = -180 * 16;

path.arcTo( rectangle2, startAngle2, spanAngle2 );

QLine line1( ( m_x + 125.0 ) * m_scale, m_y * m_scale, ( m_x + 295.0 ) * m_scale, m_y * m_scale );
QLine line2( ( m_x + 125.0 ) * m_scale, ( m_y + 250.0) * m_scale, ( m_x + 295.0 ) * m_scale,
             ( m_y + 250.0) * m_scale );



// paint figure

QPainter painter(this);
painter.drawArc(rectangle1, startAngle1, spanAngle1);
painter.drawArc(rectangle2, startAngle2, spanAngle2);
painter.drawLine( line1 );
painter.drawLine( line2 );

Why I used QPainterPath? Cause you can easily fill it with color, which I need later, when I want to fill with color second picture, you need to calculate, what pixels to color( or I don't know something ). So the question is, what I wrote at the top, why is this line is also drawn, when I use path.arcto(...)?

Upvotes: 2

Views: 1334

Answers (1)

Marek R
Marek R

Reputation: 37697

You made it too complicated. There is a method drawRoundedRect and it will do the job.

QPainter painter(this);
QRect r(internalPartOfRect.adjusted(-r.height()/2, 0, r.height()/2, 0));
painter.setPen(palette().color(QPalette::Normal, QPalette::WindowText));
painter.drawRoundedRect(r, r.height()/2);

Upvotes: 2

Related Questions