本页内容

QPolygon Class

QPolygon 类提供了一个采用整数精度的点列表。更多内容...

头文件: #include <QPolygon>
CMake: find_package(Qt6 REQUIRED COMPONENTS Gui)
target_link_libraries(mytarget PRIVATE Qt6::Gui)
qmake: QT += gui
继承自: QList

注意:本类中的所有函数均为可重入的。

公共函数

QPolygon()
QPolygon(const QList<QPoint> &points)
QPolygon(const QRect &rectangle, bool closed = false)
QRect boundingRect() const
bool containsPoint(const QPoint &point, Qt::FillRule fillRule) const
QPolygon intersected(const QPolygon &r) const
bool intersects(const QPolygon &p) const
void point(int index, int *x, int *y) const
QPoint point(int index) const
void putPoints(int index, int nPoints, int firstx, int firsty, ...)
void putPoints(int index, int nPoints, const QPolygon &fromPolygon, int fromIndex = 0)
void setPoint(int index, int x, int y)
void setPoint(int index, const QPoint &point)
void setPoints(int nPoints, const int *points)
void setPoints(int nPoints, int firstx, int firsty, ...)
QPolygon subtracted(const QPolygon &r) const
void swap(QPolygon &other)
(since 6.4) QPolygonF toPolygonF() const
void translate(int dx, int dy)
void translate(const QPoint &offset)
QPolygon translated(int dx, int dy) const
QPolygon translated(const QPoint &offset) const
QPolygon united(const QPolygon &r) const
operator QVariant() const
QDataStream &operator<<(QDataStream &stream, const QPolygon &polygon)
QDataStream &operator>>(QDataStream &stream, QPolygon &polygon)

详细说明

QPolygon 对象是一个QList<QPoint>。向 QPolygon 添加点的最简单方法是使用QList 的流操作符,如下所示:

QPolygon polygon;
polygon << QPoint(10, 20) << QPoint(20, 30);

除了QList 提供的函数外,QPolygon还提供了一些针对点的功能。

可以通过将索引传递给point() 函数来获取多边形中的每个点。要填充多边形, QPolygon 提供了setPoint() 函数来设置指定索引处的点,setPoints() 函数来设置多边形中的所有点(并将其调整为指定数量的点),以及putPoints() 函数,该函数将指定数量的点从指定索引处复制到多边形中(必要时会调整多边形大小)。

QPolygon 提供了用于几何操作的boundingRect() 和translate() 函数。若需对 QPolygon 进行更通用的变换,请使用QTransform::map() 函数。

QPolygon 类是隐式共享的。

另请参阅 QList 、QPolygonF 和QLine 。

成员函数文档

[constexpr noexcept default] QPolygon::QPolygon()

构建一个没有顶点的多边形。

另请参阅 QList::isEmpty()。

QPolygon::QPolygon(const QList<QPoint> &points)

构建一个包含指定points 的多边形。

另请参阅 setPoints()。

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

根据给定的rectangle 构建一个多边形。如果closed 为false,则该多边形仅包含矩形按顺时针方向排列的四个点;否则,多边形的第五个点将设置为rectangle.topLeft()。

请注意,矩形的右下角位于 (rectangle.x() + rectangle.width(), rectangle.y() + rectangle.height()) 处。

另请参阅 setPoints()。

QRect QPolygon::boundingRect() const

返回多边形的包围矩形;如果多边形为空,则返回QRect(0, 0, 0, 0)。

另请参阅 QList::isEmpty()。

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

如果给定的point 根据指定的fillRule 位于多边形内部,则返回true ;否则返回false 。

QPolygon QPolygon::intersected(const QPolygon &r) const

返回一个多边形,该多边形是本多边形与r 的交集。

对多边形的集合运算将把多边形视为区域。非闭合多边形将被隐式地视为闭合。

另请参阅 intersects()。

bool QPolygon::intersects(const QPolygon &p) const

如果当前多边形与给定多边形p 在任意一点发生相交,则返回true 。此外,如果当前多边形包含p 的任意部分,或者被其任意部分包含,则返回true 。

多边形的集合运算将把多边形视为区域。非闭合多边形将被隐式视为闭合。

另请参阅 intersected()。

void QPolygon::point(int index, int *x, int *y) const

将给定坐标index 所对应的点坐标提取到*x 和*y 中(如果它们是有效的指针)。

另请参阅 setPoint()。

QPoint QPolygon::point(int index) const

返回位于给定坐标index 处的点。

这是一个重载函数。

void QPolygon::putPoints(int index, int nPoints, int firstx, int firsty, ...)

将变量参数列表中的nPoints 个点复制到由给定index 定义的多边形中。

这些点以整数序列的形式给出,从firstx 开始,接着是firsty ,依此类推。如果index+nPoints 超过多边形的当前尺寸,则会调整多边形的大小。

示例代码通过将多边形从 1 个点扩展到 3 个点,创建了一个由三个点 (4,5)、(6,7) 和 (8,9) 组成的多边形:

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

以下代码的结果相同,但此处的 putPoints() 函数会覆盖原有点而非扩展:

QPolygon polygon(3);
polygon.putPoints(0, 3, 4,5, 0,0, 8,9);
polygon.putPoints(1, 1, 6,7);

另请参阅 setPoints()。

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

将nPoints 中的点从给定的fromIndex (默认为0)在fromPolygon 中复制到该多边形中,并从指定的index 开始。例如:

QPolygon polygon1;
polygon1.putPoints(0, 3, 1,2, 0,0, 5,6);
// polygon1 is now the three-point polygon(1,2, 0,0, 5,6);

QPolygon polygon2;
polygon2.putPoints(0, 3, 4,4, 5,5, 6,6);
// polygon2 is now (4,4, 5,5, 6,6);

polygon1.putPoints(2, 3, polygon2);
// polygon1 is now the five-point polygon(1,2, 0,0, 4,4, 5,5, 6,6);

这是一个重载函数。

void QPolygon::setPoint(int index, int x, int y)

将位于给定坐标index 的点设置为由 (x,y) 指定的点。

另请参阅 point()、putPoints() 和setPoints()。

void QPolygon::setPoint(int index, const QPoint &point)

将位于给定坐标index 处的点设置为给定的point 。

这是一个重载函数。

void QPolygon::setPoints(int nPoints, const int *points)

将多边形调整为nPoints 的大小,并用给定的points 填充其内部。

示例代码创建了一个由两个点 (10, 20) 和 (30, 40) 组成的多边形:

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

另请参阅 setPoint() 和putPoints()。

void QPolygon::setPoints(int nPoints, int firstx, int firsty, ...)

将多边形调整为nPoints 大小,并使用变量参数列表中指定的点填充该多边形。这些点以整数序列的形式给出,从firstx 开始,接着是firsty ,以此类推。

以下示例代码创建了一个由两个点 (10, 20) 和 (30, 40) 组成的多边形:

QPolygon polygon;
polygon.setPoints(2, 10, 20, 30, 40);

这是一个重载函数。

QPolygon QPolygon::subtracted(const QPolygon &r) const

返回一个多边形,该多边形由当前多边形减去r 所得。

对多边形的集合运算将把多边形视为区域。非闭合多边形将被隐式地视为闭合多边形。

[noexcept] void QPolygon::swap(QPolygon &other)

将该多边形与other 互换。此操作速度极快,且绝不会失败。

[since 6.4] QPolygonF QPolygon::toPolygonF() const

将该多边形作为具有浮点精度的多边形返回。

该函数在 Qt 6.4 中引入。

另请参阅 QPolygonF::toPolygon()。

void QPolygon::translate(int dx, int dy)

将多边形中的所有点平移 (dx,dy)。

另请参阅 translated()。

void QPolygon::translate(const QPoint &offset)

将多边形中的所有点沿给定的offset 方向平移。

这是一个重载函数。

另请参阅 translated()。

QPolygon QPolygon::translated(int dx, int dy) const

返回一个经过 (dx,dy) 平移的多边形副本。

另请参阅 translate()。

QPolygon QPolygon::translated(const QPoint &offset) const

返回一个经过给定offset 平移的多边形副本。

这是一个重载函数。

另请参阅 translate()。

QPolygon QPolygon::united(const QPolygon &r) const

返回一个多边形,该多边形是本多边形与r 的并集。

对多边形的集合运算将把多边形视为区域,并隐式地将多边形闭合。

另请参阅 intersected() 和subtracted()。

QPolygon::operator QVariant() const

将多边形作为QVariant

相关非会员

QDataStream &operator<<(QDataStream &stream, const QPolygon &polygon)

将给定的polygon 写入给定的stream ,并返回该流的引用。

另请参阅 《Qt 数据类型的序列化》。

QDataStream &operator>>(QDataStream &stream, QPolygon &polygon)

将给定的stream 中的多边形读取到给定的polygon 中,并返回该流的引用。

另请参阅 《Qt 数据类型的序列化》。

© 2026 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.