QLineF Class

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

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

该类可等价比较

该类可与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(),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)

构造一个表示p1p2 之间直线的直线对象。

[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。角度是从 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)

返回带有给定lengthangleQLineF

直线的第一个点将位于原点上。

角度的正值表示逆时针方向,负值表示顺时针方向。零度位于 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

返回一条与此直线垂直且起点和长度相同的直线。

另请参见 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)

将直线的长度设置为给定的有限lengthQLineF 将移动直线的端点 -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()。

void QLineF::translate(const QPointF &offset)

用给定的offset 翻译这一行。

void QLineF::translate(qreal dx, qreal dy)

这是一个重载函数。

按照dxdy 指定的距离平移此行。

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

Returns this line translated by the givenoffset.

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

这是一个重载函数。

dxdy 指定的距离返回这条直线的平移结果。

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 数据类型

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