QPoint Class
QPoint 类使用整数精度定义平面中的一个点。更多
头文件: | #include <QPoint> |
CMake: | find_package(Qt6 REQUIRED COMPONENTS Core) target_link_libraries(mytarget PRIVATE Qt6::Core) |
qmake: | QT += core |
- 所有成员(包括继承成员)的列表
- QPoint 属于绘画类。
该类可等价比较。
该类可与QPointF 进行等价比较。
注意:该类中的所有函数都是可重入的。
公共函数
QPoint() | |
QPoint(int xpos, int ypos) | |
bool | isNull() const |
int | manhattanLength() const |
int & | rx() |
int & | ry() |
void | setX(int x) |
void | setY(int y) |
CGPoint | toCGPoint() const |
(since 6.4) QPointF | toPointF() const |
QPoint | transposed() const |
int | x() const |
int | y() const |
QPoint & | operator*=(double factor) |
QPoint & | operator*=(float factor) |
QPoint & | operator*=(int factor) |
QPoint & | operator+=(const QPoint &point) |
QPoint & | operator-=(const QPoint &point) |
QPoint & | operator/=(qreal divisor) |
静态公共成员
int | dotProduct(const QPoint &p1, const QPoint &p2) |
相关非成员
bool | operator!=(const QPoint &lhs, const QPoint &rhs) |
QPoint | operator*(const QPoint &point, double factor) |
QPoint | operator*(const QPoint &point, float factor) |
QPoint | operator*(const QPoint &point, int factor) |
QPoint | operator*(double factor, const QPoint &point) |
QPoint | operator*(float factor, const QPoint &point) |
QPoint | operator*(int factor, const QPoint &point) |
QPoint | operator+(const QPoint &point) |
QPoint | operator+(const QPoint &p1, const QPoint &p2) |
QPoint | operator-(const QPoint &p1, const QPoint &p2) |
QPoint | operator-(const QPoint &point) |
QPoint | operator/(const QPoint &point, qreal divisor) |
QDataStream & | operator<<(QDataStream &stream, const QPoint &point) |
bool | operator==(const QPoint &lhs, const QPoint &rhs) |
QDataStream & | operator>>(QDataStream &stream, QPoint &point) |
详细说明
点由 x 坐标和 y 坐标指定,可使用x() 和y() 函数访问这两个坐标。如果 x 和 y 都设置为 0,则isNull() 函数返回true
。可以使用setX() 和setY() 函数设置(或更改)坐标,或者使用rx() 和ry() 函数返回坐标引用(允许直接操作)。
给定一个点p,下面的语句都是等价的:
QPoint 对象也可用作矢量:加法和减法的定义与向量相同(每个分量分别相加)。QPoint 对象还可以被int
或qreal
除或乘。
此外,QPoint 类还提供了manhattanLength() 函数,该函数提供了 QPoint 对象长度的低成本近似值,并将其解释为矢量。最后,QPoint 对象可以进行流式传输和比较。
成员函数文档
[constexpr noexcept]
QPoint::QPoint()
构造一个空点,即坐标为 (0, 0) 的点
另请参见 isNull().
[constexpr noexcept]
QPoint::QPoint(int xpos, int ypos)
以给定的坐标 (xpos,ypos) 构建一个点。
[static constexpr]
int QPoint::dotProduct(const QPoint &p1, const QPoint &p2)
QPoint p( 3, 7); QPoint q(-1, 4); int dotProduct = QPoint::dotProduct(p, q); // dotProduct becomes 25
返回p1 和p2 的点积。
[constexpr noexcept]
bool QPoint::isNull() const
如果 x 坐标和 y 坐标都设置为 0,则返回true
,否则返回false
。
[constexpr]
int QPoint::manhattanLength() const
返回x() 和y() 的绝对值之和,传统上称为从原点到点的矢量的 "曼哈顿长度"。例如
QPoint oldPosition; MyWidget::mouseMoveEvent(QMouseEvent *event) { QPoint point = event->pos() - oldPosition; if (point.manhattanLength() > 3) // the mouse has moved more than 3 pixels since the oldPosition }
这是一个有用且计算快捷的真实长度近似值:
double trueLength = std::sqrt(std::pow(x(), 2) + std::pow(y(), 2));
之所以有 "曼哈顿长度 "的传统,是因为这种距离适用于只能在矩形网格(如曼哈顿的街道)上行驶的旅行者。
[constexpr noexcept]
int &QPoint::rx()
返回该点 x 坐标的引用。
使用引用可以直接操作 x 坐标:
QPoint p(1, 2); p.rx()--; // p becomes (0, 2)
[constexpr noexcept]
int &QPoint::ry()
返回该点 y 坐标的引用。
使用引用可以直接操作 y 坐标:
QPoint p(1, 2); p.ry()++; // p becomes (1, 3)
[constexpr noexcept]
void QPoint::setX(int x)
将此点的 x 坐标设置为给定的x 坐标。
[constexpr noexcept]
void QPoint::setY(int y)
将此点的 Y 坐标设置为给定的y 坐标。
[noexcept]
CGPoint QPoint::toCGPoint() const
从QPoint 创建 CGPoint。
另请参阅 QPointF::fromCGPoint().
[constexpr noexcept, since 6.4]
QPointF QPoint::toPointF() const
将此点返回为浮点精度的点。
此函数在 Qt 6.4 中引入。
另请参阅 QPointF::toPoint()。
[constexpr noexcept]
QPoint QPoint::transposed() const
返回一个交换了 x 和 y 坐标的点:
QPoint{1, 2}.transposed() // {2, 1}
[constexpr noexcept]
int QPoint::x() const
返回该点的 x 坐标。
[constexpr noexcept]
int QPoint::y() const
返回该点的 Y 坐标。
[constexpr]
QPoint &QPoint::operator*=(double factor)
将此点的坐标与给定的factor 相乘,并返回此点的引用。例如
QPoint p(-1, 4); p *= 2.5; // p becomes (-3, 10)
请注意,由于点是以整数形式保存的,因此结果会四舍五入为最接近的整数。使用QPointF 获取浮点精度。
另请参阅 operator/=() 。
[constexpr]
QPoint &QPoint::operator*=(float factor)
将此点的坐标与给定的factor 相乘,并返回此点的引用。
请注意,由于点是以整数形式保存的,因此结果会四舍五入为最接近的整数。使用QPointF 可获得浮点精度。
另请参阅 operator/=() 。
[constexpr]
QPoint &QPoint::operator*=(int factor)
将此点的坐标与给定的factor 相乘,并返回此点的引用。
另请参见 operator/=().
[constexpr]
QPoint &QPoint::operator+=(const QPoint &point)
将给定的point 添加到此点,并返回此点的引用。例如
另请参见 operator-=().
[constexpr]
QPoint &QPoint::operator-=(const QPoint &point)
从该点减去给定的point ,并返回对该点的引用。例如
另请参见 operator+=().
[constexpr]
QPoint &QPoint::operator/=(qreal divisor)
这是一个重载函数。
用给定的divisor 除以 x 和 y,并返回对该点的引用。例如
QPoint p(-3, 10); p /= 2.5; // p becomes (-1, 4)
请注意,由于点是以整数形式保存的,因此结果会四舍五入为最接近的整数。使用QPointF 可获得浮点精度。
另请参阅 operator*=() 。
相关非会员
[constexpr noexcept]
bool operator!=(const QPoint &lhs, const QPoint &rhs)
如果lhs 和rhs 不相等,则返回true
;否则返回false
。
[constexpr]
QPoint operator*(const QPoint &point, double factor)
返回给定point 乘以给定factor 的副本。
请注意,由于点是以整数形式保存的,因此结果会四舍五入为最接近的整数。使用QPointF 可获得浮点精度。
另请参阅 QPoint::operator*=() 。
[constexpr]
QPoint operator*(const QPoint &point, float factor)
返回给定point 乘以给定factor 的副本。
请注意,由于点是以整数形式保存的,因此结果会四舍五入到最接近的整数。使用QPointF 可获得浮点精度。
另请参阅 QPoint::operator*=() 。
[constexpr noexcept]
QPoint operator*(const QPoint &point, int factor)
返回给定point 乘以给定factor 的副本。
另请参阅 QPoint::operator*=() 。
[constexpr]
QPoint operator*(double factor, const QPoint &point)
这是一个重载函数。
返回给定point 乘以给定factor 的副本。
请注意,由于点是以整数形式保存的,因此结果会四舍五入为最接近的整数。使用QPointF 可获得浮点精度。
另请参阅 QPoint::operator*=() 。
[constexpr]
QPoint operator*(float factor, const QPoint &point)
这是一个重载函数。
返回给定point 乘以给定factor 的副本。
请注意,由于点是以整数形式保存的,因此结果会四舍五入为最接近的整数。使用QPointF 可获得浮点精度。
另请参阅 QPoint::operator*=() 。
[constexpr noexcept]
QPoint operator*(int factor, const QPoint &point)
这是一个重载函数。
返回给定point 乘以给定factor 的副本。
另请参阅 QPoint::operator*=() 。
[constexpr noexcept]
QPoint operator+(const QPoint &point)
返回未修改的point 。
[constexpr noexcept]
QPoint operator+(const QPoint &p1, const QPoint &p2)
返回QPoint 对象,该对象是给定点p1 和p2 的总和;每个分量分别相加。
另请参见 QPoint::operator+=().
[constexpr noexcept]
QPoint operator-(const QPoint &p1, const QPoint &p2)
返回一个QPoint 对象,该对象由p2 与p1 相减而得,每个部分分别相减。
另请参阅 QPoint::operator-=().
[constexpr noexcept]
QPoint operator-(const QPoint &point)
这是一个重载函数。
返回QPoint 对象,该对象是通过改变给定point 的两个分量的符号形成的。
等价于QPoint(0,0) - point
。
[constexpr]
QPoint operator/(const QPoint &point, qreal divisor)
返回QPoint ,即用给定的point 的两个分量除以给定的divisor 。
请注意,由于点是以整数形式保存的,因此结果会四舍五入为最接近的整数。使用QPointF 可获得浮点精度。
另请参阅 QPoint::operator/=() 。
QDataStream &operator<<(QDataStream &stream, const QPoint &point)
将给定的point 写入给定的stream ,并返回对该数据流的引用。
另请参阅 序列化 Qt 数据类型。
[constexpr noexcept]
bool operator==(const QPoint &lhs, const QPoint &rhs)
如果lhs 和rhs 相等,则返回true
;否则返回false
。
QDataStream &operator>>(QDataStream &stream, QPoint &point)
将一个点从给定的stream 读入给定的point ,并返回对该数据流的引用。
另请参阅 序列化 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.