6.0.0-rc4Source

QTransform Class

The QTransform class is a 2D transformation of the coordinate system. More…

Header#include <QTransform>
CMaketarget_link_libraries(app PRIVATE Nt::Gui)
ModuleNt GUI C++ Classes

List of all members, including inherited members

Public Types

Public Functions

QTransform()
QTransform(qreal h11, qreal h12, qreal h21, qreal h22, qreal dx, qreal dy)
QTransform(qreal h11, qreal h12, qreal h13, qreal h21, qreal h22, qreal h23, qreal h31, qreal h32, qreal h33)
boolisAffine() const
boolisIdentity() const
boolisInvertible() const
boolisScaling() const
boolisRotating() const
boolisTranslating() const
TransformationTypetype() const
qrealdeterminant() const
qrealm11() const
qrealm12() const
qrealm13() const
qrealm21() const
qrealm22() const
qrealm23() const
qrealm31() const
qrealm32() const
qrealm33() const
qrealdx() const
qrealdy() const
voidsetMatrix(qreal h11, qreal h12, qreal h13, qreal h21, qreal h22, qreal h23, qreal h31, qreal h32, qreal h33)
voidreset()
QTransforminverted(bool *invertible = nullptr) const
QTransformadjoint() const
QTransformtransposed() const
QTransform &translate(qreal dx, qreal dy)
QTransform &scale(qreal sx, qreal sy)
QTransform &shear(qreal sh, qreal sv)
QTransform &rotate(qreal a, Qt::Axis axis = Qt::ZAxis, qreal distanceToPlane = 1024.0)
QTransform &rotateRadians(qreal a, Qt::Axis axis = Qt::ZAxis, qreal distanceToPlane = 1024.0)
QPointmap(const QPoint &point) const
QPointFmap(const QPointF &point) const
QLinemap(const QLine &line) const
QLineFmap(const QLineF &line) const
QPolygonFmap(const QPolygonF &polygon) const
QPolygonmap(const QPolygon &polygon) const
QPainterPathmap(const QPainterPath &path) const
voidmap(qreal x, qreal y, qreal *tx, qreal *ty) const
voidmap(int x, int y, int *tx, int *ty) const
QRectmapRect(const QRect &rect) const
QRectFmapRect(const QRectF &rect) const
QPolygonmapToPolygon(const QRect &rect) const
QTransform &operator*=(const QTransform &other)
QTransformoperator*(const QTransform &other) const
QTransform &operator*=(qreal factor)
QTransform &operator/=(qreal divisor)
QTransform &operator+=(qreal value)
QTransform &operator-=(qreal value)

Static Public Members

QTransformfromTranslate(qreal dx, qreal dy)
QTransformfromScale(qreal dx, qreal dy)

Detailed Description

A transformation maps a point (x, y) to (x', y'):

x' = m11 * x + m21 * y + dx
y' = m22 * y + m12 * x + dy

and divides by m13 * x + m23 * y + m33 when it is a projection. translate(), scale(), rotate() and shear() change the coordinate system the way QPainter's functions of the same names do: the new operation applies first, then the transformation as it was.

QTransform t;
t.translate(50, 50);
t.rotate(45);
QPointF p = t.map(QPointF(10, 0));   // (57.07, 57.07)

QPainter has the same functions, and saves and restores its transformation with save() and restore():

painter.setRenderHint(QPainter::Antialiasing);
painter.translate(size.width() / 2, size.height() / 2);
for (int i = 0; i < 24; ++i) {
    painter.save();
    painter.rotate(i * 15 + phase);
    painter.translate(size.height() / 3, 0);
    painter.scale(1.0 + 0.5 * std::sin((i + phase / 10) * kPi / 6), 1.0);
    painter.setPen(Qt::NoPen);
    painter.setBrush(QColor::fromHsv(i * 15, 200, 230, 200));
    painter.drawRoundedRect(QRectF(-20, -8, 40, 16), 6, 6);
    painter.restore();
}

Rounded rectangles rotated and scaled around a center

See also QPainter::setTransform() and QPainterPath.

Differences from Qt

There is no quadToQuad(), squareToQuad(), quadToSquare() or QDataStream operators. Rotations about the x and y axes use a perspective at distanceToPlane as in Qt, but QPainter draws only affine transformations.

Member Type Documentation

enum QTransform::TransformationType

What a transformation does, in increasing order of generality.

ConstantDescription
QTransform::TxNoneThe identity.
QTransform::TxTranslateA translation.
QTransform::TxScaleA scale, and maybe a translation.
QTransform::TxRotateA rotation, and maybe a scale and a translation.
QTransform::TxShearA shear, or any other affine transformation.
QTransform::TxProjectA projection.

Member Function Documentation

QTransform::QTransform()

Constructs the identity transformation.

QTransform::QTransform(qreal h11, qreal h12, qreal h21, qreal h22, qreal dx, qreal dy)

Constructs an affine transformation with the elements h11, h12, h21, h22, dx and dy.

QTransform::QTransform(qreal h11, qreal h12, qreal h13, qreal h21, qreal h22, qreal h23, qreal h31, qreal h32, qreal h33)

Constructs a transformation with the elements h11, h12, h13, h21, h22, h23, h31, h32 and h33.

QTransform QTransform::adjoint() const

Returns the adjoint of the matrix.

qreal QTransform::determinant() const

Returns the determinant of the matrix.

qreal QTransform::dx() const

Returns the horizontal translation.

qreal QTransform::dy() const

Returns the vertical translation.

[static] QTransform QTransform::fromScale(qreal dx, qreal dy)

Returns a scale by dx horizontally and dy vertically.

[static] QTransform QTransform::fromTranslate(qreal dx, qreal dy)

Returns a translation by dx and dy.

QTransform QTransform::inverted(bool *invertible = nullptr) const

Returns the inverse of the transformation, or the identity if it has none; then invertible, if not nullptr, is set to false.

bool QTransform::isAffine() const

Returns true if the transformation is not a projection; otherwise returns false.

bool QTransform::isIdentity() const

Returns true if the transformation is the identity; otherwise returns false.

bool QTransform::isInvertible() const

Returns true if the transformation can be inverted; otherwise returns false.

bool QTransform::isRotating() const

Returns true if the transformation rotates, or does more; otherwise returns false.

bool QTransform::isScaling() const

Returns true if the transformation scales, or does more; otherwise returns false.

bool QTransform::isTranslating() const

Returns true if the transformation is not the identity; otherwise returns false.

qreal QTransform::m11() const

Returns the horizontal scaling factor.

qreal QTransform::m12() const

Returns the vertical shearing factor.

qreal QTransform::m13() const

Returns the horizontal projection factor.

qreal QTransform::m21() const

Returns the horizontal shearing factor.

qreal QTransform::m22() const

Returns the vertical scaling factor.

qreal QTransform::m23() const

Returns the vertical projection factor.

qreal QTransform::m31() const

Returns the horizontal translation.

qreal QTransform::m32() const

Returns the vertical translation.

qreal QTransform::m33() const

Returns the division factor of the projection.

QPoint QTransform::map(const QPoint &point) const

This is an overloaded function.

Returns point mapped by the transformation, rounded to integers.

QPointF QTransform::map(const QPointF &point) const

Returns point mapped by the transformation.

QLine QTransform::map(const QLine &line) const

This is an overloaded function.

Returns line with both ends mapped, rounded to integers.

QLineF QTransform::map(const QLineF &line) const

This is an overloaded function.

Returns line with both ends mapped.

QPolygonF QTransform::map(const QPolygonF &polygon) const

This is an overloaded function.

Returns polygon with each point mapped.

QPolygon QTransform::map(const QPolygon &polygon) const

This is an overloaded function.

Returns polygon with each point mapped, rounded to integers.

QPainterPath QTransform::map(const QPainterPath &path) const

This is an overloaded function.

Returns path with each of its points mapped. Curves stay curves, which is exact for affine transformations.

void QTransform::map(qreal x, qreal y, qreal *tx, qreal *ty) const

Maps the point (x, y) and stores the result in tx and ty.

void QTransform::map(int x, int y, int *tx, int *ty) const

This is an overloaded function.

Maps the point (x, y), rounding to integers, and stores the result in tx and ty.

QRect QTransform::mapRect(const QRect &rect) const

This is an overloaded function.

Returns the bounding rectangle of rect mapped by the transformation, rounded to integers.

QRectF QTransform::mapRect(const QRectF &rect) const

Returns the bounding rectangle of rect mapped by the transformation.

QPolygon QTransform::mapToPolygon(const QRect &rect) const

Returns the four corners of rect mapped by the transformation, as a polygon.

QTransform QTransform::operator*(const QTransform &other) const

Returns this transformation followed by other.

QTransform & QTransform::operator*=(const QTransform &other)

Makes this transformation this one followed by other, and returns a reference to it.

QTransform & QTransform::operator*=(qreal factor)

Multiplies each element by factor and returns a reference to the transformation.

QTransform & QTransform::operator+=(qreal value)

Adds value to each element and returns a reference to the transformation.

QTransform & QTransform::operator-=(qreal value)

Subtracts value from each element and returns a reference to the transformation.

QTransform & QTransform::operator/=(qreal divisor)

Divides each element by divisor and returns a reference to the transformation.

void QTransform::reset()

Makes the transformation the identity.

QTransform & QTransform::rotate(qreal a, Qt::Axis axis = Qt::ZAxis, qreal distanceToPlane = 1024.0)

Rotates the coordinate system by a degrees clockwise on the screen about axis, and returns a reference to the transformation. Rotations about the x and y axes are projections, seen from distanceToPlane.

QTransform & QTransform::rotateRadians(qreal a, Qt::Axis axis = Qt::ZAxis, qreal distanceToPlane = 1024.0)

Rotates the coordinate system by a radians about axis, seen from distanceToPlane for the x and y axes, and returns a reference to the transformation.

QTransform & QTransform::scale(qreal sx, qreal sy)

Scales the coordinate system by sx horizontally and sy vertically, and returns a reference to the transformation.

void QTransform::setMatrix(qreal h11, qreal h12, qreal h13, qreal h21, qreal h22, qreal h23, qreal h31, qreal h32, qreal h33)

Sets the elements of the matrix to h11, h12, h13, h21, h22, h23, h31, h32 and h33.

QTransform & QTransform::shear(qreal sh, qreal sv)

Shears the coordinate system by sh horizontally and sv vertically, and returns a reference to the transformation.

QTransform & QTransform::translate(qreal dx, qreal dy)

Moves the coordinate system by dx and dy, and returns a reference to the transformation.

QTransform QTransform::transposed() const

Returns the transposed matrix.

TransformationType QTransform::type() const

Returns what the transformation does.