1. 函数原型:
void QPainterPath::arcTo(qreal x, qreal y, qreal width, qreal height, qreal startAngle, qreal sweepLength)
void QPainterPath::arcTo(const QRectF &rectangle, qreal startAngle, qreal sweepLength)
2. Qt里的解释:(有道翻译)
创建一个占用给定矩形的弧,从指定的起始角开始,并逆时针扩展甜度。
角度用度表示。顺时针的弧可以用负角指定。
请注意,如果弧的起始点尚未连接,则此函数将其连接到当前位置。添加电弧后,当前位置是电弧中的最后一个点。使用closeSubpath()函数将线绘制回第一个点。
如上图,startAngle为起始角度,逆时针转sweepLength角度,形成的弧线,上图的函数大约是:
arcTo(rect, 45,315);//即在矩形rect内,起始角度为45,逆时针转315度形成的弧。还有最后的注意:如果弧的起始点尚未连接,则此函数将其连接到当前位置。
3. 我的理解:其实Qt里的解释说的,很明白了。
首先要确定矩形框rect,一旦rect确定(其位置+大小),里面的椭圆(如果是椭圆的话)就已经定形了,arcTo这个函数只是帮我们取哪一段弧而已!如上图,长方形rect内为椭圆,如果rect为正方形,则里面的为圆;然后再确定从哪个角度逆时针取多少角度的弧线。
void QPainterPath::arcTo(qreal x, qreal y, qreal width, qreal height, qreal startAngle, qreal sweepLength)这个函数前四个参数就是确定rect的;后面两个参数就是要取哪一段弧。下面的函数一样的意义。
void QPainterPath::arcTo(const QRectF &rectangle, qreal startAngle, qreal sweepLength)
4.示例,copy可试。
第一个图,白色rect正好是弧所在的rect,取的是左半边(90度开始逆时针180度);第二个图,rect未涂色,弧线取的是上半部分,0-180。
void Widget::paintEvent(QPaintEvent *event) { QPoint startPt(30,30); QRect rect(startPt.x(),startPt.y(),40,100); qDebug()<<rect.x()<<rect.y(); QPainter p(this); p.setRenderHint(QPainter::Antialiasing); p.fillRect(rect,QColor(255,255,255)); int wid=40; //弧所在矩形宽度,高度为100 int het=100; QPainterPath path; path.moveTo(startPt.x()+wid/2,startPt.y()); path.arcTo(startPt.x(),startPt.y(),wid,het,90,180); p.fillPath(path,QBrush(QColor(122,122,122))); /*第一图完,下面是第二图*/ startPt.setX(30); startPt.setY(30+110); QRect rect1(startPt.x(),startPt.y(),100,100); // p.fillRect(rect1,QColor(255,255,255)); QPainterPath path2; path2.moveTo(startPt.x()+wid,startPt.y()+rect1.width()/2); path2.arcTo(startPt.x(),startPt.y(),wid,het,0,180); path2.lineTo(startPt.x(),startPt.y()+rect1.height()); path2.lineTo(startPt.x()+wid,startPt.y()+rect1.height()); p.fillPath(path2,QBrush(QColor(122,122,122))); return; }