QLineF的使用及避坑指南

QLineF 描述了二维表面上的有限长度线(或线段)。QLineF 使用坐标的浮点精度定义线的起点和终点。

QLineF的使用及避坑指南_第1张图片

坑1:如果P1设置(0,0)位置,设置角度和长度后无法构造线段

    QLineF linePositive;
    linePositive.setP1(QPointF(0, 0));
    linePositive.setAngle(0);
    linePositive.setLength(20);
    qDebug() << linePositive;

打印结果:QLineF(QPointF(0,0),QPointF(0,0))

P1设置非(0,0)才可以

    QLineF linePositive;
    linePositive.setP1(QPointF(0.01, 0));
    linePositive.setAngle(0);
    linePositive.setLength(20);
    qDebug() << linePositive;

打印结果:LineF(QPointF(0.01,0),QPointF(20.01,0))


 更多接口定义如下:

/*******************************************************************************
 * class QLineF
 *******************************************************************************/
class Q_CORE_EXPORT QLineF {
public:

    enum IntersectType { NoIntersection, BoundedIntersection, UnboundedIntersection };

    Q_DECL_CONSTEXPR inline QLineF();
    Q_DECL_CONSTEXPR inline QLineF(const QPointF &pt1, const QPointF &pt2);
    Q_DECL_CONSTEXPR inline QLineF(qreal x1, qreal y1, qreal x2, qreal y2);
    Q_DECL_CONSTEXPR inline QLineF(const QLine &line) : pt1(line.p1()), pt2(line.p2()) { }

    Q_REQUIRED_RESULT static QLineF fromPolar(qreal length, qreal angle);

    Q_DECL_CONSTEXPR bool isNull() const;

    Q_DECL_CONSTEXPR inline QPointF p1() const;
    Q_DECL_CONSTEXPR inline QPointF p2() const;

    Q_DECL_CONSTEXPR inline qreal x1() const;
    Q_DECL_CONSTEXPR inline qreal y1() const;

    Q_DECL_CONSTEXPR inline qreal x2() const;
    Q_DECL_CONSTEXPR inline qreal y2() const;

    Q_DECL_CONSTEXPR inline qreal dx() const;
    Q_DECL_CONSTEXPR inline qreal dy() const;

    qreal length() const;
    void setLength(qreal len);

    qreal angle() const;
    void setAngle(qreal angle);

    qreal angleTo(const QLineF &l) const;

    Q_REQUIRED_RESULT QLineF unitVector() const;
    Q_REQUIRED_RESULT Q_DECL_CONSTEXPR inline QLineF normalVector() const;

    // ### Qt 6: rename intersects() or intersection() and rename IntersectType IntersectionType
    IntersectType intersect(const QLineF &l, QPointF *intersectionPoint) const;

    qreal angle(const QLineF &l) const;

    Q_DECL_CONSTEXPR inline QPointF pointAt(qreal t) const;
    inline void translate(const QPointF &p);
    inline void translate(qreal dx, qreal dy);

    Q_REQUIRED_RESULT Q_DECL_CONSTEXPR inline QLineF translated(const QPointF &p) const;
    Q_REQUIRED_RESULT Q_DECL_CONSTEXPR inline QLineF translated(qreal dx, qreal dy) const;

    Q_REQUIRED_RESULT Q_DECL_CONSTEXPR inline QPointF center() const;

    inline void setP1(const QPointF &p1);
    inline void setP2(const QPointF &p2);
    inline void setPoints(const QPointF &p1, const QPointF &p2);
    inline void setLine(qreal x1, qreal y1, qreal x2, qreal y2);

    Q_DECL_CONSTEXPR inline bool operator==(const QLineF &d) const;
    Q_DECL_CONSTEXPR inline bool operator!=(const QLineF &d) const { return !(*this == d); }

    Q_DECL_CONSTEXPR QLine toLine() const;

private:
    QPointF pt1, pt2;
};
Q_DECLARE_TYPEINFO(QLineF, Q_MOVABLE_TYPE);

你可能感兴趣的:(QT学习之路,QLineF)