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
QVariant 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() 函数。使用QTransform::map() 函数可对 QPolygon 进行更一般的变换。

QPolygon 类是隐式共享的。

另请参见 QList,QPolygonF, 和QLine

成员函数文档

[constexpr noexcept] 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

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

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

返回此多边形与r 相交的多边形。

多边形的集合操作将把多边形视为区域。非封闭多边形将被视为隐式封闭多边形。

另请参见 intersects()。

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

如果当前多边形与给定多边形相交,则返回true p 。如果当前多边形包含或被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)

这是一个重载函数。

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

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()。

QVariant QPolygon::operator QVariant() const

返回多边形的QVariant

相关非成员

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

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

另请参阅 序列化 Qt 数据类型

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

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

另请参阅 序列化 Qt 数据类型

© 2025 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.