Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
QPoint Class Reference

\inmodule QtCore\reentrant More...

#include <qpoint.h>

+ Inheritance diagram for QPoint:
+ Collaboration diagram for QPoint:

Public Member Functions

constexpr QPoint () noexcept
 Constructs a null point, i.e.
 
constexpr QPoint (int xpos, int ypos) noexcept
 Constructs a point with the given coordinates (xpos, ypos).
 
constexpr bool isNull () const noexcept
 Returns true if both the x and y coordinates are set to 0, otherwise returns false.
 
constexpr int x () const noexcept
 Returns the x coordinate of this point.
 
constexpr int y () const noexcept
 Returns the y coordinate of this point.
 
constexpr void setX (int x) noexcept
 Sets the x coordinate of this point to the given x coordinate.
 
constexpr void setY (int y) noexcept
 Sets the y coordinate of this point to the given y coordinate.
 
constexpr int 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.
 
constexpr QPoint transposed () const noexcept
 
constexpr int & rx () noexcept
 Returns a reference to the x coordinate of this point.
 
constexpr int & ry () noexcept
 Returns a reference to the y coordinate of this point.
 
constexpr QPointoperator+= (const QPoint &p)
 Adds the given point to this point and returns a reference to this point.
 
constexpr QPointoperator-= (const QPoint &p)
 Subtracts the given point from this point and returns a reference to this point.
 
constexpr QPointoperator*= (float factor)
 Multiplies this point's coordinates by the given factor, and returns a reference to this point.
 
constexpr QPointoperator*= (double factor)
 Multiplies this point's coordinates by the given factor, and returns a reference to this point.
 
constexpr QPointoperator*= (int factor)
 Multiplies this point's coordinates by the given factor, and returns a reference to this point.
 
constexpr QPointoperator/= (qreal divisor)
 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.Divides both x and y by the given divisor, and returns a reference to this point.
 
constexpr QPointF toPointF () const noexcept
 

Static Public Member Functions

static constexpr int dotProduct (const QPoint &p1, const QPoint &p2)
 

Friends

class QRect
 
constexpr bool comparesEqual (const QPoint &p1, const QPoint &p2) noexcept
 
constexpr QPoint operator+ (const QPoint &p1, const QPoint &p2) noexcept
 Returns a QPoint object that is the sum of the given points, p1 and p2; each component is added separately.
 
constexpr QPoint operator- (const QPoint &p1, const QPoint &p2) noexcept
 Returns a QPoint object that is formed by subtracting p2 from p1; each component is subtracted separately.
 
constexpr QPoint operator* (const QPoint &p, float factor)
 Returns a copy of the given point multiplied by the given factor.
 
constexpr QPoint operator* (const QPoint &p, double factor)
 Returns a copy of the given point multiplied by the given factor.
 
constexpr QPoint operator* (const QPoint &p, int factor) noexcept
 Returns a copy of the given point multiplied by the given factor.
 
constexpr QPoint operator* (float factor, const QPoint &p)
 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.Returns a copy of the given point multiplied by the given factor.
 
constexpr QPoint operator* (double factor, const QPoint &p)
 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.Returns a copy of the given point multiplied by the given factor.
 
constexpr QPoint operator* (int factor, const QPoint &p) noexcept
 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.Returns a copy of the given point multiplied by the given factor.
 
constexpr QPoint operator+ (const QPoint &p) noexcept
 
constexpr QPoint operator- (const QPoint &p) noexcept
 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.Returns a QPoint object that is formed by changing the sign of both components of the given point.
 
constexpr QPoint operator/ (const QPoint &p, qreal c)
 Returns the QPoint formed by dividing both components of the given point by the given divisor.
 
template<std::size_t I, typename P, std::enable_if_t<(I< 2), bool > = true, std::enable_if_t< std::is_same_v< q20::remove_cvref_t< P >, QPoint >, bool > = true>
constexpr decltype(auto) get (P &&p) noexcept
 

Related Symbols

(Note that these are not member symbols.)

QDataStreamoperator<< (QDataStream &stream, const QPoint &point)
 Writes the given point to the given stream and returns a reference to the stream.
 
QDataStreamoperator>> (QDataStream &stream, QPoint &point)
 Reads a point from the given stream into the given point and returns a reference to the stream.
 

Detailed Description

\inmodule QtCore

\reentrant

\compares equality \compareswith equality QPointF \endcompareswith

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

Definition at line 27 of file qpoint.h.

Constructor & Destructor Documentation

◆ QPoint() [1/2]

QPoint::QPoint ( )
inlineconstexprnoexcept

Constructs a null point, i.e.

with coordinates (0, 0)

See also
isNull()

Definition at line 132 of file qpoint.h.

Referenced by QSwipeGestureRecognizer::reset().

+ Here is the caller graph for this function:

◆ QPoint() [2/2]

QPoint::QPoint ( int xpos,
int ypos )
inlineconstexprnoexcept

Constructs a point with the given coordinates (xpos, ypos).

See also
setX(), setY()

Definition at line 134 of file qpoint.h.

Member Function Documentation

◆ dotProduct()

static constexpr int QPoint::dotProduct ( const QPoint & p1,
const QPoint & p2 )
inlinestaticconstexpr
Since
5.1
QPoint p( 3, 7);
QPoint q(-1, 4);
int dotProduct = QPoint::dotProduct(p, q); // dotProduct becomes 25

Returns the dot product of p1 and p2.

Definition at line 56 of file qpoint.h.

◆ isNull()

bool QPoint::isNull ( ) const
inlineconstexprnoexcept

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

Definition at line 136 of file qpoint.h.

◆ manhattanLength()

int constexpr QPoint::manhattanLength ( ) const
inlineconstexpr

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.

For example:

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

Definition at line 161 of file qpoint.h.

◆ operator*=() [1/3]

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

Multiplies this point's coordinates by the given 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 for floating point accuracy.

See also
operator/=()

Definition at line 197 of file qpoint.h.

◆ operator*=() [2/3]

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

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

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

See also
operator/=()

Definition at line 190 of file qpoint.h.

◆ operator*=() [3/3]

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

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

See also
operator/=()

Definition at line 204 of file qpoint.h.

◆ operator+=()

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

Adds the given 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)
See also
operator-=()

Definition at line 176 of file qpoint.h.

◆ operator-=()

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

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)
See also
operator+=()

Definition at line 183 of file qpoint.h.

◆ operator/=()

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

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.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 for floating point accuracy.

See also
operator*=()

Definition at line 211 of file qpoint.h.

◆ rx()

int & QPoint::rx ( )
inlineconstexprnoexcept

Returns a reference to the x coordinate of this point.

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

QPoint p(1, 2);
p.rx()--; // p becomes (0, 2)
See also
x(), setX()

Definition at line 166 of file qpoint.h.

◆ ry()

int & QPoint::ry ( )
inlineconstexprnoexcept

Returns a reference to the y coordinate of this point.

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

QPoint p(1, 2);
p.ry()++; // p becomes (1, 3)
See also
y(), setY()

Definition at line 171 of file qpoint.h.

◆ setX()

void QPoint::setX ( int x)
inlineconstexprnoexcept

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

See also
x(), setY()

Definition at line 151 of file qpoint.h.

◆ setY()

void QPoint::setY ( int y)
inlineconstexprnoexcept

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

See also
y(), setX()

Definition at line 156 of file qpoint.h.

◆ toPointF()

QPointF QPoint::toPointF ( ) const
inlinenodiscardconstexprnoexcept
Since
6.4

Returns this point as a point with floating point accuracy.

See also
QPointF::toPoint()

Definition at line 414 of file qpoint.h.

◆ transposed()

QPoint QPoint::transposed ( ) const
inlineconstexprnoexcept
Since
5.14

Returns a point with x and y coordinates exchanged:

QPoint{1, 2}.transposed() // {2, 1}
constexpr QPoint() noexcept
Constructs a null point, i.e.
Definition qpoint.h:132
See also
x(), y(), setX(), setY()

Definition at line 42 of file qpoint.h.

◆ x()

int QPoint::x ( ) const
inlineconstexprnoexcept

Returns the x coordinate of this point.

See also
setX(), rx()

Definition at line 141 of file qpoint.h.

◆ y()

int QPoint::y ( ) const
inlineconstexprnoexcept

Returns the y coordinate of this point.

See also
setY(), ry()

Definition at line 146 of file qpoint.h.

Friends And Related Symbol Documentation

◆ comparesEqual

bool comparesEqual ( const QPoint & p1,
const QPoint & p2 )
friend

Definition at line 60 of file qpoint.h.

◆ get

template<std::size_t I, typename P, std::enable_if_t<(I< 2), bool > = true, std::enable_if_t< std::is_same_v< q20::remove_cvref_t< P >, QPoint >, bool > = true>
decltype(auto) get ( P && p)
friend

Definition at line 109 of file qpoint.h.

◆ operator* [1/6]

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

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

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

See also
QPoint::operator*=()

Definition at line 69 of file qpoint.h.

◆ operator* [2/6]

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

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

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

See also
QPoint::operator*=()

Definition at line 67 of file qpoint.h.

◆ operator* [3/6]

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

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

See also
QPoint::operator*=()

Definition at line 71 of file qpoint.h.

◆ operator* [4/6]

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

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.Returns a copy of the given point multiplied by the given factor.

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

See also
QPoint::operator*=()

Definition at line 75 of file qpoint.h.

◆ operator* [5/6]

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

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.Returns a copy of the given point multiplied by the given factor.

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

See also
QPoint::operator*=()

Definition at line 73 of file qpoint.h.

◆ operator* [6/6]

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

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.Returns a copy of the given point multiplied by the given factor.

See also
QPoint::operator*=()

Definition at line 77 of file qpoint.h.

◆ operator+ [1/2]

QPoint operator+ ( const QPoint & point)
friend
Since
5.0

Returns point unmodified.

Definition at line 79 of file qpoint.h.

◆ operator+ [2/2]

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

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

See also
QPoint::operator+=()

Definition at line 63 of file qpoint.h.

◆ operator- [1/2]

QPoint operator- ( const QPoint & point)
friend

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.Returns a QPoint object that is formed by changing the sign of both components of the given point.

Equivalent to {QPoint(0,0) - point}.

Definition at line 81 of file qpoint.h.

◆ operator- [2/2]

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

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

See also
QPoint::operator-=()

Definition at line 65 of file qpoint.h.

◆ operator/

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

Returns the 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 for floating point accuracy.

See also
QPoint::operator/=()

Definition at line 83 of file qpoint.h.

◆ operator<<()

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

Writes the given point to the given stream and returns a reference to the stream.

See also
{Serializing Qt Data Types}

◆ operator>>()

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

Reads a point from the given stream into the given point and returns a reference to the stream.

See also
{Serializing Qt Data Types}

◆ QRect

friend class QRect
friend

Definition at line 98 of file qpoint.h.


The documentation for this class was generated from the following files: