qt - 如何获得 QQuickItem 的变换矩阵?

标签 qt qml qtquick2

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

最佳答案

实际上是QQuickItem提供transform()方法,但是它返回分配给给定项目的所有转换的列表。这是因为多个转换可以分配给单个 ItemQQuickItem::transform的返回类型是 QQmlListProperty<QQuickTransform> — 它是 QML list<Transform> 的包装器类型(参见 documentation for Item )。它可以被迭代,产生 QQuickTransform *元素。 QQuickTransform是提供虚拟方法的转换的基类 applyTo采取QMatrix4x4 *论证并对其应用变换。

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

要获得所需的单个变换矩阵,您必须从单位矩阵开始并顺序应用给定 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;
}

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

编辑

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

关于qt - 如何获得 QQuickItem 的变换矩阵?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28535528/

相关文章:

android - Android 上的 MouseArea 虚拟双击

c++ - 如何在QT中的QToolButton下方设置文本而不是在图标下方

C++ Qt : is it possible to create a sort of template slot?

c++ - 序列化多态指针的QVector

qt - QML MouseArea onEntered hideEnabled 不起作用

qt - "Bind"两个 QML CheckBox 在一起,确保它们的状态始终相同

qt - 当MouseArea被另一个MouseArea覆盖时,如何在qml中更改光标形状

qt - 没有 QtCreator,exe 文件无法运行

QtCreator 新项目 Qt Quick Controls vs. Qt Quick

c++ - 如何使用 QStringList 过滤 QSortFilterProxyModel?