QGraphicsPathItem 类
QGraphicsPathItem类提供了可以添加到QGraphicsScene的路径项。
Header: | #include < QGraphicsPathItem > |
---|---|
qmake: | QT += widgets |
Inherits: | QAbstractGraphicsShapeItem |
Inherited By: | |
Since: | Qt 4.2 |
详述
要设置项目的路径,请将QPainterPath传递给QGraphicsPathItem的构造函数,或调用setPath() 函数。
函数 path() 返回当前路径。
QGraphicsPathItem使用该路径来提供boundingRect() ,shape() 和contains() 的合理实现。 paint() 函数使用项目的关联笔刷绘制路径,您可以通过调用setPen() 和setBrush() 函数来设置它们。
另请参见QGraphicsRectItem,QGraphicsEllipseItem,QGraphicsPolygonItem,QGraphicsTextItem,QGraphicsLineItem,QGraphicsPixmapItem和图形视图框架。
公共类型
enum anonymous
由virtual type()函数返回的值。
Constant | Value | Description |
---|---|---|
QGraphicsPathItem::Type | 2 | 图形椭圆项 |
公共函数
构造和析构
- QGraphicsPathItem(const QPainterPath &path, QGraphicsItem *parent = nullptr)
- QGraphicsPathItem(QGraphicsItem *parent = nullptr)
- virtual ~QGraphicsPathItem()
路径
- QPainterPath path() const
- void setPath(const QPainterPath &path)
重写的公共函数
- virtual QRectF boundingRect() const override
- virtual bool contains(const QPointF &point) const override
- virtual bool isObscuredBy(const QGraphicsItem *item) const override
- virtual QPainterPath opaqueArea() const override
- virtual void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = nullptr) override
- virtual QPainterPath shape() const override
- 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();
}