Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get transform matrix for QQuickItem?

Tags:

qt

qml

qtquick2

I worked for a long time with QGraphicsItem and it has transform() function. Now I wont to do same thing with QQuickItem but unfortunately it misses transform(). So my question - how can I get transform matrix for QQuickItem?

like image 580
folibis Avatar asked Feb 16 '15 06:02

folibis


1 Answers

Actually the QQuickItem provides the transform() method, however it returns the list of all transformations assigned to given item. It is because multiple transformations can be assigned to a single Item. The return type of QQuickItem::transform is QQmlListProperty<QQuickTransform> — it is a wrapper to QML list<Transform> type (see documentation for Item). It can be iterated over, yielding QQuickTransform * elements. QQuickTransform is a base class for a transformation that provides a virtual method applyTo taking a QMatrix4x4 * argument and applying the transformation upon it.

The QML allows instantiating several QQuickTransform subclasses (for translation, rotation and scale) and user is allowed to defined custom transformations (eg. for skew).

To obtain a single transformation matrix you need, you have to start with identity matrix and sequentially apply all the transformations of given QQuickItem.

QMatrix4x4 transformOfItem(QQuickItem *item)
{
    QQmlListProperty transformations = item->transform();

    const int count = transformations.count(&transformations);

    // Prepare result structure, it will be default-initialized to be an identity matrix
    QMatrix4x4 transformMatrix;

    // Apply sequentially all transformation from the item
    for(int i = 0; i applyTo(&transformMatrix);
    }

    return transformMatrix;
}

Note that the function returns a tranformation matrix as QMatrix4x4 — it is more than old QTransform that was based on 3x3 transformation matrix, so it cannot be converted without loss. If you want, you may use QMatrix4x4::toAffine to get the QMatrix (3x3) and use it to create QTransform object. However, if your QQuickItem transformations contain non-affinic elements, they will be lost.

Edit

There's one more thing to note: the method I posted works only for transformations defined by assigning to transform property. It does not check for scale and rotation properties. If you use them, you should check their values with appropriate QQuickItem methods and adjust returned matrix to include these two additional tranformations.

like image 51
Michał W. Urbańczyk Avatar answered Nov 15 '22 07:11

Michał W. Urbańczyk