c++ - QPainterPath画圆弧

标签 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?因为你可以轻松地用颜色填充它,这是我稍后需要的,当我想用​​颜色填充第二张图片时,你需要计算要着色的像素(或者我不知道什么)。 所以问题是,我在上面写的,为什么这条线也画出来了,当我使用 路径.arto(...)?

最佳答案

你把事情搞得太复杂了。有一个方法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);

关于c++ - QPainterPath画圆弧,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22823153/

相关文章:

qt - 在 QML TableView 中单击时编辑数据(如 excel)

c++ - QTreeView 与 setIndexWidget

c++ - 为什么以下代码的时间复杂度是 O(n^2)?

c++ - 如何使用qt将数据插入sqlite

c++ - 如何从 qplaintextedit 获取文本颜色?

EOF : behavior change; work-around? 上的 C++ istream tellg()/fail()

qt - 如何将 QUdp 数据包放入 AVPacket/FFmpeg

c++ - 虚拟继承使应用程序崩溃

c++ - std::max 与 lambda 和 auto

c++ - 为什么使用 DrawText API 绘制时我的字体边缘不平滑?