图形视图(07):【类】QGraphicsPathItem[官翻]

QGraphicsPathItem 类

QGraphicsPathItem类提供了可以添加到QGraphicsScene的路径项。

Header:#include < QGraphicsPathItem >
qmake:QT += widgets
Inherits:QAbstractGraphicsShapeItem
Inherited By:
Since:Qt 4.2

详述

要设置项目的路径,请将QPainterPath传递给QGraphicsPathItem的构造函数,或调用setPath() 函数。

函数 path() 返回当前路径。

img

QGraphicsPathItem使用该路径来提供boundingRect() ,shape() 和contains() 的合理实现。 paint() 函数使用项目的关联笔刷绘制路径,您可以通过调用setPen() 和setBrush() 函数来设置它们。

另请参见QGraphicsRectItem,QGraphicsEllipseItem,QGraphicsPolygonItem,QGraphicsTextItem,QGraphicsLineItem,QGraphicsPixmapItem和图形视图框架。

公共类型

enum anonymous

由virtual type()函数返回的值。

ConstantValueDescription
QGraphicsPathItem::Type2图形椭圆项

公共函数

构造和析构

  1. QGraphicsPathItem(const QPainterPath &path, QGraphicsItem *parent = nullptr)
  2. QGraphicsPathItem(QGraphicsItem *parent = nullptr)
  3. virtual ~QGraphicsPathItem()

路径

  1. QPainterPath path() const
  2. void setPath(const QPainterPath &path)

重写的公共函数

  1. virtual QRectF boundingRect() const override
  2. virtual bool contains(const QPointF &point) const override
  3. virtual bool isObscuredBy(const QGraphicsItem *item) const override
  4. virtual QPainterPath opaqueArea() const override
  5. virtual void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = nullptr) override
  6. virtual QPainterPath shape() const override
  7. virtual int type() const override

演示代码

#include <QtWidgets>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    a.setApplicationName ("图形框架");

    QGraphicsView view;

    int x = -200,y = -200;
    int w = 400,h=400;

    QGraphicsScene scene(x,y,w,h);
    scene.setBackgroundBrush (QBrush(qRgb (173,241,174)));

    QPen pen(qRgb (140,159,141));
    pen.setWidth (0);

    for (int i = x + 20; i < x+w; i+=20) {
        scene.addLine (x,i,x+w,i,pen);
        scene.addLine (i,y,i,y+h,pen);
    }

    QGraphicsItemGroup xAxis;
    scene.addItem (&xAxis);
    xAxis.addToGroup (new QGraphicsLineItem(x,0,x+w,0));
    xAxis.addToGroup (new QGraphicsLineItem(x+w-10,-10,x+w,-1));
    xAxis.addToGroup (new QGraphicsLineItem(x+w-10, 10,x+w, 1));

    QGraphicsItemGroup yAxis;
    scene.addItem (&yAxis);

    yAxis.addToGroup (new QGraphicsLineItem(0,y,0,y+h));
    yAxis.addToGroup (new QGraphicsLineItem(-1, y+h,-10,y+h-10));
    yAxis.addToGroup (new QGraphicsLineItem(1 , y+h, 10,y+h-10));

    auto  *gi = new QGraphicsPathItem;
    gi->setFlag (QGraphicsItem::ItemIsMovable);
    QPainterPath path;
    path.moveTo(0, 0);
    path.cubicTo(120, -20,  60, 100,  120, 80);
    gi->setPath (path);

    QLinearGradient gLg;
    gLg.setStart (0,0);
    gLg.setFinalStop (100,100);
    gLg.setColorAt (0,qRgb (221,186,68));
    gLg.setColorAt (1,qRgb (221,226,68));
    gi->setPen (pen);
    gi->setBrush (gLg);
    gi->setOpacity (0.8);

    gi->moveBy (-60,-50);

    scene.addItem (gi);

    view.setScene (&scene);
    view.setRenderHint (QPainter::Antialiasing);
    view.setWindowFlag (Qt::WindowMinMaxButtonsHint,false);
    view.show ();
    view.setWindowOpacity (0.8);
    return a.exec();
}
  • 3
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值