QPoint 类

QPoint class defines a point in the plane using integer precision. 更多...

头: #include <QPoint>
qmake: QT += core

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

公共函数

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

详细描述

QPoint class defines a point in the plane using integer precision.

点通过 X 坐标和 Y 坐标进行指定,可以访问使用 x () 和 y () 函数。 isNull () 函数返回 true 若 x 和 y 两者被设为 0。可以设置 (或变更) 坐标使用 setX () 和 setY () 函数,或另外的 rx () 和 ry () 函数返回坐标引用 (允许直接操纵)。

给定点 p ,下列语句全部等效:

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 class provides the manhattanLength () function which gives an inexpensive approximation of the length of the QPoint object interpreted as a vector. Finally, QPoint 对象可以被流化及比较。

另请参阅 QPointF and QPolygon .

成员函数文档编制

QPoint:: QPoint ()

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

另请参阅 isNull ().

QPoint:: QPoint ( int xpos , int ypos )

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

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

[static] 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
					

返回点积为 p1 and p2 .

该函数在 Qt 5.1 引入。

bool QPoint:: isNull () const

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

int QPoint:: manhattanLength () const

返回绝对值的和对于 x () 和 y (),传统称为从原点到点的向量 "曼哈顿长度"。例如:

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
}
					

这计算近似真实长度有用且快速:

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

"曼哈顿长度" 传统的兴起,是因为这种距离适用于只能按矩形栅格 (像曼哈顿街道) 旅行的旅行者。

int &QPoint:: rx ()

返回此点的 x 坐标引用。

使用引用使之可能直接操纵 X。例如:

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

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

int &QPoint:: ry ()

返回此点的 y 坐标引用。

使用引用使之可能直接操纵 Y。例如:

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 ().

CGPoint QPoint:: toCGPoint () const

创建 CGPoint 从 QPoint .

该函数在 Qt 5.8 引入。

另请参阅 QPointF::fromCGPoint ().

int QPoint:: x () const

返回此点的 x 坐标。

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

int QPoint:: y () const

返回此点的 y 坐标。

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

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

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

注意,结果被四舍五入到最近整数,由于点按整数保持。使用 QPointF 对于浮点精度。

另请参阅 operator/= ().

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

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

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

注意,结果被四舍五入到最近整数,由于点按整数保持。使用 QPointF 对于浮点精度。

另请参阅 operator/= ().

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

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

另请参阅 operator/= ().

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

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

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

另请参阅 operator-= ().

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

减去给定 point 从此点并返回此点的引用。例如:

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

另请参阅 operator+= ().

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

这是重载函数。

X 和 Y 两者除以给定 divisor ,并返回此点的引用。例如:

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

注意,结果被四舍五入到最近整数,由于点按整数保持。使用 QPointF 对于浮点精度。

另请参阅 operator*= ().

相关非成员

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

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

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

返回副本为给定 point 乘以给定 factor .

注意,结果被四舍五入到最近整数,由于点按整数保持。使用 QPointF 对于浮点精度。

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

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

返回副本为给定 point 乘以给定 factor .

注意,结果被四舍五入到最近整数,由于点按整数保持。使用 QPointF 对于浮点精度。

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

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

返回副本为给定 point 乘以给定 factor .

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

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

这是重载函数。

返回副本为给定 point 乘以给定 factor .

注意,结果被四舍五入到最近整数,由于点按整数保持。使用 QPointF 对于浮点精度。

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

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

这是重载函数。

返回副本为给定 point 乘以给定 factor .

注意,结果被四舍五入到最近整数,由于点按整数保持。使用 QPointF 对于浮点精度。

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

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

这是重载函数。

返回副本为给定 point 乘以给定 factor .

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

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

返回 QPoint 对象是和对于给定点 p1 and p2 ;分别相加各分量。

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

const QPoint operator+ (const QPoint & point )

返回 point 未经修改。

该函数在 Qt 5.0 引入。

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

返回 QPoint 对象的形成是通过减去 p2 from p1 ;分别减去各分量。

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

const QPoint operator- (const QPoint & point )

这是重载函数。

返回 QPoint 对象的形成是通过改变 2 分量的符号为给定 point .

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

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

返回 QPoint 的形成是通过除以 2 分量对于给定 point 通过给定 divisor .

注意,结果被四舍五入到最近整数,由于点按整数保持。使用 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 相等;否则返回 false。

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

读取点从给定 stream 进给定 point 并返回流引用。

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