QGraphicsPolygonItem 类
QGraphicsPolygonItem类提供了一个多边形项目,您可以将其添加到QGraphicsScene中。
Header: | #include < QGraphicsPolygonItem > |
---|---|
qmake: | QT += widgets |
Inherits: | QAbstractGraphicsShapeItem |
Inherited By: | |
Since: | Qt 4.2 |
详述
要设置项目的多边形,请将QPolygonF传递给QGraphicsPolygonItem的构造函数,或调用setPolygon() 函数。 polygon() 函数返回当前的多边形。
QGraphicsPolygonItem使用多边形和笔宽来提供boundingRect() ,shape() 和contains() 的合理实现。 paint() 函数使用项目的关联笔刷绘制多边形,您可以通过调用setPen() 和setBrush() 函数进行设置。
参见QGraphicsPathItem, qgraphicsrectem, QGraphicsEllipseItem, QGraphicsTextItem, QGraphicsLineItem, QGraphicsPixmapItem,和图形视图框架。
公共类型
enum anonymous
由virtual type()函数返回的值。
Constant | Value | Description |
---|---|---|
QGraphicsPolygonItem::Type | 5 | 图形多边形项 |
公共函数
构造和析构
- QGraphicsPolygonItem(const QPolygonF &polygon, QGraphicsItem *parent = nullptr)
- QGraphicsPolygonItem(QGraphicsItem *parent = nullptr)
- virtual ~QGraphicsPolygonItem()
多边形
- QPolygonF polygon() const
- void setPolygon(const QPolygonF &polygon)
填充规则
- Qt::FillRule fillRule() const
- void setFillRule(Qt::FillRule rule)
重写的公共函数
- 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 QGraphicsPolygonItem;
gi->setFlag (QGraphicsItem::ItemIsMovable);
QPolygonF pf;
pf << QPointF(-55, -35) << QPointF(55,35)<< QPointF(55,-35)<< QPointF(-55,35);
gi->setPolygon (pf);
QLinearGradient gLg(-55,-35,55,35);
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 (15,5);
scene.addItem (gi);
view.setScene (&scene);
view.setRenderHint (QPainter::Antialiasing);
view.setWindowFlag (Qt::WindowMinMaxButtonsHint,false);
view.show ();
// view.setWindowOpacity (0.8);
return a.exec();
}