CopperSpice API  1.9.1
QPoint Class Reference

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

Public Methods

 QPoint ()
 
 QPoint (int xPos, int yPos)
 
bool isNull () const
 
int manhattanLength () const
 
QPoint & operator*= (double factor)
 
QPoint & operator*= (float factor)
 
QPoint & operator*= (int factor)
 
QPoint & operator+= (const QPoint &point)
 
QPoint & operator-= (const QPoint &point)
 
QPoint & operator/= (qreal factor)
 
int & rx ()
 
int & ry ()
 
void setX (int xPos)
 
void setY (int yPos)
 
int x () const
 
int y () const
 

Friends

bool operator!= (const QPoint &point1, const QPoint &point2)
 
const QPoint operator* (const QPoint &point, double factor)
 
const QPoint operator* (const QPoint &point, float factor)
 
const QPoint operator* (const QPoint &point, int factor)
 
const QPoint operator* (double factor, const QPoint &point)
 
const QPoint operator* (float factor, const QPoint &point)
 
const QPoint operator* (int factor, const QPoint &point)
 
const QPoint operator+ (const QPoint &point1, const QPoint &point2)
 
const QPoint operator- (const QPoint &point)
 
const QPoint operator- (const QPoint &point1, const QPoint &point2)
 
const QPoint operator/ (const QPoint &point, qreal factor)
 
bool operator== (const QPoint &point1, const QPoint &point2)
 
class QTransform
 

Related Functions

These are not member functions

QDataStreamoperator<< (QDataStream &stream, const QPoint &point)
 
QDataStreamoperator>> (QDataStream &stream, QPoint &point)
 

Detailed Description

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

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

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

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 or a qreal.

In addition, the QPoint class provides the 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.

See also
QPointF, QPolygon

Constructor & Destructor Documentation

QPoint::QPoint ( )
inline

Constructs a null QPoint with coordinates (0, 0).

See also
isNull()
QPoint::QPoint ( int  xPos,
int  yPos 
)
inline

Constructs a point with the given coordinates xPos and yPos.

See also
setX(), setY()

Method Documentation

bool QPoint::isNull ( ) const
inline

Returns true if both the x and y coordinates are set to 0, otherwise returns false.

int QPoint::manhattanLength ( ) const

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

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 calculation to approximate the true length.

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

Multiplies this point's coordinates by the given factor, and returns a reference to this point. The result is rounded to the nearest integer as points are held as integers. Use QPointF for floating point accuracy.

QPoint p(-1, 4);
p *= 2.5; // p becomes (-3, 10)
See also
operator/=()
QPoint & QPoint::operator*= ( float  factor)
inline

Multiplies this point's coordinates by the given factor, and returns a reference to this point. The result is rounded to the nearest integer as points are held as integers. Use QPointF for floating point accuracy.

See also
operator/=()
QPoint & QPoint::operator*= ( int  factor)
inline

Multiplies this point's coordinates by the given factor, and returns a reference to this point.

See also
operator/=()
QPoint & QPoint::operator+= ( const QPoint &  point)
inline

Adds the given point to this point and returns a reference to this point.

QPoint p( 3, 7);
QPoint q(-1, 4);
p += q; // p becomes (2, 11)
See also
operator-=()
QPoint & QPoint::operator-= ( const QPoint &  point)
inline

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

QPoint p( 3, 7);
QPoint q(-1, 4);
p -= q; // p becomes (4, 3)
See also
operator+=()
QPoint & QPoint::operator/= ( qreal  factor)
inline

Divides both x and y by the given factor and returns a reference to this point.

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

The result is rounded to the nearest integer as points are held as integers. Use QPointF for floating point accuracy.

See also
operator*=()
int & QPoint::rx ( )
inline

Returns a reference to the x coordinate of this point.

Using a reference makes it possible to directly manipulate x.

QPoint p(1, 2);
p.rx()--; // p becomes (0, 2)
See also
x(), setX()
int & QPoint::ry ( )
inline

Returns a reference to the y coordinate of this point.

Using a reference makes it possible to directly manipulate y.

QPoint p(1, 2);
p.ry()++; // p becomes (1, 3)
See also
y(), setY()
void QPoint::setX ( int  xPos)
inline

Sets the x coordinate of this point to the given xPos coordinate.

See also
x(), setY()
void QPoint::setY ( int  yPos)
inline

Sets the y coordinate of this point to the given yPos coordinate.

See also
y(), setX()
int QPoint::x ( ) const
inline

Returns the x coordinate of this point.

See also
setX(), rx()
int QPoint::y ( ) const
inline

Returns the y coordinate of this point.

See also
setY(), ry()

Friends And Related Function Documentation

bool operator!= ( const QPoint &  point1,
const QPoint &  point2 
)
friend

Returns true if point1 and point2 are not equal, otherwise returns false.

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

Returns a copy of the given point multiplied by the given factor. The result is rounded to the nearest integer as points are held as integers. Use QPointF for floating point accuracy.

See also
QPoint::operator*=()
const QPoint operator* ( const QPoint &  point,
float  factor 
)
friend

Returns a copy of the given point multiplied by the given factor. The result is rounded to the nearest integer as points are held as integers. Use QPointF for floating point accuracy.

See also
QPoint::operator*=()
const QPoint operator* ( const QPoint &  point,
int  factor 
)
friend

Returns a copy of the given point multiplied by the given factor.

See also
QPoint::operator*=()
const QPoint operator* ( double  factor,
const QPoint &  point 
)
friend

Returns a copy of the given point multiplied by the given factor.

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

Returns a copy of the given point multiplied by the given factor.

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

Returns a copy of the given point multiplied by the given factor.

const QPoint operator+ ( const QPoint &  point1,
const QPoint &  point2 
)
friend

Returns a QPoint object that is the sum of the given points, point1 and point2, each component is added separately.

See also
QPoint::operator+=()
const QPoint operator- ( const QPoint &  point)
friend

Returns a QPoint object that is formed by changing the sign of both components of the given point. Equivalent to calling QPoint(0,0) - point.

const QPoint operator- ( const QPoint &  point1,
const QPoint &  point2 
)
friend

Returns a QPoint object that is formed by subtracting point2 from point1, each component is subtracted separately.

See also
QPoint::operator-=()
const QPoint operator/ ( const QPoint &  point,
qreal  factor 
)
friend

Returns the QPoint formed by dividing both components of the given point by the given factpr. The result is rounded to the nearest integer as points are held as integers. Use QPointF for floating point accuracy.

See also
QPoint::operator/=()
QDataStream & operator<< ( QDataStream stream,
const QPoint &  point 
)
related

Writes the given point to the stream. Returns a reference to the stream.

Refer to Serializing Data Types for additional information.

bool operator== ( const QPoint &  point1,
const QPoint &  point2 
)
friend

Returns true if point1 and point2 are equal, otherwise returns false.

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

Reads from the stream into the given point. Returns a reference to the stream.

Refer to Serializing Data Types for additional information.