如何获得 QQuickItem 的变换矩阵? [英] How can I get transform matrix for QQuickItem?

查看:35
本文介绍了如何获得 QQuickItem 的变换矩阵?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 QGraphicsItem 工作了很长时间,它有 transform() 功能.现在我不会用 QQuickItem 做同样的事情,但不幸的是它错过了 transform().所以我的问题 - 如何获得 QQuickItem 的变换矩阵?

解决方案

实际上 QQuickItem 提供了 transform() 方法,但是它返回分配的所有转换的列表到给定的项目.这是因为可以将多个转换分配给单个 Item.QQuickItem::transform 的返回类型是 QQmlListProperty — 它是 QML list 类型的包装器(参见 项目文档).它可以迭代,产生 QQuickTransform * 元素.QQuickTransform 是转换的基类,它提供了一个虚方法 applyTo 接受一个 QMatrix4x4 * 参数并对其应用转换.

QML 允许实例化多个 QQuickTransform 子类(用于平移、旋转和缩放),并且允许用户定义自定义转换(例如用于倾斜).

要获得您需要的单个变换矩阵,您必须从单位矩阵开始,然后依次应用给定 QQuickItem 的所有变换.

<前>QMatrix4x4 transformOfItem(QQuickItem *item){QQmlListProperty 转换 = item->transform();const int count =transforms.count(&transformations);//准备结果结构,默认初始化为单位矩阵QMatrix4x4 变换矩阵;//从项目依次应用所有变换for(int i = 0; i applyTo(&transformMatrix);}返回变换矩阵;}

请注意,该函数返回的转换矩阵为 QMatrix4x4 — 它比基于 3x3 转换矩阵的旧 QTransform 多,因此无法进行无损转换.如果需要,您可以使用 QMatrix4x4::toAffine 来获取 QMatrix (3x3) 并使用它来创建 QTransform 对象.但是,如果您的 QQuickItem 转换包含非亲和元素,它们将会丢失.

编辑

还有一件事需要注意:我发布的方法仅适用于通过分配给 transform 属性定义的转换.它不检查 scalerotation 属性.如果您使用它们,您应该使用适当的 QQuickItem 方法检查它们的值并调整返回的矩阵以包含这两个额外的转换.

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?

解决方案

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.

这篇关于如何获得 QQuickItem 的变换矩阵?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆