QT绘制不含线段的圆弧添加到QGraphicsScene场景中

实现步骤:

要去除圆心到弧线的线段,可以这样实现:

  1. 创建一个QPainterPath对象。
  2. 使用QPainterPath的arcMoveTo()方法来移动到圆弧的起点。
  3. 使用QPainterPath的arcTo()方法来添加一个圆弧。
  4. 创建一个QGraphicsPathItem对象,并将QPainterPath对象传递给它的setPath()方法。
  5. 最后setPen(pen)设置画笔,将项添加到场景中scene->addItem(item)。
    下面是示范代码:
#include <QGraphicsScene>
#include <QGraphicsView>
#include <QGraphicsRectItem>
#include <QPainter>
//创建场景
QGraphicsView *view = new QGraphicsView(this);
view->setBackgroundBrush(Qt::black);
QRect viewRect = this->geometry();
QGraphicsScene *scene = new QGraphicsScene();
scene->setSceneRect(-viewRect.width() / 2, -viewRect.height() / 2, viewRect.width() - 2, viewRect.height() - 2);//设置原点和scene大小
view->setScene(scene);
view->resize(viewRect.width(), viewRect.height());
view->show();

QPen m_pen();   // 定义一个画笔,设置画笔颜色和宽度
m_pen.setColor(QColor(255, 255, 255));
m_pen.setWidth(2);

//绘制圆弧路径
QPainterPath path;
path.arcMoveTo(0, 0, 100, 100, startAngle);
path.arcTo(0, 0, 100, 100, startAngle, spanAngle);

QGraphicsPathItem *item = new QGraphicsPathItem;
item->setPath(path);
item->setPen(m_pen);
scene->addItem(item); // 将项添加到场景中

绘图部分参数说明:

在使用QPainterPath的arcTo()方法时,需要传递5个参数,分别是x、y、width、height和startAngle。其中,x和y表示圆弧所在矩形的左上角坐标,width和height表示矩形的宽度和高度,startAngle表示圆弧的起始角度,以度为单位。

在使用QPainterPath的arcMoveTo()方法时,需要传递4个参数,分别是x、y、width、height和startAngle。其中,x和y表示圆弧所在矩形的左上角坐标,width和height表示矩形的宽度和高度,startAngle表示圆弧的起始角度,以度为单位。
与arcTo()方法不同的是,arcMoveTo()方法只是移动到圆弧的起点,并不添加圆弧。

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值