QPointF Class

QPointF 类使用浮点精度定义平面中的一个点。更多

头文件: #include <QPointF>
CMake: find_package(Qt6 REQUIRED COMPONENTS Core)
target_link_libraries(mytarget PRIVATE Qt6::Core)
qmake: QT += core

该类可等价比较

该类可与QPoint 进行等价比较

注意:该类中的所有函数都是可重入的

公共函数

QPointF()
QPointF(const QPoint &point)
QPointF(qreal xpos, qreal ypos)
bool isNull() const
qreal manhattanLength() const
qreal &rx()
qreal &ry()
void setX(qreal x)
void setY(qreal y)
CGPoint toCGPoint() const
QPoint toPoint() const
QPointF transposed() const
qreal x() const
qreal y() const
QPointF &operator*=(qreal factor)
QPointF &operator+=(const QPointF &point)
QPointF &operator-=(const QPointF &point)
QPointF &operator/=(qreal divisor)

静态公共成员

qreal dotProduct(const QPointF &p1, const QPointF &p2)
QPointF fromCGPoint(CGPoint point)
(since 6.8) bool qFuzzyCompare(const QPointF &p1, const QPointF &p2)
(since 6.8) bool qFuzzyIsNull(const QPointF &point)
bool operator!=(const QPointF &lhs, const QPointF &rhs)
QPointF operator*(const QPointF &point, qreal factor)
QPointF operator*(qreal factor, const QPointF &point)
QPointF operator+(const QPointF &point)
QPointF operator+(const QPointF &p1, const QPointF &p2)
QPointF operator-(const QPointF &p1, const QPointF &p2)
QPointF operator-(const QPointF &point)
QPointF operator/(const QPointF &point, qreal divisor)
QDataStream &operator<<(QDataStream &stream, const QPointF &point)
bool operator==(const QPointF &lhs, const QPointF &rhs)
QDataStream &operator>>(QDataStream &stream, QPointF &point)

详细说明

点由 x 坐标和 y 坐标指定,可使用x() 和y() 函数访问这两个坐标。点的坐标使用有限浮点数指定,以确保准确性。如果 x 和 y 都设置为 0.0,则isNull() 函数返回true 。可以使用setX() 和setY() 函数设置(或更改)坐标,或者使用rx() 和ry() 函数返回坐标引用(允许直接操作)。

给定一个点p,以下语句都是等价的:

QPointF p;

p.setX(p.x() + 1.0);
p += QPointF(1.0, 0.0);
p.rx()++;

QPointF 对象也可用作矢量:加法和减法的定义与向量相同(每个分量分别相加)。QPointF 对象也可以被intqreal 除或乘。

此外,QPointF 类还提供了一个将QPoint 对象转换为 QPointF 对象的构造函数,以及一个相应的toPoint() 函数,该函数可返回该点QPoint 副本。最后,QPointF 对象可以进行流式传输和比较。

另请参阅 QPointQPolygonF

成员函数文档

[constexpr noexcept] QPointF::QPointF()

构造一个空点,即坐标为 (0.0, 0.0) 的点

另请参见 isNull().

[constexpr noexcept] QPointF::QPointF(const QPoint &point)

构造给定point 的副本。

另请参阅 toPoint() 和QPoint::toPointF()。

[constexpr noexcept] QPointF::QPointF(qreal xpos, qreal ypos)

以给定的坐标 (xpos,ypos) 构建一个点。

另请参见 setX() 和setY() 。

[static constexpr] qreal QPointF::dotProduct(const QPointF &p1, const QPointF &p2)

QPointF p( 3.1, 7.1);
QPointF q(-1.0, 4.1);
qreal dotProduct = QPointF::dotProduct(p, q);   // dotProduct becomes 26.01

返回p1p2 的点积。

[static noexcept] QPointF QPointF::fromCGPoint(CGPoint point)

从 CGPointpoint 创建QRectF

另请参阅 toCGPoint() 。

[noexcept] bool QPointF::isNull() const

如果 x 坐标和 y 坐标都设置为 0.0,则返回true (忽略符号);否则返回false

[constexpr] qreal QPointF::manhattanLength() const

返回x() 和y() 的绝对值之和,传统上称为从原点到点的矢量的 "曼哈顿长度"。

另请参见 QPoint::manhattanLength()。

[constexpr noexcept] qreal &QPointF::rx()

返回该点 x 坐标的引用。

使用引用可以直接操作 x 坐标:

 QPointF p(1.1, 2.5);
 p.rx()--;   // p becomes (0.1, 2.5)

另请参见 x() 和setX()。

[constexpr noexcept] qreal &QPointF::ry()

返回该点 y 坐标的引用。

使用引用可以直接操作 y 坐标:

QPointF p(1.1, 2.5);
p.ry()++;   // p becomes (1.1, 3.5)

另请参见 y() 和setY()。

[constexpr noexcept] void QPointF::setX(qreal x)

将此点的 x 坐标设置为给定的有限x 坐标。

另请参阅 x() 和setY()。

[constexpr noexcept] void QPointF::setY(qreal y)

将此点的 y 坐标设置为给定的有限y 坐标。

另请参阅 y() 和setX()。

[noexcept] CGPoint QPointF::toCGPoint() const

QPointF 创建 CGPoint。

另请参阅 fromCGPoint().

[constexpr] QPoint QPointF::toPoint() const

将该点的坐标舍入为最接近的整数,并返回一个带有舍入坐标的QPoint 对象。

另请参见 QPointF() 和QPoint::toPointF()。

[constexpr noexcept] QPointF QPointF::transposed() const

返回一个交换了 x 和 y 坐标的点:

QPointF{1.0, 2.0}.transposed() // {2.0, 1.0}

另请参阅 x()、y()、setX() 和setY()。

[constexpr noexcept] qreal QPointF::x() const

返回该点的 x 坐标。

另请参阅 setX() 和rx()。

[constexpr noexcept] qreal QPointF::y() const

返回该点的 Y 坐标。

另请参阅 setY() 和ry()。

[constexpr] QPointF &QPointF::operator*=(qreal factor)

将此点的坐标与给定的有限factor 相乘,并返回此点的引用。例如

QPointF p(-1.1, 4.1);
p *= 2.5;    // p becomes (-2.75, 10.25)

另请参见 operator/=().

[constexpr] QPointF &QPointF::operator+=(const QPointF &point)

将给定的point 添加到此点,并返回此点的引用。例如

QPointF p( 3.1, 7.1);
QPointF q(-1.0, 4.1);
p += q;    // p becomes (2.1, 11.2)

另请参见 operator-=().

[constexpr] QPointF &QPointF::operator-=(const QPointF &point)

从该点减去给定的point ,并返回对该点的引用。例如

QPointF p( 3.1, 7.1);
QPointF q(-1.0, 4.1);
p -= q;    // p becomes (4.1, 3.0)

另请参见 operator+=().

[constexpr] QPointF &QPointF::operator/=(qreal divisor)

用给定的divisor 除以 x 和 y,并返回对该点的引用。例如

QPointF p(-2.75, 10.25);
p /= 2.5;           // p becomes (-1.1, 4.1)

divisor 不能为零或 NaN。

另请参阅 operator*=() 。

相关非成员

[constexpr noexcept, since 6.8] bool qFuzzyCompare(const QPointF &p1, const QPointF &p2)

如果p1 近似等于p2 ,则返回true ;否则返回false

此函数在 Qt 6.8 中引入。

另请参见 qFuzzyIsNull

[constexpr noexcept, since 6.8] bool qFuzzyIsNull(const QPointF &point)

如果point 近似等于点(0.0, 0.0) ,则返回true

此函数在 Qt 6.8 中引入。

另请参见 qFuzzyCompare

[constexpr noexcept] bool operator!=(const QPointF &lhs, const QPointF &rhs)

如果lhsrhs 充分不同,则返回true ;否则返回false

警告: 此函数不检查严格不等式;而是使用模糊比较来比较点的坐标。

另请参见 qFuzzyCompare

[constexpr] QPointF operator*(const QPointF &point, qreal factor)

返回给定point 的副本,并乘以给定的有限factor

另请参阅 QPointF::operator*=() 。

[constexpr] QPointF operator*(qreal factor, const QPointF &point)

这是一个重载函数。

返回给定point 乘以给定有限factor 的副本。

[constexpr] QPointF operator+(const QPointF &point)

返回未修改的point

[constexpr] QPointF operator+(const QPointF &p1, const QPointF &p2)

返回QPointF 对象,该对象是给定点p1p2 的总和;每个分量分别相加。

另请参见 QPointF::operator+=().

[constexpr] QPointF operator-(const QPointF &p1, const QPointF &p2)

返回一个QPointF 对象,该对象由p2p1 相减而得,每个部分分别相减。

另请参阅 QPointF::operator-=().

[constexpr] QPointF operator-(const QPointF &point)

这是一个重载函数。

返回一个QPointF 对象,该对象是通过改变给定point 中每个分量的符号形成的。

等价于QPointF(0,0) - point

[constexpr] QPointF operator/(const QPointF &point, qreal divisor)

返回QPointF 对象,该对象由给定的point 的每个分量除以给定的divisor 形成。

divisor 不得为零或 NaN。

另请参阅 QPointF::operator/=()。

QDataStream &operator<<(QDataStream &stream, const QPointF &point)

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

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

[constexpr noexcept] bool operator==(const QPointF &lhs, const QPointF &rhs)

如果lhs 近似等于rhs ,则返回true ;否则返回false

警告: 此函数不检查严格的相等;而是使用模糊比较来比较点的坐标。

另请参见 qFuzzyCompare

QDataStream &operator>>(QDataStream &stream, QPointF &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.