QPoint 类

QPoint 类使用整数精度,定义平面点。 更多...

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

注意: 此类的所有函数 可重入 .

公共函数

QPoint (int xpos , int ypos )
QPoint ()
bool isNull () const
int manhattanLength () const
int & rx ()
int & ry ()
void setX (int x )
void setY (int y )
CGPoint toCGPoint () const
QPoint transposed () const
int x () const
int y () const
QPoint & operator*= (float factor )
QPoint & operator*= (double 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 & p1 , const QPoint & p2 )
QPoint operator* (const QPoint & point , float factor )
QPoint operator* (const QPoint & point , double factor )
QPoint operator* (const QPoint & point , int factor )
QPoint operator* (float factor , const QPoint & point )
QPoint operator* (double factor , const QPoint & point )
QPoint operator* (int factor , const QPoint & point )
QPoint operator+ (const QPoint & p1 , const QPoint & p2 )
QPoint operator+ (const QPoint & point )
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 & p1 , const QPoint & p2 )
QDataStream & operator>> (QDataStream & stream , QPoint & point )

详细描述

A point is specified by a x coordinate and an y coordinate which can be accessed using the x () 和 y () 函数。 isNull () 函数返回 true if both x and y are set to 0. The coordinates can be set (or altered) using the setX () 和 setY () functions, or alternatively the rx () 和 ry () functions which return references to the coordinates (allowing direct manipulation).

Given a point p , the following statements are all equivalent:

QPoint p;
p.setX(p.x() + 1);
p += QPoint(1, 0);
p.rx()++;
							

A QPoint object can also be used as a vector: Addition and subtraction are defined as for vectors (each component is added separately). A QPoint object can also be divided or multiplied by an int qreal .

此外,QPoint 类提供 manhattanLength () function which gives an inexpensive approximation of the length of the QPoint object interpreted as a vector. Finally, QPoint objects can be streamed as well as compared.

另请参阅 QPointF and QPolygon .

成员函数文档编制

QPoint:: QPoint ( int xpos , int ypos )

构造点采用给定坐标 ( xpos , ypos ).

另请参阅 setX () 和 setY ().

QPoint:: QPoint ()

构造 null 点 (即:采用坐标 (0, 0))

另请参阅 isNull ().

[static, since 5.1] int QPoint:: dotProduct (const QPoint & p1 , const QPoint & p2 )

QPoint p( 3, 7);
QPoint q(-1, 4);
int lengthSquared = QPoint::dotProduct(p, q);   // lengthSquared becomes 25
							

Returns the dot product of p1 and p2 .

该函数在 Qt 5.1 引入。

bool QPoint:: isNull () const

返回 true 若 x 和 y 坐标两者被设为 0,否则返回 false .

int QPoint:: manhattanLength () const

Returns the sum of the absolute values of x () 和 y (), traditionally known as the "Manhattan length" of the vector from the origin to the point. For example:

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
}
							

This is a useful, and quick to calculate, approximation to the true length:

double trueLength = std::sqrt(std::pow(x(), 2) + std::pow(y(), 2));
							

The tradition of "Manhattan length" arises because such distances apply to travelers who can only travel on a rectangular grid, like the streets of Manhattan.

int &QPoint:: rx ()

返回此点的 x 坐标引用。

Using a reference makes it possible to directly manipulate x. For example:

QPoint p(1, 2);
p.rx()--;   // p becomes (0, 2)
							

另请参阅 x () 和 setX ().

int &QPoint:: ry ()

返回此点的 y 坐标引用。

Using a reference makes it possible to directly manipulate y. For example:

QPoint p(1, 2);
p.ry()++;   // p becomes (1, 3)
							

另请参阅 y () 和 setY ().

void QPoint:: setX ( int x )

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

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

void QPoint:: setY ( int y )

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

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

[since 5.8] CGPoint QPoint:: toCGPoint () const

创建 CGPoint 从 QPoint .

该函数在 Qt 5.8 引入。

另请参阅 QPointF::fromCGPoint ().

[since 5.14] QPoint QPoint:: transposed () const

返回具有 x 和 y 交换坐标的点:

QPoint{1, 2}.transposed() // {2, 1}
							

该函数在 Qt 5.14 引入。

另请参阅 x (), y (), setX (),和 setY ().

int QPoint:: x () const

返回此点的 x 坐标。

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

int QPoint:: y () const

返回此点的 y 坐标。

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

QPoint &QPoint:: operator*= ( float factor )

将此点的坐标乘以给定 factor ,并返回此点的引用。

Note that the result is rounded to the nearest integer as points are held as integers. Use QPointF 对于浮点精度。

另请参阅 operator/= ().

QPoint &QPoint:: operator*= ( double factor )

将此点的坐标乘以给定 factor , and returns a reference to this point. For example:

QPoint p(-1, 4);
p *= 2.5;    // p becomes (-3, 10)
							

Note that the result is rounded to the nearest integer as points are held as integers. Use QPointF 对于浮点精度。

另请参阅 operator/= ().

QPoint &QPoint:: operator*= ( int factor )

将此点的坐标乘以给定 factor ,并返回此点的引用。

另请参阅 operator/= ().

QPoint &QPoint:: operator+= (const QPoint & point )

添加给定 point to this point and returns a reference to this point. For example:

QPoint p( 3, 7);
QPoint q(-1, 4);
p += q;    // p becomes (2, 11)
							

另请参阅 operator-= ().

QPoint &QPoint:: operator-= (const QPoint & point )

Subtracts the given point from this point and returns a reference to this point. For example:

QPoint p( 3, 7);
QPoint q(-1, 4);
p -= q;    // p becomes (4, 3)
							

另请参阅 operator+= ().

QPoint &QPoint:: operator/= ( qreal divisor )

这是重载函数。

Divides both x and y by the given divisor , and returns a reference to this point. For example:

QPoint p(-3, 10);
p /= 2.5;           // p becomes (-1, 4)
							

Note that the result is rounded to the nearest integer as points are held as integers. Use QPointF 对于浮点精度。

另请参阅 operator*= ().

相关非成员

bool operator!= (const QPoint & p1 , const QPoint & p2 )

返回 true if p1 and p2 不相等;则返回 false .

QPoint operator* (const QPoint & point , float factor )

返回副本为给定 point multiplied by the given factor .

Note that the result is rounded to the nearest integer as points are held as integers. Use QPointF 对于浮点精度。

另请参阅 QPoint::operator*= ().

QPoint operator* (const QPoint & point , double factor )

返回副本为给定 point multiplied by the given factor .

Note that the result is rounded to the nearest integer as points are held as integers. Use QPointF 对于浮点精度。

另请参阅 QPoint::operator*= ().

QPoint operator* (const QPoint & point , int factor )

返回副本为给定 point multiplied by the given factor .

另请参阅 QPoint::operator*= ().

QPoint operator* ( float factor , const QPoint & point )

这是重载函数。

返回副本为给定 point multiplied by the given factor .

Note that the result is rounded to the nearest integer as points are held as integers. Use QPointF 对于浮点精度。

另请参阅 QPoint::operator*= ().

QPoint operator* ( double factor , const QPoint & point )

这是重载函数。

返回副本为给定 point multiplied by the given factor .

Note that the result is rounded to the nearest integer as points are held as integers. Use QPointF 对于浮点精度。

另请参阅 QPoint::operator*= ().

QPoint operator* ( int factor , const QPoint & point )

这是重载函数。

返回副本为给定 point multiplied by the given factor .

另请参阅 QPoint::operator*= ().

QPoint operator+ (const QPoint & p1 , const QPoint & p2 )

返回 QPoint object that is the sum of the given points, p1 and p2 ; each component is added separately.

另请参阅 QPoint::operator+= ().

[since 5.0] QPoint operator+ (const QPoint & point )

返回 point unmodified.

该函数在 Qt 5.0 引入。

QPoint operator- (const QPoint & p1 , const QPoint & p2 )

返回 QPoint object that is formed by subtracting p2 from p1 ; each component is subtracted separately.

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

QPoint operator- (const QPoint & point )

这是重载函数。

返回 QPoint object that is formed by changing the sign of both components of the given point .

相当于 QPoint(0,0) - point .

QPoint operator/ (const QPoint & point , qreal divisor )

返回 QPoint formed by dividing both components of the given point by the given divisor .

Note that the result is rounded to the nearest integer as points are held as integers. Use QPointF 对于浮点精度。

另请参阅 QPoint::operator/= ().

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

写入给定 point 到给定 stream 并返回流引用。

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

bool operator== (const QPoint & p1 , const QPoint & p2 )

返回 true if p1 and p2 are equal; otherwise returns false.

QDataStream & operator>> ( QDataStream & stream , QPoint & point )

Reads a point from the given stream 进给定 point 并返回流引用。

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