Qt:基于 QGraphicsItem 绘制 箭头(用于两个图形项Item的连接)

熟练运用QGraphicsItem图形项,QGraphicsScene场景,QGraphicsView视图等类对于Qt开发绘图是必不可少的知识点。

通常会需要一些箭头来展示流程图之类的,效果如下:

arrowItem.h

class ArrowItem : public QGraphicsLineItem
{
public:
    enum { Type = UserType + 4 };

    //构造函数(两个item参数)
    QMyItem(QGraphicsPolygonItem*startI,QGraphicsPolygonItem *endI,QGraphicsItem *parent = nullptr);

    //item绘制区域的估计值
    QRectF boundingRect() const override;
    //实际绘制
    void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = nullptr)override;

    int type() const override { return Type; }

    QPainterPath shape() const override;

private:
    QGraphicsPolygonItem *m_pStartItem;
    QGraphicsPolygonItem *m_pendItem;
    QPolygonF arrowHead;

};

arrowItem.cpp(根据Qt示例所写

ArrowItem::ArrowItem(QGraphicsPolygonItem *startI, QGraphicsPolygonItem *endI,QGraphicsItem *parent):
    QGraphicsLineItem (parent)
{
    m_pendItem = endI; //起点item
    m_pStartItem = startI; //终点item
    this->setZValue(m_pStartItem->zValue() - 1); //目的:让箭头后置
    setFlag(QGraphicsItem::ItemIsSelectable);
    setPen(QPen(Qt::black, 2, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin));
}

QRectF ArrowItem::boundingRect() const
{
    //item绘制区域的估计值
    qreal extra = (this->pen().width() + 20) /2;
    return QRectF(line().p1(),QSizeF(line().p2().x()-line().p1().x(),line().p2().y() - line().p1().y()))
            .normalized().adjusted(-extra,-extra,extra,extra);
}

void ArrowItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{

    if(m_pStartItem->collidesWithItem(m_pendItem))//判断图形项是否存在相交
        return;
    QPen pen(Qt::black);
    painter->setPen(pen);

    painter->setBrush(Qt::black);
    qreal arrowsize = 10;

    QLineF centerLine(m_pStartItem->pos(),m_pendItem->pos());
    QPolygonF endPolygon = m_pendItem->polygon();//获取item的点组合(闭环路)
    QPointF p1 = endPolygon.first() + m_pendItem->pos();//item的左上方点  + item在场景的位置点(偏移得到左上点在场景中的位置)
    QPointF p2;
    QPointF intersectPoint;
    QLineF polyLine;
    for(int i = 1;i < endPolygon.count(); ++i)
    {
        p2 = endPolygon.at(i) + m_pendItem->pos();//偏移得到场景中的位置点
        polyLine = QLineF(p1,p2);//两点构成直线

        QLineF::IntersectType intersectType =
                polyLine.intersect(centerLine,&intersectPoint);//两条线比较是否有交集   存在
        if(intersectType == QLineF::BoundedIntersection)
            break;
        p1 = p2;
    }

    painter->setPen(Qt::black);
    setLine(QLineF(intersectPoint,m_pStartItem->pos()));

    double angle = std::atan2(-line().dy(),line().dx()); //反正切 [-PI,PI]
    QPointF arrowP1 = line().p1() +
            QPointF(sin(angle + M_PI / 3) * arrowsize, //计算对边
                    cos(angle + M_PI / 3) * arrowsize); //计算临边
    QPointF arrowP2 = line().p1() +
            QPointF(sin(angle + M_PI - M_PI / 3) * arrowsize,
                    cos(angle + M_PI - M_PI / 3) * arrowsize);

    arrowHead.clear();
    arrowHead<<line().p1() << arrowP1 << arrowP2;

    painter->drawLine(line());
    painter->drawPolygon(arrowHead);

}

QPainterPath ArrowItem::shape() const
{
    QPainterPath path = QGraphicsLineItem::shape();
    path.addPolygon(arrowHead);
    return path;
}

输出结果如下:

 

  • 12
    点赞
  • 103
    收藏
    觉得还不错? 一键收藏
  • 9
    评论
Qt中,使用QGraphicsItem类可以更方便地绘制图形元素,并且可以直接在场景中进行移动、缩放、旋转等操作。 要绘制带角度的椭圆,可以自定义一个继承自QGraphicsItem的类,并重载其paint函数。在paint函数中,可以使用QPainter绘制椭圆并进行旋转变换。 下面是一个示例代码,可以绘制一个倾斜的椭圆: ```cpp class MyItem : public QGraphicsItem { public: MyItem(QGraphicsItem *parent = nullptr) : QGraphicsItem(parent) {} QRectF boundingRect() const override { return QRectF(-50, -25, 100, 50); } void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) override { Q_UNUSED(option); Q_UNUSED(widget); QRectF rect = boundingRect(); painter->setBrush(Qt::NoBrush); painter->setPen(QPen(Qt::red, 2)); painter->save(); // 保存当前绘制状态 painter->translate(rect.center()); // 将坐标系移到矩形中心 painter->rotate(45); // 旋转45度 painter->translate(-rect.center()); // 将坐标系移回原点 painter->drawEllipse(rect); painter->restore(); // 恢复之前的绘制状态 } }; ``` 这段代码中,我们首先自定义了一个MyItem类,继承自QGraphicsItem,并重载了其中的boundingRect和paint函数。在paint函数中,我们使用QPainter绘制椭圆,并进行旋转变换,最后绘制完成后,再恢复之前的绘制状态。 使用这个自定义的MyItem类时,可以通过调用其setPos函数来设置其在场景中的位置,也可以通过调用其setRotation函数来设置其旋转角度。 ```cpp MyItem *item = new MyItem; item->setPos(100, 100); item->setRotation(45); QGraphicsScene *scene = new QGraphicsScene; scene->addItem(item); QGraphicsView *view = new QGraphicsView(scene); view->show(); ``` 这段代码中,我们首先创建了一个MyItem对象,设置其位置和旋转角度,并将其添加到场景中。然后创建一个QGraphicsView对象,并将场景设置给它,最后显示这个视图。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值