本页内容

QLineF Class

QLineF 类提供了一个使用浮点精度的二维向量。更多内容...

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

QLineF 比较

类别可比较类型
相等性QLineF
相等性QLine.

公共类型

enum IntersectionType { NoIntersection, UnboundedIntersection, BoundedIntersection }

公共函数

QLineF()
QLineF(const QLine &line)
QLineF(const QPointF &p1, const QPointF &p2)
QLineF(qreal x1, qreal y1, qreal x2, qreal y2)
QPointF p1() const
QPointF p2() const
qreal x1() const
qreal x2() const
qreal y1() const
qreal y2() const
qreal angle() const
qreal angleTo(const QLineF &line) const
QPointF center() const
qreal dx() const
qreal dy() const
QLineF::IntersectionType intersects(const QLineF &line, QPointF *intersectionPoint = nullptr) const
bool isNull() const
qreal length() const
QLineF normalVector() const
QPointF pointAt(qreal t) const
void setP1(const QPointF &p1)
void setP2(const QPointF &p2)
void setAngle(qreal angle)
void setLength(qreal length)
void setLine(qreal x1, qreal y1, qreal x2, qreal y2)
void setPoints(const QPointF &p1, const QPointF &p2)
QLine toLine() const
void translate(const QPointF &offset)
void translate(qreal dx, qreal dy)
QLineF translated(const QPointF &offset) const
QLineF translated(qreal dx, qreal dy) const
QLineF unitVector() const

静态公共成员

QLineF fromPolar(qreal length, qreal angle)
(since 6.8) bool qFuzzyCompare(const QLineF &lhs, const QLineF &rhs)
(since 6.8) bool qFuzzyIsNull(const QLineF &line)
bool operator!=(const QLineF &lhs, const QLineF &rhs)
QDataStream &operator<<(QDataStream &stream, const QLineF &line)
bool operator==(const QLineF &lhs, const QLineF &rhs)
QDataStream &operator>>(QDataStream &stream, QLineF &line)

详细说明

QLineF 描述了二维平面上的一条有限长度的直线(或线段)。QLineF 使用浮点精度定义直线的起始点和终点坐标。使用toLine() 函数可获取该直线的整数型副本。

标有 p1、p2、dx、dy 的直线标有 x1、y1、x2、y2、dx、dy 的直线

可以通过p1()、x1()、y1()、p2()、x2()和y2()函数获取线的起始点和终点位置。dx()和dy()函数分别返回线的水平分量和垂直分量。

可通过length() 函数获取直线的长度,并通过setLength() 函数对其进行修改。同样,angle() 和setAngle() 函数分别用于获取和修改直线的角度。使用isNull() 函数可判断 QLineF 表示的是有效直线还是空直线。

intersects() 函数用于确定该直线与给定直线之间的IntersectionType ,而angleTo() 函数则返回两条直线之间的夹角。 此外,unitVector() 函数返回一条与该线段起点相同、但长度仅为 1 的线段,而normalVector() 函数返回一条与该线段垂直、且起点和长度均相同的线段。

最后,可以使用translate()函数将直线平移指定距离,并使用pointAt()函数遍历该直线。

约束条件

QLine 受限于int 类型的最小值和最大值。对QLine 进行的任何操作,若可能导致值超出此范围,将导致未定义行为。

另请参阅 QLine 、QPolygonF 和QRectF 。

成员类型文档

enum QLineF::IntersectionType

描述了两条直线之间的交点。

两条带延长线的直线相交两条直线在边界内相交
QLineF::UnboundedIntersectionQLineF::BoundedIntersection
常量值描述
QLineF::NoIntersection0表示两条直线不相交;即它们是平行的。
QLineF::UnboundedIntersection2两条直线相交,但交点不在由其长度定义的范围内。如果两条直线不平行,就会出现这种情况。如果交点仅位于其中一条直线的起始点和终点之间,intersect() 也会返回此值。
QLineF::BoundedIntersection1两条直线在各自的起始点和终点之间相交。

另请参阅 intersects()。

成员函数文档

[constexpr] QLineF::QLineF()

构造一条虚线。

[constexpr] QLineF::QLineF(const QLine &line)

根据给定的基于整数的line 构建一个QLineF对象。

另请参阅 toLine() 和QLine::toLineF()。

[constexpr] QLineF::QLineF(const QPointF &p1, const QPointF &p2)

创建一个线段对象,表示p1 与p2 之间的线段。

[constexpr] QLineF::QLineF(qreal x1, qreal y1, qreal x2, qreal y2)

构建一个线段对象,该对象表示连接 (x1,y1) 与 (x2,y2) 之间的线段。

[constexpr] QPointF QLineF::p1() const

返回该行的起始点。

另请参阅 setP1()、x1()、y1() 以及p2()。

[constexpr] QPointF QLineF::p2() const

返回该线的终点。

另请参阅 setP2()、x2()、y2() 和p1()。

[constexpr] qreal QLineF::x1() const

返回直线的起点的 x 坐标。

另请参阅 p1()。

[constexpr] qreal QLineF::x2() const

返回直线的端点的x坐标。

另请参阅 p2()。

[constexpr] qreal QLineF::y1() const

返回直线的起点的 y 坐标。

另请参阅 p1()。

[constexpr] qreal QLineF::y2() const

返回直线的端点的 y 坐标。

另请参阅 p2()。

qreal QLineF::angle() const

返回直线的角度(单位为度)。

返回值将在 0.0 到 360.0(不包括 360.0)之间。角度以逆时针方向测量,起点为原点右侧的 x 轴上某一点(x > 0)。

另请参阅 setAngle()。

qreal QLineF::angleTo(const QLineF &line) const

返回从该直线到给定直线line 的夹角(单位为度),并考虑两条直线的方向。如果两条直线在其范围内未intersect ,则以延长线的交点作为原点(参见QLineF::UnboundedIntersection )。

返回值表示需要向该直线添加多少度,使其与给定的line 形成相同的角度(逆时针方向)。

另请参阅 intersects()。

[constexpr] QPointF QLineF::center() const

返回该直线的中心点。这等同于 0.5 *p1() + 0.5 *p2()。

[constexpr] qreal QLineF::dx() const

返回直线的向量中的水平分量。

另请参阅 dy() 和pointAt()。

[constexpr] qreal QLineF::dy() const

返回直线的向量的垂直分量。

另请参阅 dx() 和pointAt()。

[static] QLineF QLineF::fromPolar(qreal length, qreal angle)

返回一个QLineF ,其中包含给定的length 和angle 。

该直线的起始点位于原点。

角度的正值表示逆时针方向,负值表示顺时针方向。零度位于3点钟位置。

QLineF::IntersectionType QLineF::intersects(const QLineF &line, QPointF *intersectionPoint = nullptr) const

返回一个值,表示该直线是否与给定的line 相交。

实际的交点将被提取到intersectionPoint 中(如果该指针有效)。如果两条直线平行,则交点未定义。

[constexpr] bool QLineF::isNull() const

如果该线段没有不同的起始点和终点,则返回true ;否则返回false 。如果qFuzzyCompare() 能在至少一个坐标上区分起始点和终点,则认为它们是不同的。

注意:由于 使用了模糊比较,对于其length() 结果不为零的线段,isNull() 可能会返回true 。

另请参阅 qFuzzyCompare() 和length()。

qreal QLineF::length() const

返回该行的长度。

另请参阅 setLength() 和isNull()。

[constexpr] QLineF QLineF::normalVector() const

返回一条与该直线垂直、且起始点和长度均与该直线相同的直线。

图示了两个起点相同的向量:(v_x, v_y) 和 (v_y, -v_x)。

另请参阅 unitVector()。

[constexpr] QPointF QLineF::pointAt(qreal t) const

返回由有限参数t 指定的位置处的点。当t = 0时,该函数返回线的起点;当t = 1时,返回线的终点。

另请参阅 dx() 和dy()。

void QLineF::setP1(const QPointF &p1)

将该线的起始点设置为p1 。

另请参阅 setP2() 和p1()。

void QLineF::setP2(const QPointF &p2)

将该线的终点设置为p2 。

另请参阅 setP1() 和p2()。

void QLineF::setAngle(qreal angle)

将直线的角度设置为给定的angle (单位为度)。这将改变直线的第二个点的位置,使直线具有给定的角度。

角度的正值表示逆时针方向,负值表示顺时针方向。零度对应3点钟位置。

另请参阅 angle()。

void QLineF::setLength(qreal length)

将线的长度设置为给定的有限值length 。QLineF 将移动线的端点 -p2() - 以使线达到新的长度,除非length() 之前为零,在这种情况下不会进行缩放。

另请参阅 length() 和unitVector()。

void QLineF::setLine(qreal x1, qreal y1, qreal x2, qreal y2)

将该行设为x1 、y1 中的起始行,以及x2 、y2 中的结束行。

另请参阅 setP1()、setP2()、p1() 和p2()。

void QLineF::setPoints(const QPointF &p1, const QPointF &p2)

将该行的起始点设置为p1 ,终点设置为p2 。

另请参阅 setP1()、setP2()、p1() 以及p2()。

[constexpr] QLine QLineF::toLine() const

返回该行基于整数的副本。

请注意,返回的行起始点和结束点会四舍五入到最接近的整数。

另请参阅 QLineF() 和QLine::toLineF()。

[constexpr] void QLineF::translate(const QPointF &offset)

使用给定的offset 对这一行进行翻译。

[constexpr] void QLineF::translate(qreal dx, qreal dy)

将该行按dx 和dy 中指定的距离进行平移。

这是一个重载函数。

[constexpr] QLineF QLineF::translated(const QPointF &offset) const

返回由给定的offset 翻译后的该行内容。

[constexpr] QLineF QLineF::translated(qreal dx, qreal dy) const

返回将该行按dx 和dy 中指定的距离进行平移后的结果。

这是一个重载函数。

QLineF QLineF::unitVector() const

返回该直线的单位向量,即一条与该直线起点相同、长度为 1.0 的直线(前提是该直线不为空)。

另请参阅 normalVector() 和setLength()。

相关非成员

[constexpr noexcept, since 6.8] bool qFuzzyCompare(const QLineF &lhs, const QLineF &rhs)

如果行lhs 与行rhs 近似相等,则返回true ;否则返回false 。

如果两条直线的起始点和终点近似相等,则认为它们近似相等。

该函数在 Qt 6.8 中引入。

[constexpr noexcept, since 6.8] bool qFuzzyIsNull(const QLineF &line)

如果直线line 的起点近似等于其终点,则返回true ;否则返回false 。

该函数在 Qt 6.8 中引入。

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

如果行lhs 与行rhs 不相同,则返回true 。

若两条线的起始点或终点不同,或者点的位置顺序不同,则认为这两条线不同。

QDataStream &operator<<(QDataStream &stream, const QLineF &line)

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

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

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

如果行lhs 与行rhs 相同,则返回true 。

若两条线的起始点和终点相同,且点序列的内部顺序一致,则这两条线被视为相同。

QDataStream &operator>>(QDataStream &stream, QLineF &line)

从给定的stream 中读取一行内容到给定的line 中,并返回该流的引用。

另请参阅 《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.