IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)

QPoint Class

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

All functions in this class are reentrant.

Article lu   fois.

L'auteur

Liens sociaux

Viadeo Twitter Facebook Share on Google+   

QPoint Class

  • Header: QPoint

  • CMake:

    find_package(Qt6 REQUIRED COMPONENTS Core)

    target_link_libraries(mytarget PRIVATE Qt6::Core)

  • qmake: QT += core

  • Group: QPoint is part of painting

Detailed Description

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:

 
Sélectionnez
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 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

See also QPointF, QPolygon

Member Function Documentation

 

[constexpr] QPoint::QPoint()

Constructs a null point, i.e. with coordinates (0, 0)

See Also

See also isNull()

[constexpr] QPoint::QPoint(int xpos, int ypos)

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

See Also

See also setX(), setY()

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

 
Sélectionnez
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.

This function was introduced in Qt 5.1.

[constexpr] bool QPoint::isNull() const

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

[constexpr] 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. For example:

 
Sélectionnez
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:

 
Sélectionnez
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.

[constexpr] int &QPoint::rx()

Returns a reference to the x coordinate of this point.

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

 
Sélectionnez
QPoint p(1, 2);
p.rx()--;   // p becomes (0, 2)
See Also

See also x(), setX()

[constexpr] int &QPoint::ry()

Returns a reference to the y coordinate of this point.

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

 
Sélectionnez
QPoint p(1, 2);
p.ry()++;   // p becomes (1, 3)
See Also

See also y(), setY()

[constexpr] void QPoint::setX(int x)

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

See Also

See also x(), setY()

[constexpr] void QPoint::setY(int y)

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

See Also

See also y(), setX()

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

Creates a CGPoint from a QPoint.

This function was introduced in Qt 5.8.

See Also

[constexpr, since 6.4] QPointF QPoint::toPointF() const

Returns this point as a point with floating point accuracy.

This function was introduced in Qt 6.4.

See Also

See also QPointF::toPoint()

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

Returns a point with x and y coordinates exchanged:

 
Sélectionnez
QPoint{1, 2}.transposed() // {2, 1}

This function was introduced in Qt 5.14.

See Also

See also x(), y(), setX(), setY()

[constexpr] int QPoint::x() const

Returns the x coordinate of this point.

See Also

See also setX(), rx()

[constexpr] int QPoint::y() const

Returns the y coordinate of this point.

See Also

See also setY(), ry()

[constexpr] QPoint &QPoint::operator*=(float factor)

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

See also operator/=()

[constexpr] QPoint &QPoint::operator*=(double factor)

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

 
Sélectionnez
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

See also operator/=()

[constexpr] QPoint &QPoint::operator*=(int factor)

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

See Also

See also operator/=()

[constexpr] QPoint &QPoint::operator+=(const QPoint &point)

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

 
Sélectionnez
QPoint p( 3, 7);
QPoint q(-1, 4);
p += q;    // p becomes (2, 11)
See Also

See also operator-=()

[constexpr] QPoint &QPoint::operator-=(const QPoint &point)

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

 
Sélectionnez
QPoint p( 3, 7);
QPoint q(-1, 4);
p -= q;    // p becomes (4, 3)
See Also

See also operator+=()

[constexpr] QPoint &QPoint::operator/=(qreal divisor)

This is an overloaded function.

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

 
Sélectionnez
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

See also operator*=()

Related Non-Members

 

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

Returns true if p1 and p2 are not equal; otherwise returns false.

[constexpr] QPoint operator*(const QPoint &point, float factor)

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

See also QPoint::operator*=()

[constexpr] QPoint operator*(const QPoint &point, double factor)

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

See also QPoint::operator*=()

[constexpr] QPoint operator*(const QPoint &point, int factor)

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

See Also

See also QPoint::operator*=()

[constexpr] QPoint operator*(float factor, const QPoint &point)

This is an overloaded function.

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

See also QPoint::operator*=()

[constexpr] QPoint operator*(double factor, const QPoint &point)

This is an overloaded function.

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

See also QPoint::operator*=()

[constexpr] QPoint operator*(int factor, const QPoint &point)

This is an overloaded function.

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

See Also

See also QPoint::operator*=()

[constexpr] QPoint operator+(const QPoint &p1, const QPoint &p2)

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

See Also

See also QPoint::operator+=()

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

Returns point unmodified.

This function was introduced in Qt 5.0.

[constexpr] QPoint operator-(const QPoint &p1, const QPoint &p2)

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

See Also

See also QPoint::operator-=()

[constexpr] QPoint operator-(const QPoint &point)

This is an overloaded function.

Returns a QPoint object that is formed by changing the sign of both components of the given point.

Equivalent to QPoint(0,0) - point.

[constexpr] QPoint operator/(const QPoint &point, qreal divisor)

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

See also QPoint::operator/=()

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

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

See Also

[constexpr] bool operator==(const QPoint &p1, const QPoint &p2)

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

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

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

See Also

Vous avez aimé ce tutoriel ? Alors partagez-le en cliquant sur les boutons suivants : Viadeo Twitter Facebook Share on Google+