class QVector3D#

The `QVector3D` class represents a vector or vertex in 3D space. More

New in version 4.6.

# Synopsis#

## Methods#

• def `operator/=()`

• def `operator[]()`

## Static functions#

Note

This documentation may contain snippets that were automatically translated from C++ to Python. We always welcome contributions to the snippet translation. If you see an issue with the translation, you can also let us know by creating a ticket on https:/bugreports.qt.io/projects/PYSIDE

# Detailed Description#

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

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

__init__(point)#
Parameters:

point`QPoint`

Constructs a vector with x and y coordinates from a 2D `point`, and a z coordinate of 0.

__init__(point)
Parameters:

point`QPointF`

Constructs a vector with x and y coordinates from a 2D `point`, and a z coordinate of 0.

__init__(vector)
Parameters:

vector`QVector2D`

Constructs a 3D vector from the specified 2D `vector`. The z coordinate is set to zero.

__init__(vector, zpos)
Parameters:

Constructs a 3D vector from the specified 2D `vector`. The z coordinate is set to `zpos`, which must be finite.

__init__(vector)
Parameters:

vector`QVector4D`

Constructs a 3D vector from the specified 4D `vector`. The w coordinate is dropped.

__init__(xpos, ypos, zpos)
Parameters:
• xpos – float

• ypos – float

• zpos – float

Constructs a vector with coordinates (`xpos`, `ypos`, `zpos`). All parameters must be finite.

__init__()

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

__reduce__()#
Return type:

object

__repr__()#
Return type:

object

static crossProduct(v1, v2)#
Parameters:
Return type:

`QVector3D`

Returns the cross-product of vectors `v1` and `v2`, which is normal to the plane spanned by `v1` and `v2`. It will be zero if the two vectors are parallel.

distanceToLine(point, direction)#
Parameters:
Return type:

float

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.

distanceToPlane(plane, normal)#
Parameters:
Return type:

float

Returns the distance from this vertex to a plane defined by the vertex `plane` and a `normal` unit vector. The `normal` parameter is assumed to have been normalized to a unit vector.

The return value will be negative if the vertex is below the plane, or zero if it is on the plane.

distanceToPlane(plane1, plane2, plane3)
Parameters:
Return type:

float

Returns the distance from this vertex to a plane defined by the vertices `plane1`, `plane2` and `plane3`.

The return value will be negative if the vertex is below the plane, or zero if it is on the plane.

The two vectors that define the plane are `plane2` - `plane1` and `plane3` - `plane1`.

distanceToPoint(point)#
Parameters:

point`QVector3D`

Return type:

float

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

static dotProduct(v1, v2)#
Parameters:
Return type:

float

Returns the dot product of `v1` and `v2`.

isNull()#
Return type:

bool

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

length()#
Return type:

float

Returns the length of the vector from the origin.

lengthSquared()#
Return type:

float

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

static normal(v1, v2)#
Parameters:
Return type:

`QVector3D`

Returns the unit normal vector of a plane spanned by vectors `v1` and `v2`, which must not be parallel to one another.

Use `crossProduct()` to compute the cross-product of `v1` and `v2` if you do not need the result to be normalized to a unit vector.

static normal(v1, v2, v3)
Parameters:
Return type:

`QVector3D`

Returns the unit normal vector of a plane spanned by vectors `v2` - `v1` and `v3` - `v1`, which must not be parallel to one another.

Use `crossProduct()` to compute the cross-product of `v2` - `v1` and `v3` - `v1` if you do not need the result to be normalized to a unit vector.

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.

normalized()#
Return type:

`QVector3D`

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.

__ne__(v2)#
Parameters:
Return type:

bool

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

__mul__(factor)#
Parameters:

factor – float

Return type:

`QVector3D`

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

`operator*=()`

__mul__(factor)
Parameters:

factor – float

Return type:

`QVector3D`

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

`operator*=()`

__mul__(quaternion)
Parameters:

quaternion`QQuaternion`

Return type:

`QVector3D`

__mul__(matrix)
Parameters:

matrix`QMatrix4x4`

Return type:

`QVector3D`

Note

This function is deprecated.

__mul__(v2)
Parameters:
Return type:

`QVector3D`

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

Note

This is not the same as the `crossProduct()` of `v1` and `v2`. (Its components add up to the dot product of `v1` and `v2`.)

__mul__(matrix)
Parameters:

matrix`QMatrix4x4`

Return type:

`QVector3D`

Note

This function is deprecated.

__imul__(vector)#
Parameters:

vector`QVector3D`

Return type:

`QVector3D`

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

Note: this is not the same as the `crossProduct()` of this vector and `vector`. (Its components add up to the dot product of this vector and `vector`.)

`crossProduct()` `operator/=()` `operator*()`

__imul__(factor)
Parameters:

factor – float

Return type:

`QVector3D`

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

`operator/=()` `operator*()`

Parameters:
Return type:

`QVector3D`

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

`operator+=()`

Parameters:

vector`QVector3D`

Return type:

`QVector3D`

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

`operator-=()`

__sub__()#
Return type:

`QVector3D`

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

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

__sub__(v2)
Parameters:
Return type:

`QVector3D`

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

`operator-=()`

__isub__(vector)#
Parameters:

vector`QVector3D`

Return type:

`QVector3D`

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

`operator+=()`

__div__(divisor)#
Parameters:

divisor`QVector3D`

Return type:

`QVector3D`

Returns the `QVector3D` 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.

`operator/=()`

__div__(divisor)
Parameters:

divisor – float

Return type:

`QVector3D`

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

The `divisor` must not be either zero or NaN.

`operator/=()`

operator/=(vector)
Parameters:

vector`QVector3D`

Return type:

`QVector3D`

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

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

`operator*=()` `operator/()`

operator/=(divisor)
Parameters:

divisor – float

Return type:

`QVector3D`

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.

`operator*=()` `operator/()`

__eq__(v2)#
Parameters:
Return type:

bool

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

operator(i)#
Parameters:

i – int

Return type:

float

Returns the component of the vector at index position `i`.

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

project(modelView, projection, viewport)#
Parameters:
Return type:

`QVector3D`

Returns the window coordinates of this vector initially in object/model coordinates using the model view matrix `modelView`, the projection matrix `projection` and the viewport dimensions `viewport`.

When transforming from clip to normalized space, a division by the w component on the vector components takes place. To prevent dividing by 0 if w equals to 0, it is set to 1.

Note

the returned y coordinates are in OpenGL orientation. OpenGL expects the bottom to be 0 whereas for Qt top is 0.

setX(x)#
Parameters:

x – float

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

setY(y)#
Parameters:

y – float

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

setZ(z)#
Parameters:

z – float

Sets the z coordinate of this point to the given finite `z` coordinate.

toPoint()#
Return type:

`QPoint`

Returns the QPoint form of this 3D vector. The z coordinate is dropped. The x and y coordinates are rounded to nearest integers.

toPointF()#
Return type:

`QPointF`

Returns the QPointF form of this 3D vector. The z coordinate is dropped.

toTuple()#
Return type:

object

toVector2D()#
Return type:

`QVector2D`

Returns the 2D vector form of this 3D vector, dropping the z coordinate.

toVector4D()#
Return type:

`QVector4D`

Returns the 4D form of this 3D vector, with the w coordinate set to zero.

unproject(modelView, projection, viewport)#
Parameters:
Return type:

`QVector3D`

Returns the object/model coordinates of this vector initially in window coordinates using the model view matrix `modelView`, the projection matrix `projection` and the viewport dimensions `viewport`.

When transforming from clip to normalized space, a division by the w component of the vector components takes place. To prevent dividing by 0 if w equals to 0, it is set to 1.

Note

y coordinates in `viewport` should use OpenGL orientation. OpenGL expects the bottom to be 0 whereas for Qt top is 0.

x()#
Return type:

float

Returns the x coordinate of this point.

y()#
Return type:

float

Returns the y coordinate of this point.