Movatterモバイル変換


[0]ホーム

URL:


We bake cookies in your browser for a better experience. Using this site means that you consent.Read More

Menu

Qt Documentation

QTransform Class

TheQTransform class specifies 2D transformations of a coordinate system.More...

Header:#include <QTransform>
Since: Qt 4.3

Public Types

enumTransformationType { TxNone, TxTranslate, TxScale, TxRotate, TxShear, TxProject }

Public Functions

QTransform()
QTransform(qreal m11, qreal m12, qreal m13, qreal m21, qreal m22, qreal m23, qreal m31, qreal m32, qreal m33 = 1.0)
QTransform(qreal m11, qreal m12, qreal m21, qreal m22, qreal dx, qreal dy)
QTransform(const QMatrix & matrix)
qrealm11() const
qrealm12() const
qrealm13() const
qrealm21() const
qrealm22() const
qrealm23() const
qrealm31() const
qrealm32() const
qrealm33() const
QTransformadjoint() const
qrealdeterminant() const
qrealdx() const
qrealdy() const
QTransforminverted(bool * invertible = 0) const
boolisAffine() const
boolisIdentity() const
boolisInvertible() const
boolisRotating() const
boolisScaling() const
boolisTranslating() const
voidmap(qreal x, qreal y, qreal * tx, qreal * ty) const
QPointFmap(const QPointF & p) const
QPointmap(const QPoint & point) const
QLinemap(const QLine & l) const
QLineFmap(const QLineF & line) const
QPolygonFmap(const QPolygonF & polygon) const
QPolygonmap(const QPolygon & polygon) const
QRegionmap(const QRegion & region) const
QPainterPathmap(const QPainterPath & path) const
voidmap(int x, int y, int * tx, int * ty) const
QRectFmapRect(const QRectF & rectangle) const
QRectmapRect(const QRect & rectangle) const
QPolygonmapToPolygon(const QRect & rectangle) const
voidreset()
QTransform &rotate(qreal angle, Qt::Axis axis = Qt::ZAxis)
QTransform &rotateRadians(qreal angle, Qt::Axis axis = Qt::ZAxis)
QTransform &scale(qreal sx, qreal sy)
voidsetMatrix(qreal m11, qreal m12, qreal m13, qreal m21, qreal m22, qreal m23, qreal m31, qreal m32, qreal m33)
QTransform &shear(qreal sh, qreal sv)
const QMatrix &toAffine() const
QTransform &translate(qreal dx, qreal dy)
QTransformtransposed() const
TransformationTypetype() const
operator QVariant() const
booloperator!=(const QTransform & matrix) const
QTransformoperator*(const QTransform & matrix) const
QTransform &operator*=(const QTransform & matrix)
QTransform &operator*=(qreal scalar)
QTransform &operator+=(qreal scalar)
QTransform &operator-=(qreal scalar)
QTransform &operator/=(qreal scalar)
QTransform &operator=(const QTransform & matrix)
booloperator==(const QTransform & matrix) const

Static Public Members

QTransformfromScale(qreal sx, qreal sy)
QTransformfromTranslate(qreal dx, qreal dy)
boolquadToQuad(const QPolygonF & one, const QPolygonF & two, QTransform & trans)
boolquadToSquare(const QPolygonF & quad, QTransform & trans)
boolsquareToQuad(const QPolygonF & quad, QTransform & trans)

Related Non-Members

boolqFuzzyCompare(const QTransform & t1, const QTransform & t2)
QPointoperator*(const QPoint & point, const QTransform & matrix)
QPointFoperator*(const QPointF & point, const QTransform & matrix)
QLineFoperator*(const QLineF & line, const QTransform & matrix)
QLineoperator*(const QLine & line, const QTransform & matrix)
QPolygonoperator*(const QPolygon & polygon, const QTransform & matrix)
QPolygonFoperator*(const QPolygonF & polygon, const QTransform & matrix)
QRegionoperator*(const QRegion & region, const QTransform & matrix)
QPainterPathoperator*(const QPainterPath & path, const QTransform & matrix)
QDataStream &operator<<(QDataStream & stream, const QTransform & matrix)
QDataStream &operator>>(QDataStream & stream, QTransform & matrix)

Detailed Description

TheQTransform class specifies 2D transformations of a coordinate system.

A transformation specifies how to translate, scale, shear, rotate or project the coordinate system, and is typically used when rendering graphics.

QTransform differs from QMatrix in that it is a true 3x3 matrix, allowing perspective transformations.QTransform'stoAffine() method allows castingQTransform to QMatrix. If a perspective transformation has been specified on the matrix, then the conversion will cause loss of data.

QTransform is the recommended transformation class in Qt.

AQTransform object can be built using thesetMatrix(),scale(),rotate(),translate() andshear() functions. Alternatively, it can be built by applyingbasic matrix operations. The matrix can also be defined when constructed, and it can be reset to the identity matrix (the default) using thereset() function.

TheQTransform class supports mapping of graphic primitives: A given point, line, polygon, region, or painter path can be mapped to the coordinate system defined bythis matrix using themap() function. In case of a rectangle, its coordinates can be transformed using themapRect() function. A rectangle can also be transformed into apolygon (mapped to the coordinate system defined bythis matrix), using themapToPolygon() function.

QTransform provides theisIdentity() function which returns true if the matrix is the identity matrix, and theisInvertible() function which returns true if the matrix is non-singular (i.e. AB = BA = I). Theinverted() function returns an inverted copy ofthis matrix if it is invertible (otherwise it returns the identity matrix), andadjoint() returns the matrix's classical adjoint. In addition,QTransform provides thedeterminant() function which returns the matrix's determinant.

Finally, theQTransform class supports matrix multiplication, addition and subtraction, and objects of the class can be streamed as well as compared.

Rendering Graphics

When rendering graphics, the matrix defines the transformations but the actual transformation is performed by the drawing routines inQPainter.

By default,QPainter operates on the associated device's own coordinate system. The standard coordinate system of aQPaintDevice has its origin located at the top-left position. Thex values increase to the right;y values increase downward. For a complete description, see thecoordinate system documentation.

QPainter has functions to translate, scale, shear and rotate the coordinate system without using aQTransform. For example:

void SimpleTransformation::paintEvent(QPaintEvent*){QPainter painter(this);    painter.setPen(QPen(Qt::blue,1,Qt::DashLine));    painter.drawRect(0,0,100,100);    painter.rotate(45);    painter.setFont(QFont("Helvetica",24));    painter.setPen(QPen(Qt::black,1));    painter.drawText(20,10,"QTransform");}

Although these functions are very convenient, it can be more efficient to build aQTransform and callQPainter::setTransform() if you want to perform more than a single transform operation. For example:

void CombinedTransformation::paintEvent(QPaintEvent*){QPainter painter(this);    painter.setPen(QPen(Qt::blue,1,Qt::DashLine));    painter.drawRect(0,0,100,100);QTransform transform;    transform.translate(50,50);    transform.rotate(45);    transform.scale(0.5,1.0);    painter.setTransform(transform);    painter.setFont(QFont("Helvetica",24));    painter.setPen(QPen(Qt::black,1));    painter.drawText(20,10,"QTransform");}

Basic Matrix Operations

AQTransform object contains a 3 x 3 matrix. Them31 (dx) andm32 (dy) elements specify horizontal and vertical translation. Them11 andm22 elements specify horizontal and vertical scaling. Them21 andm12 elements specify horizontal and verticalshearing. And finally, them13 andm23 elements specify horizontal and vertical projection, withm33 as an additional projection factor.

QTransform transforms a point in the plane to another point using the following formulas:

x' = m11*x + m21*y + dxy'= m22*y+ m12*x+ dyif (isnot affine) {    w' = m13*x + m23*y + m33    x'/= w'    y'/= w'}

The point(x, y) is the original point, and(x', y') is the transformed point.(x', y') can be transformed back to(x, y) by performing the same operation on theinverted() matrix.

The various matrix elements can be set when constructing the matrix, or by using thesetMatrix() function later on. They can also be manipulated using thetranslate(),rotate(),scale() andshear() convenience functions. The currently set values can be retrieved using them11(),m12(),m13(),m21(),m22(),m23(),m31(),m32(),m33(),dx() anddy() functions.

Translation is the simplest transformation. Settingdx anddy will move the coordinate systemdx units along the X axis anddy units along the Y axis. Scaling can be done by settingm11 andm22. For example, settingm11 to 2 andm22 to 1.5 will double the height and increase the width by 50%. The identity matrix hasm11,m22, andm33 set to 1 (all others are set to 0) mapping a point to itself. Shearing is controlled bym12 andm21. Setting these elements to values different from zero will twist the coordinate system. Rotation is achieved by setting both the shearing factors and the scaling factors. Perspective transformation is achieved by setting both the projection factors and the scaling factors.

Here's the combined transformations example using basic matrix operations:

void BasicOperations::paintEvent(QPaintEvent*){double pi=3.14;double a= pi/180*45.0;double sina= sin(a);double cosa= cos(a);QTransform translationTransform(1,0,0,1,50.0,50.0);QTransform rotationTransform(cosa, sina,-sina, cosa,0,0);QTransform scalingTransform(0.5,0,0,1.0,0,0);QTransform transform;    transform= scalingTransform* rotationTransform* translationTransform;QPainter painter(this);    painter.setPen(QPen(Qt::blue,1,Qt::DashLine));    painter.drawRect(0,0,100,100);    painter.setTransform(transform);    painter.setFont(QFont("Helvetica",24));    painter.setPen(QPen(Qt::black,1));    painter.drawText(20,10,"QTransform");}

See alsoQPainter,Coordinate System,Affine Transformations Demo, andTransformations Example.

Member Type Documentation

enum QTransform::TransformationType

ConstantValue
QTransform::TxNone0x00
QTransform::TxTranslate0x01
QTransform::TxScale0x02
QTransform::TxRotate0x04
QTransform::TxShear0x08
QTransform::TxProject0x10

Member Function Documentation

QTransform::QTransform()

Constructs an identity matrix.

All elements are set to zero exceptm11 andm22 (specifying the scale) andm13 which are set to 1.

See alsoreset().

QTransform::QTransform(qreal m11,qreal m12,qreal m13,qreal m21,qreal m22,qreal m23,qreal m31,qreal m32,qreal m33 = 1.0)

Constructs a matrix with the elements,m11,m12,m13,m21,m22,m23,m31,m32,m33.

See alsosetMatrix().

QTransform::QTransform(qreal m11,qreal m12,qreal m21,qreal m22,qreal dx,qreal dy)

Constructs a matrix with the elements,m11,m12,m21,m22,dx anddy.

See alsosetMatrix().

QTransform::QTransform(constQMatrix & matrix)

Constructs a matrix that is a copy of the givenmatrix. Note that them13,m23, andm33 elements are set to 0, 0, and 1 respectively.

qreal QTransform::m11() const

Returns the horizontal scaling factor.

See alsoscale() andBasic Matrix Operations.

qreal QTransform::m12() const

Returns the vertical shearing factor.

See alsoshear() andBasic Matrix Operations.

qreal QTransform::m13() const

Returns the horizontal projection factor.

See alsotranslate() andBasic Matrix Operations.

qreal QTransform::m21() const

Returns the horizontal shearing factor.

See alsoshear() andBasic Matrix Operations.

qreal QTransform::m22() const

Returns the vertical scaling factor.

See alsoscale() andBasic Matrix Operations.

qreal QTransform::m23() const

Returns the vertical projection factor.

See alsotranslate() andBasic Matrix Operations.

qreal QTransform::m31() const

Returns the horizontal translation factor.

See alsodx(),translate(), andBasic Matrix Operations.

qreal QTransform::m32() const

Returns the vertical translation factor.

See alsody(),translate(), andBasic Matrix Operations.

qreal QTransform::m33() const

Returns the division factor.

See alsotranslate() andBasic Matrix Operations.

QTransform QTransform::adjoint() const

Returns the adjoint of this matrix.

qreal QTransform::determinant() const

Returns the matrix's determinant.

qreal QTransform::dx() const

Returns the horizontal translation factor.

See alsom31(),translate(), andBasic Matrix Operations.

qreal QTransform::dy() const

Returns the vertical translation factor.

See alsotranslate() andBasic Matrix Operations.

[static]QTransform QTransform::fromScale(qreal sx,qreal sy)

Creates a matrix which corresponds to a scaling ofsx horizontally andsy vertically. This is the same asQTransform().scale(sx, sy) but slightly faster.

This function was introduced in Qt 4.5.

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

Creates a matrix which corresponds to a translation ofdx along the x axis anddy along the y axis. This is the same asQTransform().translate(dx, dy) but slightly faster.

This function was introduced in Qt 4.5.

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

Returns an inverted copy of this matrix.

If the matrix is singular (not invertible), the returned matrix is the identity matrix. Ifinvertible is valid (i.e. not 0), its value is set to true if the matrix is invertible, otherwise it is set to false.

See alsoisInvertible().

bool QTransform::isAffine() const

Returns true if the matrix represent an affine transformation, otherwise returns false.

bool QTransform::isIdentity() const

Returns true if the matrix is the identity matrix, otherwise returns false.

See alsoreset().

bool QTransform::isInvertible() const

Returns true if the matrix is invertible, otherwise returns false.

See alsoinverted().

bool QTransform::isRotating() const

Returns true if the matrix represents some kind of a rotating transformation, otherwise returns false.

See alsoreset().

bool QTransform::isScaling() const

Returns true if the matrix represents a scaling transformation, otherwise returns false.

See alsoreset().

bool QTransform::isTranslating() const

Returns true if the matrix represents a translating transformation, otherwise returns false.

See alsoreset().

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

Maps the given coordinatesx andy into the coordinate system defined by this matrix. The resulting values are put in *tx and *ty, respectively.

The coordinates are transformed using the following formulas:

x'= m11*x+ m21*y+ dxy' = m22*y + m12*x + dyif (is not affine) {    w'= m13*x+ m23*y+ m33    x' /= w'    y' /= w'}

The point (x, y) is the original point, and (x', y') is the transformed point.

See alsoBasic Matrix Operations.

QPointF QTransform::map(constQPointF & p) const

This is an overloaded function.

Creates and returns aQPointF object that is a copy of the given point,p, mapped into the coordinate system defined by this matrix.

QPoint QTransform::map(constQPoint & point) const

This is an overloaded function.

Creates and returns aQPoint object that is a copy of the givenpoint, mapped into the coordinate system defined by this matrix. Note that the transformed coordinates are rounded to the nearest integer.

QLine QTransform::map(constQLine & l) const

This is an overloaded function.

Creates and returns aQLineF object that is a copy of the given line,l, mapped into the coordinate system defined by this matrix.

QLineF QTransform::map(constQLineF & line) const

This is an overloaded function.

Creates and returns aQLine object that is a copy of the givenline, mapped into the coordinate system defined by this matrix. Note that the transformed coordinates are rounded to the nearest integer.

QPolygonF QTransform::map(constQPolygonF & polygon) const

This is an overloaded function.

Creates and returns aQPolygonF object that is a copy of the givenpolygon, mapped into the coordinate system defined by this matrix.

QPolygon QTransform::map(constQPolygon & polygon) const

This is an overloaded function.

Creates and returns aQPolygon object that is a copy of the givenpolygon, mapped into the coordinate system defined by this matrix. Note that the transformed coordinates are rounded to the nearest integer.

QRegion QTransform::map(constQRegion & region) const

This is an overloaded function.

Creates and returns aQRegion object that is a copy of the givenregion, mapped into the coordinate system defined by this matrix.

Calling this method can be rather expensive if rotations or shearing are used.

QPainterPath QTransform::map(constQPainterPath & path) const

This is an overloaded function.

Creates and returns aQPainterPath object that is a copy of the givenpath, mapped into the coordinate system defined by this matrix.

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

This is an overloaded function.

Maps the given coordinatesx andy into the coordinate system defined by this matrix. The resulting values are put in *tx and *ty, respectively. Note that the transformed coordinates are rounded to the nearest integer.

QRectF QTransform::mapRect(constQRectF & rectangle) const

Creates and returns aQRectF object that is a copy of the givenrectangle, mapped into the coordinate system defined by this matrix.

The rectangle's coordinates are transformed using the following formulas:

x' = m11*x + m21*y + dxy'= m22*y+ m12*x+ dyif (isnot affine) {    w' = m13*x + m23*y + m33    x'/= w'    y'/= w'}

If rotation or shearing has been specified, this function returns thebounding rectangle. To retrieve the exact region the givenrectangle maps to, use themapToPolygon() function instead.

See alsomapToPolygon() andBasic Matrix Operations.

QRect QTransform::mapRect(constQRect & rectangle) const

This is an overloaded function.

Creates and returns aQRect object that is a copy of the givenrectangle, mapped into the coordinate system defined by this matrix. Note that the transformed coordinates are rounded to the nearest integer.

QPolygon QTransform::mapToPolygon(constQRect & rectangle) const

Creates and returns aQPolygon representation of the givenrectangle, mapped into the coordinate system defined by this matrix.

The rectangle's coordinates are transformed using the following formulas:

x'= m11*x+ m21*y+ dxy' = m22*y + m12*x + dyif (is not affine) {    w'= m13*x+ m23*y+ m33    x' /= w'    y' /= w'}

Polygons and rectangles behave slightly differently when transformed (due to integer rounding), somatrix.map(QPolygon(rectangle)) is not always the same asmatrix.mapToPolygon(rectangle).

See alsomapRect() andBasic Matrix Operations.

[static]bool QTransform::quadToQuad(constQPolygonF & one, constQPolygonF & two,QTransform & trans)

Creates a transformation matrix,trans, that maps a four-sided polygon,one, to another four-sided polygon,two. Returns true if the transformation is possible; otherwise returns false.

This is a convenience method combiningquadToSquare() andsquareToQuad() methods. It allows the input quad to be transformed into any other quad.

See alsosquareToQuad() andquadToSquare().

[static]bool QTransform::quadToSquare(constQPolygonF & quad,QTransform & trans)

Creates a transformation matrix,trans, that maps a four-sided polygon,quad, to a unit square. Returns true if the transformation is constructed or false if such a transformation does not exist.

See alsosquareToQuad() andquadToQuad().

void QTransform::reset()

Resets the matrix to an identity matrix, i.e. all elements are set to zero, exceptm11 andm22 (specifying the scale) andm33 which are set to 1.

See alsoQTransform(),isIdentity(), andBasic Matrix Operations.

QTransform & QTransform::rotate(qreal angle,Qt::Axis axis = Qt::ZAxis)

Rotates the coordinate system counterclockwise by the givenangle about the specifiedaxis and returns a reference to the matrix.

Note that if you apply aQTransform to a point defined in widget coordinates, the direction of the rotation will be clockwise because the y-axis points downwards.

The angle is specified in degrees.

See alsosetMatrix().

QTransform & QTransform::rotateRadians(qreal angle,Qt::Axis axis = Qt::ZAxis)

Rotates the coordinate system counterclockwise by the givenangle about the specifiedaxis and returns a reference to the matrix.

Note that if you apply aQTransform to a point defined in widget coordinates, the direction of the rotation will be clockwise because the y-axis points downwards.

The angle is specified in radians.

See alsosetMatrix().

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

Scales the coordinate system bysx horizontally andsy vertically, and returns a reference to the matrix.

See alsosetMatrix().

void QTransform::setMatrix(qreal m11,qreal m12,qreal m13,qreal m21,qreal m22,qreal m23,qreal m31,qreal m32,qreal m33)

Sets the matrix elements to the specified values,m11,m12,m13m21,m22,m23m31,m32 andm33. Note that this function replaces the previous values.QTransform provides thetranslate(),rotate(),scale() andshear() convenience functions to manipulate the various matrix elements based on the currently defined coordinate system.

See alsoQTransform().

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

Shears the coordinate system bysh horizontally andsv vertically, and returns a reference to the matrix.

See alsosetMatrix().

[static]bool QTransform::squareToQuad(constQPolygonF & quad,QTransform & trans)

Creates a transformation matrix,trans, that maps a unit square to a four-sided polygon,quad. Returns true if the transformation is constructed or false if such a transformation does not exist.

See alsoquadToSquare() andquadToQuad().

constQMatrix & QTransform::toAffine() const

Returns theQTransform as an affine matrix.

Warning: If a perspective transformation has been specified, then the conversion will cause loss of data.

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

Moves the coordinate systemdx along the x axis anddy along the y axis, and returns a reference to the matrix.

See alsosetMatrix().

QTransform QTransform::transposed() const

Returns the transpose of this matrix.

TransformationType QTransform::type() const

Returns the transformation type of this matrix.

The transformation type is the highest enumeration value capturing all of the matrix's transformations. For example, if the matrix both scales and shears, the type would beTxShear, becauseTxShear has a higher enumeration value thanTxScale.

Knowing the transformation type of a matrix is useful for optimization: you can often handle specific types more optimally than handling the generic case.

QTransform::operator QVariant() const

Returns the transform as aQVariant.

bool QTransform::operator!=(constQTransform & matrix) const

Returns true if this matrix is not equal to the givenmatrix, otherwise returns false.

QTransform QTransform::operator*(constQTransform & matrix) const

Returns the result of multiplying this matrix by the givenmatrix.

Note that matrix multiplication is not commutative, i.e. a*b != b*a.

QTransform & QTransform::operator*=(constQTransform & matrix)

This is an overloaded function.

Returns the result of multiplying this matrix by the givenmatrix.

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

This is an overloaded function.

Returns the result of performing an element-wise multiplication of this matrix with the givenscalar.

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

This is an overloaded function.

Returns the matrix obtained by adding the givenscalar to each element of this matrix.

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

This is an overloaded function.

Returns the matrix obtained by subtracting the givenscalar from each element of this matrix.

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

This is an overloaded function.

Returns the result of performing an element-wise division of this matrix by the givenscalar.

QTransform & QTransform::operator=(constQTransform & matrix)

Assigns the givenmatrix's values to this matrix.

bool QTransform::operator==(constQTransform & matrix) const

Returns true if this matrix is equal to the givenmatrix, otherwise returns false.

Related Non-Members

boolqFuzzyCompare(constQTransform & t1, constQTransform & t2)

Returns true ift1 andt2 are equal, allowing for a small fuzziness factor for floating-point comparisons; false otherwise.

This function was introduced in Qt 4.6.

QPointoperator*(constQPoint & point, constQTransform & matrix)

This is the same asmatrix.map(point).

See alsoQTransform::map().

QPointFoperator*(constQPointF & point, constQTransform & matrix)

Same asmatrix.map(point).

See alsoQTransform::map().

QLineFoperator*(constQLineF & line, constQTransform & matrix)

This is the same asmatrix.map(line).

See alsoQTransform::map().

QLineoperator*(constQLine & line, constQTransform & matrix)

This is the same asmatrix.map(line).

See alsoQTransform::map().

QPolygonoperator*(constQPolygon & polygon, constQTransform & matrix)

This is the same asmatrix.map(polygon).

See alsoQTransform::map().

QPolygonFoperator*(constQPolygonF & polygon, constQTransform & matrix)

This is the same asmatrix.map(polygon).

This function was introduced in Qt 4.3.

See alsoQTransform::map().

QRegionoperator*(constQRegion & region, constQTransform & matrix)

This is the same asmatrix.map(region).

See alsoQTransform::map().

QPainterPathoperator*(constQPainterPath & path, constQTransform & matrix)

This is the same asmatrix.map(path).

This function was introduced in Qt 4.3.

See alsoQTransform::map().

QDataStream &operator<<(QDataStream & stream, constQTransform & matrix)

Writes the givenmatrix to the givenstream and returns a reference to the stream.

This function was introduced in Qt 4.3.

See alsoSerializing Qt Data Types.

QDataStream &operator>>(QDataStream & stream,QTransform & matrix)

Reads the givenmatrix from the givenstream and returns a reference to the stream.

This function was introduced in Qt 4.3.

See alsoSerializing Qt Data Types.

© 2016 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of theGNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.


[8]ページ先頭

©2009-2025 Movatter.jp