- Qt 6.11
- Qt GUI
- C++ Classes
- QVector2D
- Qt 6.11.1 Reference Documentation

# QVector2D Class

The QVector2D class represents a vector or vertex in 2D space. More...

Header: | `#include <QVector2D>`
CMake: | `find_package(Qt6 REQUIRED COMPONENTS Gui)` `target_link_libraries(mytarget PRIVATE Qt6::Gui)`
qmake: | `QT += gui`

- List of all members, including inherited members
- QVector2D is part of Painting Classes and Rendering in 3D.

## Public Functions

| QVector2D()
| QVector2D(QPoint point)
| QVector2D(QPointF point)
| QVector2D(QVector3D vector)
| QVector2D(QVector4D vector)
| QVector2D(float xpos, float ypos)
float | distanceToLine(QVector2D point, QVector2D direction) const
float | distanceToPoint(QVector2D point) const
bool | isNull() const
float | length() const
float | lengthSquared() const
void | normalize()
QVector2D | normalized() const
void | setX(float x)
void | setY(float y)
QPoint | toPoint() const
QPointF | toPointF() const
QVector3D | toVector3D() const
QVector4D | toVector4D() const
float | x() const
float | y() const
| operator QVariant() const
QVector2D & | operator*=(QVector2D vector)
QVector2D & | operator*=(float factor)
QVector2D & | operator+=(QVector2D vector)
QVector2D & | operator-=(QVector2D vector)
QVector2D & | operator/=(QVector2D vector)
QVector2D & | operator/=(float divisor)
float & | operator[](int i)
float | operator[](int i) const

## Static Public Members

float | dotProduct(QVector2D v1, QVector2D v2)

## Related Non-Members

bool | qFuzzyCompare(QVector2D v1, QVector2D v2)
bool | operator!=(QVector2D v1, QVector2D v2)
QVector2D | operator*(QVector2D v1, QVector2D v2)
QVector2D | operator*(QVector2D vector, float factor)
QVector2D | operator*(float factor, QVector2D vector)
QVector2D | operator+(QVector2D v1, QVector2D v2)
QVector2D | operator-(QVector2D v1, QVector2D v2)
QVector2D | operator-(QVector2D vector)
QVector2D | operator/(QVector2D vector, QVector2D divisor)
QVector2D | operator/(QVector2D vector, float divisor)
QDataStream & | operator<<(QDataStream &stream, QVector2D vector)
bool | operator==(QVector2D v1, QVector2D v2)
QDataStream & | operator>>(QDataStream &stream, QVector2D &vector)

## Detailed Description

Vectors are one of the main building blocks of 2D representation and drawing. They consist of two finite floating-point coordinates, traditionally called x and y.

The QVector2D class can also be used to represent vertices in 2D space. We therefore do not need to provide a separate vertex class.

See also QVector3D, QVector4D, and QQuaternion.

## Member Function Documentation

### `[constexpr noexcept]` QVector2D::QVector2D()

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

### `[explicit constexpr noexcept]` QVector2D::QVector2D(QPoint point)

Constructs a vector with x and y coordinates from a 2D point.

### `[explicit constexpr noexcept]` QVector2D::QVector2D(QPointF point)

Constructs a vector with x and y coordinates from a 2D point.

### `[explicit constexpr noexcept]` QVector2D::QVector2D(QVector3D vector)

Constructs a vector with x and y coordinates from a 3D vector. The z coordinate of vector is dropped.

See also toVector3D().

### `[explicit constexpr noexcept]` QVector2D::QVector2D(QVector4D vector)

Constructs a vector with x and y coordinates from a 3D vector. The z and w coordinates of vector are dropped.

See also toVector4D().

### `[constexpr noexcept]` QVector2D::QVector2D(float xpos, float ypos)

Constructs a vector with coordinates (xpos, ypos). Both coordinates must be finite.

### `[noexcept]` float QVector2D::distanceToLine(QVector2D point, QVector2D direction) const

Returns the distance that this vertex is from a line defined by point and the unit vector direction.

If direction is a null vector, then it does not define a line. In that case, the distance from point to this vertex is returned.

See also distanceToPoint().

### `[noexcept]` float QVector2D::distanceToPoint(QVector2D point) const

Returns the distance from this vertex to a point defined by the vertex point.

See also distanceToLine().

### `[static constexpr noexcept]` float QVector2D::dotProduct(QVector2D v1, QVector2D v2)

Returns the dot product of v1 and v2.

### `[constexpr noexcept]` bool QVector2D::isNull() const

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

### `[noexcept]` float QVector2D::length() const

Returns the length of the vector from the origin.

See also lengthSquared() and normalized().

### `[constexpr noexcept]` float QVector2D::lengthSquared() const

Returns the squared length of the vector from the origin. This is equivalent to the dot product of the vector with itself.

See also length() and dotProduct().

### `[noexcept]` void QVector2D::normalize()

Normalizes the current vector in place. Nothing happens if this vector is a null vector or the length of the vector is very close to 1.

See also length() and normalized().

### `[noexcept]` QVector2D QVector2D::normalized() const

Returns the normalized unit vector form of this vector.

If this vector is null, then a null vector is returned. If the length of the vector is very close to 1, then the vector will be returned as-is. Otherwise the normalized form of the vector of length 1 will be returned.

See also length() and normalize().

### `[constexpr noexcept]` void QVector2D::setX(float x)

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

See also x() and setY().

### `[constexpr noexcept]` void QVector2D::setY(float y)

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

See also y() and setX().

### `[constexpr noexcept]` QPoint QVector2D::toPoint() const

Returns the QPoint form of this 2D vector. Each coordinate is rounded to the nearest integer.

See also toPointF() and toVector3D().

### `[constexpr noexcept]` QPointF QVector2D::toPointF() const

Returns the QPointF form of this 2D vector.

See also toPoint() and toVector3D().

### `[constexpr noexcept]` QVector3D QVector2D::toVector3D() const

Returns the 3D form of this 2D vector, with the z coordinate set to zero.

See also toVector4D() and toPoint().

### `[constexpr noexcept]` QVector4D QVector2D::toVector4D() const

Returns the 4D form of this 2D vector, with the z and w coordinates set to zero.

See also toVector3D() and toPoint().

### `[constexpr noexcept]` float QVector2D::x() const

Returns the x coordinate of this point.

See also setX() and y().

### `[constexpr noexcept]` float QVector2D::y() const

Returns the y coordinate of this point.

See also setY() and x().

### QVector2D::operator QVariant() const

Returns the 2D vector as a QVariant.

### `[constexpr noexcept]` QVector2D &QVector2D::operator*=(QVector2D vector)

Multiplies each component of this vector by the corresponding component of vector and returns a reference to this vector.

Note: This is not a cross product of this vector with vector. (Its components add up to the dot product of this vector and vector.)

See also operator/=() and operator*().

### `[constexpr noexcept]` QVector2D &QVector2D::operator*=(float factor)

Multiplies this vector's coordinates by the given finite factor and returns a reference to this vector.

See also operator/=() and operator*().

### `[constexpr noexcept]` QVector2D &QVector2D::operator+=(QVector2D vector)

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

See also operator-=().

### `[constexpr noexcept]` QVector2D &QVector2D::operator-=(QVector2D vector)

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

See also operator+=().

### `[constexpr]` QVector2D &QVector2D::operator/=(QVector2D vector)

Divides each component of this vector by the corresponding component of vector and returns a reference to this vector.

The vector must have no component that is either zero or NaN.

See also operator*=() and operator/().

### `[constexpr]` QVector2D &QVector2D::operator/=(float divisor)

Divides this vector's coordinates by the given divisor and returns a reference to this vector. The divisor must not be either zero or NaN.

See also operator*=().

### `[constexpr]` float &QVector2D::operator[](int i)

Returns the component of the vector at index position i as a modifiable reference.

i must be a valid index position in the vector (i.e., 0 <= i < 2).

### `[constexpr]` float QVector2D::operator[](int i) const

Returns the component of the vector at index position i.

i must be a valid index position in the vector (i.e., 0 <= i < 2).

## Related Non-Members

### `[noexcept]` bool qFuzzyCompare(QVector2D v1, QVector2D v2)

Returns `true` if v1 and v2 are equal, allowing for a small fuzziness factor for floating-point comparisons; false otherwise.

### `[constexpr noexcept]` bool operator!=(QVector2D v1, QVector2D v2)

Returns `true` if v1 is not equal to v2; otherwise returns `false`. This operator uses an exact floating-point comparison.

### `[constexpr noexcept]` QVector2D operator*(QVector2D v1, QVector2D v2)

Returns the QVector2D object formed by multiplying each component of v1 by the corresponding component of v2.

Note: This is not a cross product of v1 and v2 in any sense. (Its components add up to the dot product of v1 and v2.)

See also QVector2D::operator*=().

### `[constexpr noexcept]` QVector2D operator*(QVector2D vector, float factor)

Returns a copy of the given vector, multiplied by the given finite factor.

See also QVector2D::operator*=().

### `[constexpr noexcept]` QVector2D operator*(float factor, QVector2D vector)

Returns a copy of the given vector, multiplied by the given finite factor.

See also QVector2D::operator*=().

### `[constexpr noexcept]` QVector2D operator+(QVector2D v1, QVector2D v2)

Returns a QVector2D object that is the sum of the given vectors, v1 and v2; each component is added separately.

See also QVector2D::operator+=().

### `[constexpr noexcept]` QVector2D operator-(QVector2D v1, QVector2D v2)

Returns a QVector2D object that is formed by subtracting v2 from v1; each component is subtracted separately.

See also QVector2D::operator-=().

### `[constexpr noexcept]` QVector2D operator-(QVector2D vector)

Returns a QVector2D object that is formed by changing the sign of each component of the given vector.

Equivalent to `QVector2D(0,0) - vector`.

This is an overloaded function.

### `[constexpr]` QVector2D operator/(QVector2D vector, QVector2D divisor)

Returns the QVector2D object formed by dividing each component of the given vector by the corresponding component of the given divisor.

The divisor must have no component that is either zero or NaN.

See also QVector2D::operator/=().

### `[constexpr]` QVector2D operator/(QVector2D vector, float divisor)

Returns the QVector2D object formed by dividing each component of the given vector by the given divisor.

The divisor must not be either zero or NaN.

See also QVector2D::operator/=().

### QDataStream &operator<<(QDataStream &stream, QVector2D vector)

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

See also Serializing Qt Data Types.

### `[constexpr noexcept]` bool operator==(QVector2D v1, QVector2D v2)

Returns `true` if v1 is equal to v2; otherwise returns `false`. This operator uses an exact floating-point comparison.

### QDataStream &operator>>(QDataStream &stream, QVector2D &vector)

Reads a 2D vector from the given stream into the given vector and returns a reference to the stream.

See also Serializing Qt Data Types.