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

QMatrix Class

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

Header:#include <QMatrix>

This class is obsolete. It is provided to keep old source code working. We strongly advise against using it in new code.

Public Functions

QMatrix()
QMatrix(qreal m11, qreal m12, qreal m21, qreal m22, qreal dx, qreal dy)
QMatrix(const QMatrix & matrix)
qrealm11() const
qrealm12() const
qrealm21() const
qrealm22() const
qrealdeterminant() const
qrealdx() const
qrealdy() const
QMatrixinverted(bool * invertible = 0) const
boolisIdentity() const
boolisInvertible() const
voidmap(qreal x, qreal y, qreal * tx, qreal * ty) const
voidmap(int x, int y, int * tx, int * ty) const
QPointFmap(const QPointF & point) const
QPointmap(const QPoint & point) const
QLineFmap(const QLineF & line) const
QLinemap(const QLine & line) const
QPolygonFmap(const QPolygonF & polygon) const
QPolygonmap(const QPolygon & polygon) const
QRegionmap(const QRegion & region) const
QPainterPathmap(const QPainterPath & path) const
QRectFmapRect(const QRectF & rectangle) const
QRectmapRect(const QRect & rectangle) const
QPolygonmapToPolygon(const QRect & rectangle) const
voidreset()
QMatrix &rotate(qreal degrees)
QMatrix &scale(qreal sx, qreal sy)
voidsetMatrix(qreal m11, qreal m12, qreal m21, qreal m22, qreal dx, qreal dy)
QMatrix &shear(qreal sh, qreal sv)
QMatrix &translate(qreal dx, qreal dy)
operator QVariant() const
booloperator!=(const QMatrix & matrix) const
QMatrixoperator*(const QMatrix & matrix) const
QMatrix &operator*=(const QMatrix & matrix)
QMatrix &operator=(const QMatrix & matrix)
booloperator==(const QMatrix & matrix) const

Related Non-Members

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

Detailed Description

TheQMatrix class specifies 2D transformations of a coordinate system.

A matrix specifies how to translate, scale, shear or rotate the coordinate system, and is typically used when rendering graphics.QMatrix, in contrast toQTransform, does not allow perspective transformations.QTransform is the recommended transformation class in Qt.

AQMatrix 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.

TheQMatrix 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.

QMatrix 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). In addition,QMatrix provides thedeterminant() function returning the matrix's determinant.

Finally, theQMatrix class supports matrix multiplication, 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 aQMatrix. 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,"QMatrix");}

Although these functions are very convenient, it can be more efficient to build aQMatrix and callQPainter::setMatrix() 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);QMatrix matrix;    matrix.translate(50,50);    matrix.rotate(45);    matrix.scale(0.5,1.0);    painter.setMatrix(matrix);    painter.setFont(QFont("Helvetica",24));    painter.setPen(QPen(Qt::black,1));    painter.drawText(20,10,"QMatrix");}

Basic Matrix Operations

AQMatrix object contains a 3 x 3 matrix. Thedx anddy elements specify horizontal and vertical translation. Them11 andm22 elements specify horizontal and vertical scaling. And finally, them21 andm12 elements specify horizontal and verticalshearing.

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

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

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(),m21(),m22(),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 andm22 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 carefully setting both the shearing 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);QMatrix translationMatrix(1,0,0,1,50.0,50.0);QMatrix rotationMatrix(cosa, sina,-sina, cosa,0,0);QMatrix scalingMatrix(0.5,0,0,1.0,0,0);QMatrix matrix;    matrix=  scalingMatrix* rotationMatrix* translationMatrix;QPainter painter(this);    painter.setPen(QPen(Qt::blue,1,Qt::DashLine));    painter.drawRect(0,0,100,100);    painter.setMatrix(matrix);    painter.setFont(QFont("Helvetica",24));    painter.setPen(QPen(Qt::black,1));    painter.drawText(20,10,"QMatrix");}

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

Member Function Documentation

QMatrix::QMatrix()

Constructs an identity matrix.

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

See alsoreset().

QMatrix::QMatrix(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().

QMatrix::QMatrix(constQMatrix & matrix)

Constructs a matrix that is a copy of the givenmatrix.

qreal QMatrix::m11() const

Returns the horizontal scaling factor.

See alsoscale() andBasic Matrix Operations.

qreal QMatrix::m12() const

Returns the vertical shearing factor.

See alsoshear() andBasic Matrix Operations.

qreal QMatrix::m21() const

Returns the horizontal shearing factor.

See alsoshear() andBasic Matrix Operations.

qreal QMatrix::m22() const

Returns the vertical scaling factor.

See alsoscale() andBasic Matrix Operations.

qreal QMatrix::determinant() const

Returns the matrix's determinant.

This function was introduced in Qt 4.6.

qreal QMatrix::dx() const

Returns the horizontal translation factor.

See alsotranslate() andBasic Matrix Operations.

qreal QMatrix::dy() const

Returns the vertical translation factor.

See alsotranslate() andBasic Matrix Operations.

QMatrix QMatrix::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 QMatrix::isIdentity() const

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

See alsoreset().

bool QMatrix::isInvertible() const

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

See alsoinverted().

void QMatrix::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+ dy

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

See alsoBasic Matrix Operations.

void QMatrix::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.

QPointF QMatrix::map(constQPointF & point) const

This is an overloaded function.

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

QPoint QMatrix::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.

QLineF QMatrix::map(constQLineF & line) const

This is an overloaded function.

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

QLine QMatrix::map(constQLine & 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 QMatrix::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 QMatrix::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 QMatrix::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 QMatrix::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.

QRectF QMatrix::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+ dy

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 QMatrix::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 QMatrix::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+ dy

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.

void QMatrix::reset()

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

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

QMatrix & QMatrix::rotate(qreal degrees)

Rotates the coordinate system the givendegrees counterclockwise.

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

Returns a reference to the matrix.

See alsosetMatrix().

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

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

See alsosetMatrix().

void QMatrix::setMatrix(qreal m11,qreal m12,qreal m21,qreal m22,qreal dx,qreal dy)

Sets the matrix elements to the specified values,m11,m12,m21,m22,dx anddy.

Note that this function replaces the previous values.QMatrix provide thetranslate(),rotate(),scale() andshear() convenience functions to manipulate the various matrix elements based on the currently defined coordinate system.

See alsoQMatrix().

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

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

See alsosetMatrix().

QMatrix & QMatrix::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().

QMatrix::operator QVariant() const

Returns the matrix as aQVariant.

This function was introduced in Qt 4.2.

bool QMatrix::operator!=(constQMatrix & matrix) const

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

QMatrix QMatrix::operator*(constQMatrix & 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.

QMatrix & QMatrix::operator*=(constQMatrix & matrix)

This is an overloaded function.

Returns the result of multiplying this matrix by the givenmatrix.

QMatrix & QMatrix::operator=(constQMatrix & matrix)

Assigns the givenmatrix's values to this matrix.

bool QMatrix::operator==(constQMatrix & matrix) const

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

Related Non-Members

boolqFuzzyCompare(constQMatrix & m1, constQMatrix & m2)

TheqFuzzyCompare function is for comparing two matrices using a fuzziness factor.

Returns true ifm1 andm2 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, constQMatrix & matrix)

This is the same asmatrix.map(point).

See alsoQMatrix::map().

QPointFoperator*(constQPointF & point, constQMatrix & matrix)

Same asmatrix.map(point).

See alsoQMatrix::map().

QLineFoperator*(constQLineF & line, constQMatrix & matrix)

This is the same asmatrix.map(line).

See alsoQMatrix::map().

QLineoperator*(constQLine & line, constQMatrix & matrix)

This is the same asmatrix.map(line).

See alsoQMatrix::map().

QPolygonoperator*(constQPolygon & polygon, constQMatrix & matrix)

This is the same asmatrix.map(polygon).

See alsoQMatrix::map().

QPolygonFoperator*(constQPolygonF & polygon, constQMatrix & matrix)

This is the same asmatrix.map(polygon).

See alsoQMatrix::map().

QRegionoperator*(constQRegion & region, constQMatrix & matrix)

This is the same asmatrix.map(region).

See alsoQMatrix::map().

QPainterPathoperator*(constQPainterPath & path, constQMatrix & matrix)

This is the same asmatrix.map(path).

See alsoQMatrix::map().

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

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

See alsoSerializing Qt Data Types.

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

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

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