QPainterPath绘画弧

时间:2014-04-02 21:12:04

标签: c++ qt

我是Qt的新手,我在使用QPainterPath绘制弧时遇到问题。当我画它的时候,它也画了一条线到矩形的某一端。为什么画一些线?enter image description here

这是我的代码:

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 );

我打算画这样的东西: enter image description here

代码:

// 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 );

为什么我使用QPainterPath?因为你可以轻松地用颜色填充它,我以后需要它,当我想填充颜色第二张图片,你需要计算,什么像素颜色(或我不知道的东西)。 所以问题是,我在顶部写的,当我使用时,为什么还会绘制这条线 path.arcto(...)?

1 个答案:

答案 0 :(得分:2)

你太复杂了。有一种方法drawRoundedRect,它可以完成这项工作。

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);