一、描述

QPolygon继承自是QVector <QPoint>。表示多边形的点集。除了QVector提供的功能外,QPolygon还提供了一些特定于点的功能。

二、成员函数

1、QPolygon(const QRect &rectangle, bool closed = false)

从给定的矩形构造一个多边形。如果closed为true,则多边形的第五个点设置为rectangle.topLeft()。

2、QPolygon(const QVector<QPoint> &points)

从点集构造一个多边形。

3、QPolygon(int size)

构造一个多边形,含有size个空点(QPoint(0,0))。

4、QPolygon()

构造一个多边形,不含点。

5、QRect boundingRect()

多边形的外接矩形。如果多边形为空则返回QRect(0, 0, 0, 0)。

    QPainter painter(this);
    painter.setPen(QPen(Qt::cyan, 3, Qt::DashDotLine));
    
    QPolygon polygon = QPolygon();
    polygon << QPoint(22,33)<<QPoint(78,45)<<QPoint(111,233)<<QPoint(234,88)<<QPoint(34,89);
    painter.drawPolygon(polygon);
    
    painter.setPen(QPen(Qt::red, 3, Qt::DashDotLine));
    painter.drawRect(polygon.boundingRect());

6、bool containsPoint(const QPoint &point, Qt::FillRule fillRule)

点是否在多边形范围之内。

关于Qt::FillRule参考:Qt::FillRule

7、QPolygon intersected(const QPolygon &r)

获取多边形的交集。

    QPainter painter(this);
    painter.setPen(QPen(Qt::cyan, 3, Qt::DashDotLine));

    QPolygon polygon = QPolygon();
    polygon << QPoint(22,33)<<QPoint(78,45)<<QPoint(111,233)<<QPoint(234,88)<<QPoint(34,89);
    painter.drawPolygon(polygon);

    painter.setPen(QPen(Qt::red, 3, Qt::DashDotLine));

    QPolygon polygon2 = QPolygon();
    polygon2 << QPoint(122,133)<<QPoint(66,99)<<QPoint(144,128);
    painter.drawPolygon(polygon2);

    QPainterPath path;
    path.addPolygon(polygon.intersected(polygon2));
    painter.fillPath(path,Qt::blue);

 

8、bool intersects(const QPolygon &p)

如果当前多边形与给定多边形p的任何点相交,则返回true。如果当前多边形包含或被p的任何部分包含,也返回true。

9、void point(int index, int *x, int *y) / QPoint point(int index)

获取第index个点。

10、void putPoints(int index, int nPoints, int firstx, int firsty, ...)

将变量参数列表中的点从给定索引添加到当前多边形中。参数1是添加起始的索引,参数2是添加的点的个数,参数3、4是添加的第1个点的x、y... 

    QPolygon polygon3(1);
    polygon3[0] = QPoint(4, 5);
    polygon3.putPoints(1, 2, 6,7, 8,9);
    qDebug()<<polygon3;

11、void putPoints(int index, int nPoints, const QPolygon &fromPolygon, int fromIndex = 0)

将另一个多边形A的点添加到当前多边形B,参数3是当前多边形A复制的起始索引。

12、void setPoint(int index, const QPoint &point)

设置某点。

13、void setPoints(int nPoints, const int *points)

根据数组设置多边形的点。参数1是设置点的个数。

    static const int points[] = { 10, 20, 30, 40 };
    QPolygon polygon;
    polygon.setPoints(2, points);

14、void setPoints(int n, int firstx, int firsty, ...)

设置n个点...

15、QPolygon subtracted(const QPolygon &r)

返回多边形减去一个多边形的结果。

    QPainter painter(this);
    painter.setPen(QPen(Qt::cyan, 3, Qt::DashDotLine));

    QPolygon polygon = QPolygon();
    polygon << QPoint(22,33)<<QPoint(78,45)<<QPoint(111,233)<<QPoint(234,88)<<QPoint(34,89);
    painter.drawPolygon(polygon);

    painter.setPen(QPen(Qt::red, 3, Qt::DashDotLine));

    QPolygon polygon2 = QPolygon();
    polygon2 << QPoint(122,133)<<QPoint(66,99)<<QPoint(144,128);
    painter.drawPolygon(polygon2);

    QPainterPath path;
    path.addPolygon(polygon.subtracted(polygon2));
    painter.fillPath(path,Qt::blue);

16、void translate(const QPoint &offset)

平移多边形。

    QPainter painter(this);
    painter.setPen(QPen(Qt::red, 3, Qt::DashDotLine));

    QPolygon polygon2 = QPolygon();
    polygon2 << QPoint(122,133)<<QPoint(66,99)<<QPoint(144,128);
    painter.drawPolygon(polygon2);

    polygon2.translate(20,20);
    painter.setPen(QPen(Qt::green, 3, Qt::DashDotLine));
    painter.drawPolygon(polygon2);

17、QPolygon translated(const QPoint &offset)

返回平移多边形之后的副本,不影响原多边形。

18、QPolygon united(const QPolygon &r)

获取当前多边形和参数多边形的并集。

    QPainter painter(this);
    painter.setPen(QPen(Qt::cyan, 3, Qt::DashDotLine));

    QPolygon polygon = QPolygon();
    polygon << QPoint(22,33)<<QPoint(78,45)<<QPoint(111,233)<<QPoint(234,88)<<QPoint(34,89);
    painter.drawPolygon(polygon);

    painter.setPen(QPen(Qt::red, 3, Qt::DashDotLine));

    QPolygon polygon2 = QPolygon();
    polygon2 << QPoint(122,133)<<QPoint(66,99)<<QPoint(144,128);
    painter.drawPolygon(polygon2);

    painter.setPen(QPen(Qt::green, 3, Qt::DashDotLine));
    painter.drawPolygon(polygon.united(polygon2));

Logo

腾讯云面向开发者汇聚海量精品云计算使用和开发经验,营造开放的云计算技术生态圈。

更多推荐