Viadeo Twitter Google Bookmarks ! Facebook Digg del.icio.us MySpace Yahoo MyWeb Blinklist Netvouz Reddit Simpy StumbleUpon Bookmarks Windows Live Favorites 
Logo Documentation Qt ·  Page d'accueil  ·  Toutes les classes  ·  Classes principales  ·  Annotées  ·  Classes groupées  ·  Modules  ·  Fonctions  · 

QMatrix Class Reference
[QtGui module]

The QMatrix class specifies 2D transformations of a coordinate system. More...

#include <QMatrix>

Public Functions

  • QMatrix ( qreal m11, qreal m12, qreal m21, qreal m22, qreal dx, qreal dy )
  • QMatrix ( const QMatrix & matrix )
  • qreal m11 () const
  • qreal m12 () const
  • qreal m21 () const
  • qreal m22 () const
  • qreal det () const
  • qreal dx () const
  • qreal dy () const
  • QMatrix inverted ( bool * invertible = 0 ) const
  • bool isIdentity () const
  • bool isInvertible () const
  • void map ( qreal x, qreal y, qreal * tx, qreal * ty ) const
  • void map ( int x, int y, int * tx, int * ty ) const
  • QPoint map ( const QPoint & p ) const
  • QPointF map ( const QPointF & point ) const
  • QLineF map ( const QLineF & line ) const
  • QLine map ( const QLine & line ) const
  • QPolygon map ( const QPolygon & a ) const
  • QPolygonF map ( const QPolygonF & a ) const
  • QRegion map ( const QRegion & r ) const
  • QPainterPath map ( const QPainterPath & path ) const
  • QRect mapRect ( const QRect & rect ) const
  • QRectF mapRect ( const QRectF & rect ) const
  • QPolygon mapToPolygon ( const QRect & rect ) const
  • void reset ()
  • QMatrix & rotate ( qreal a )
  • QMatrix & scale ( qreal sx, qreal sy )
  • void setMatrix ( qreal m11, qreal m12, qreal m21, qreal m22, qreal dx, qreal dy )
  • QMatrix & shear ( qreal sh, qreal sv )
  • QMatrix & translate ( qreal dx, qreal dy )
  • bool operator!= ( const QMatrix & m ) const
  • QMatrix operator* ( const QMatrix & m ) const
  • QMatrix & operator*= ( const QMatrix & m )
  • QMatrix & operator= ( const QMatrix & matrix )
  • bool operator== ( const QMatrix & m ) const

Related Non-Members

  • QPoint operator* ( const QPoint & p, const QMatrix & m )
  • QPointF operator* ( const QPointF & p, const QMatrix & m )
  • QPolygon operator* ( const QPolygon & a, const QMatrix & m )
  • QRegion operator* ( const QRegion & r, const QMatrix & m )
  • QDataStream & operator<< ( QDataStream & s, const QMatrix & m )
  • QDataStream & operator>> ( QDataStream & s, QMatrix & m )

Detailed Description

The QMatrix class specifies 2D transformations of a coordinate system.

The standard coordinate system of a paint device has the origin located at the top-left position. x values increase to the right; y values increase downward.

This coordinate system is the default for the QPainter, which renders graphics in a paint device. A user-defined coordinate system can be specified by setting a QMatrix for the painter.

Example:

    void MyWidget::paintEvent(QPaintEvent *)
    {
        QPainter p;                   // our painter
        QMatrix m;                    // our transformation matrix
        m.rotate(22.5);               // rotated coordinate system
        p.begin(this);                // start painting
        p.setMatrix(m);               // use rotated coordinate system
        p.drawText(30,20, "detator"); // draw rotated text at 30,20
        p.end();                      // painting done
    }

A matrix specifies how to translate, scale, shear or rotate the graphics; the actual transformation is performed by the drawing routines in QPainter and by QPixmap::xForm().

The QMatrix class contains a 3x3 matrix of the form:

m11m120
m21m220
dxdy1

A matrix transforms a point in the plane to another point:

    x' = m11*x + m21*y + dx
    y' = 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 the inverted matrix.

The elements dx and dy specify horizontal and vertical translation. The elements m11 and m22 specify horizontal and vertical scaling. The elements m12 and m21 specify horizontal and vertical shearing.

The identity matrix has m11 and m22 set to 1; all others are set to 0. This matrix maps a point to itself.

Translation is the simplest transformation. Setting dx and dy will move the coordinate system dx units along the X axis and dy units along the Y axis.

Scaling can be done by setting m11 and m22. For example, setting m11 to 2 and m22 to 1.5 will double the height and increase the width by 50%.

Shearing is controlled by m12 and m21. 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. The QMatrix also has a function that sets rotation directly.

QMatrix lets you combine transformations like this:

            QMatrix m;            // identity matrix
            m.translate(10, -20); // first translate (10,-20)
            m.rotate(25);         // then rotate 25 degrees
            m.scale(1.2, 0.7);    // finally scale it

Here's the same example using basic matrix operations:

            double a    = pi/180 * 25;         // convert 25 to radians
            double sina = sin(a);
            double cosa = cos(a);
            QMatrix m1(1, 0, 0, 1, 10, -20);   // translation matrix
            QMatrix m2(cosa, sina,             // rotation matrix
                        -sina, cosa, 0, 0);
            QMatrix m3(1.2, 0, 0, 0.7, 0, 0);  // scaling matrix
            QMatrix m;
            m = m3 * m2 * m1;                  // combine all transformations

The matrix can also be transformed using the map() functions, and transformed points, rectangles, etc., can be obtained using map(), mapRect(), mapToRegion(), and mapToPolygon() functions.

QPainter has functions to translate, scale, shear and rotate the coordinate system without using a QMatrix. Although these functions are very convenient, it can be more efficient to build a QMatrix and call QPainter::setMatrix() if you want to perform more than a single transform operation.

See also QPainter::setMatrix() and QPixmap::xForm().


Member Function Documentation

QMatrix::QMatrix ()

Constructs an identity matrix. All elements are set to zero except m11 and m22 (scaling), which are set to 1.

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 and dy.

QMatrix::QMatrix ( const QMatrix & matrix )

Constructs a matrix with the elements from the matrix matrix

qreal QMatrix::m11 () const

Returns the X scaling factor.

qreal QMatrix::m12 () const

Returns the vertical shearing factor.

qreal QMatrix::m21 () const

Returns the horizontal shearing factor.

qreal QMatrix::m22 () const

Returns the Y scaling factor.

qreal QMatrix::det () const

Returns the matrix's determinant.

qreal QMatrix::dx () const

Returns the horizontal translation.

qreal QMatrix::dy () const

Returns the vertical translation.

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

Returns the inverted matrix.

If the matrix is singular (not invertible), the identity matrix is returned.

If invertible is not 0: the value of *invertible is set to true if the matrix is invertible; otherwise *invertible is set to false.

See also isInvertible().

bool QMatrix::isIdentity () const

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

See also reset().

bool QMatrix::isInvertible () const

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

See also inverted().

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

Transforms (x, y) to (*tx, *ty) using the following formulas:

    *tx = m11*x + m21*y + dx
    *ty = m22*y + m12*x + dy

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

This is an overloaded member function, provided for convenience. It behaves essentially like the above function.

Transforms (x, y) to (*tx, *ty) using the formulas:

    *tx = m11*x + m21*y + dx  (rounded to the nearest integer)
    *ty = m22*y + m12*x + dy  (rounded to the nearest integer)

QPoint QMatrix::map ( const QPoint & p ) const

This is an overloaded member function, provided for convenience. It behaves essentially like the above function.

Transforms p to using these formulas:

    retx = m11*px + m21*py + dx  (rounded to the nearest integer)
    rety = m22*py + m12*px + dy  (rounded to the nearest integer)

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

This is an overloaded member function, provided for convenience. It behaves essentially like the above function.

Transforms point using these formulas:

    retx = m11*px + m21*py + dx
    rety = m22*py + m12*px + dy

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

This is an overloaded member function, provided for convenience. It behaves essentially like the above function.

Transforms both ends of line using these formulas:

    retx = m11 * px + m21 * py + dx
    rety = m22 * py + m12 * px + dy

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

This is an overloaded member function, provided for convenience. It behaves essentially like the above function.

Transforms both ends of line using these formulas:

    retx = m11 * px + m21 * py + dx (rounded to the nearest integer)
    rety = m22 * py + m12 * px + dy (rounded to the nearest integer)

QPolygon QMatrix::map ( const QPolygon & a ) const

This is an overloaded member function, provided for convenience. It behaves essentially like the above function.

Returns the point array a transformed by calling map for each point.

QPolygonF QMatrix::map ( const QPolygonF & a ) const

This is an overloaded member function, provided for convenience. It behaves essentially like the above function.

Returns the point array a transformed by calling map for each point.

QRegion QMatrix::map ( const QRegion & r ) const

This is an overloaded member function, provided for convenience. It behaves essentially like the above function.

Transforms the region r.

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

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

This is an overloaded member function, provided for convenience. It behaves essentially like the above function.

Transforms the painter path path.

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

Returns the transformed rectangle rect rounded to the neares integer.

The bounding rectangle is returned if rotation or shearing has been specified.

If you need to know the exact region rect maps to use operator*().

See also operator*().

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

This is an overloaded member function, provided for convenience. It behaves essentially like the above function.

Returns the transformed rectangle rect.

The bounding rectangle is returned if rotation or shearing has been specified.

If you need to know the exact region rect maps to use operator*().

See also operator*().

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

Returns the transformed rectangle rect as a polygon.

Polygons and rectangles behave slightly differently when transformed (due to integer rounding), so matrix.map(QPolygon(rect)) is not always the same as matrix.mapToPolygon(rect).

void QMatrix::reset ()

Resets the matrix to an identity matrix.

All elements are set to zero, except m11 and m22 (scaling) which are set to 1.

See also isIdentity().

QMatrix & QMatrix::rotate ( qreal a )

Rotates the coordinate system a degrees counterclockwise.

Returns a reference to the matrix.

See also translate(), scale(), and shear().

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

Scales the coordinate system unit by sx horizontally and sy vertically.

Returns a reference to the matrix.

See also translate(), shear(), and rotate().

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 and dy.

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

Shears the coordinate system by sh horizontally and sv vertically.

Returns a reference to the matrix.

See also translate(), scale(), and rotate().

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

Moves the coordinate system dx along the x axis and dy along the y axis.

Returns a reference to the matrix.

See also scale(), shear(), and rotate().

bool QMatrix::operator!= ( const QMatrix & m ) const

Returns true if this matrix is not equal to m; otherwise returns false.

QMatrix QMatrix::operator* ( const QMatrix & m ) const

Returns the product of this * m.

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

QMatrix & QMatrix::operator*= ( const QMatrix & m )

Returns the result of multiplying this matrix by matrix m.

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

Assigns matrix matrix's values to this matrix.

bool QMatrix::operator== ( const QMatrix & m ) const

Returns true if this matrix is equal to m; otherwise returns false.


Related Non-Members

QPoint operator* ( const QPoint & p, const QMatrix & m )

This is an overloaded member function, provided for convenience. It behaves essentially like the above function.

This is the same as m.mapRect(p).

QPointF operator* ( const QPointF & p, const QMatrix & m )

This is an overloaded member function, provided for convenience. It behaves essentially like the above function.

Same as m.map(p).

See also QMatrix::map().

QPolygon operator* ( const QPolygon & a, const QMatrix & m )

This is an overloaded member function, provided for convenience. It behaves essentially like the above function.

This is the same as m.mapRect(a).

See also QMatrix::mapRect().

QRegion operator* ( const QRegion & r, const QMatrix & m )

This is an overloaded member function, provided for convenience. It behaves essentially like the above function.

This is the same as m.mapRect(r).

See also QMatrix::mapRect().

QDataStream & operator<< ( QDataStream & s, const QMatrix & m )

This is an overloaded member function, provided for convenience. It behaves essentially like the above function.

Writes the matrix m to the stream s and returns a reference to the stream.

See also Format of the QDataStream operators.

QDataStream & operator>> ( QDataStream & s, QMatrix & m )

This is an overloaded member function, provided for convenience. It behaves essentially like the above function.

Reads the matrix m from the stream s and returns a reference to the stream.

See also Format of the QDataStream operators.

Publicité

Best Of

Actualités les plus lues

Semaine
Mois
Année
  1. « Quelque chose ne va vraiment pas avec les développeurs "modernes" », un développeur à "l'ancienne" critique la multiplication des bibliothèques 94
  2. Apercevoir la troisième dimension ou l'utilisation multithreadée d'OpenGL dans Qt, un article des Qt Quarterly traduit par Guillaume Belz 0
  3. Pourquoi les programmeurs sont-ils moins payés que les gestionnaires de programmes ? Manquent-ils de pouvoir de négociation ? 44
  4. Les développeurs ignorent-ils trop les failles découvertes dans leur code ? Prenez-vous en compte les remarques des autres ? 17
  5. Quelles nouveautés de C++11 Visual C++ doit-il rapidement intégrer ? Donnez-nous votre avis 10
  6. Les développeurs détestent-ils les antivirus ? Un programmeur manifeste sa haine envers ces solutions de sécurité 6
  7. Qt Commercial : Digia organise un webinar gratuit le 27 mars sur la conception d'interfaces utilisateur et d'applications avec le framework 0
Page suivante

Le blog Digia au hasard

Logo

Une nouvelle ère d'IHM 3D pour les automobiles

Le blog Digia est l'endroit privilégié pour la communication sur l'édition commerciale de Qt, où des réponses publiques sont apportées aux questions les plus posées au support. Lire l'article.

Communauté

Ressources

Liens utiles

Contact

  • Vous souhaitez rejoindre la rédaction ou proposer un tutoriel, une traduction, une question... ? Postez dans le forum Contribuez ou contactez-nous par MP ou par email (voir en bas de page).

Qt dans le magazine

Cette page est une traduction d'une page de la documentation de Qt, écrite par Nokia Corporation and/or its subsidiary(-ies). Les éventuels problèmes résultant d'une mauvaise traduction ne sont pas imputables à Nokia. Qt 4.0
Copyright © 2012 Developpez LLC. Tous droits réservés Developpez LLC. Aucune reproduction, même partielle, ne peut être faite de ce site et de l'ensemble de son contenu : textes, documents et images sans l'autorisation expresse de Developpez LLC. Sinon, vous encourez selon la loi jusqu'à 3 ans de prison et jusqu'à 300 000 E de dommages et intérêts. Cette page est déposée à la SACD.
Vous avez déniché une erreur ? Un bug ? Une redirection cassée ? Ou tout autre problème, quel qu'il soit ? Ou bien vous désirez participer à ce projet de traduction ? N'hésitez pas à nous contacter ou par MP !
 
 
 
 
Partenaires

Hébergement Web