Qt 动态更改QML元素的转换
我花了相当长的时间才弄清楚旋转QML对象时遇到的问题,所以我决定与大家分享 问题是: 如何动态更新QML元素的旋转(如图像)。我知道我可以设置,例如,使用变换设置旋转(在MapQuickItem示例中):Qt 动态更改QML元素的转换,qt,qml,Qt,Qml,我花了相当长的时间才弄清楚旋转QML对象时遇到的问题,所以我决定与大家分享 问题是: 如何动态更新QML元素的旋转(如图像)。我知道我可以设置,例如,使用变换设置旋转(在MapQuickItem示例中): 但是,如何动态更新角度(或变换的其他部分) 关键信息是transform实际上是一个转换列表 因此,上述问题的一个可能解决方案是这样一个函数(例如,改变旋转): 关键信息是transform实际上是一个转换列表 因此,上述问题的一个可能解决方案是这样一个函数(例如,改变旋转): 更简洁的方法是
但是,如何动态更新角度(或变换的其他部分) 关键信息是
transform
实际上是一个转换列表
因此,上述问题的一个可能解决方案是这样一个函数(例如,改变旋转):
关键信息是
transform
实际上是一个转换列表
因此,上述问题的一个可能解决方案是这样一个函数(例如,改变旋转):
更简洁的方法是使用属性或别名:
MapQuickItem
{
sourceItem: Image
{
id: image
property alias rotationAngle: rotation.angle
anchors.centerIn: parent
source: "image.png"
sourceSize.width: Screen.devicePixelRatio * 256
sourceSize.height: Screen.devicePixelRatio * 256
transform: Rotation {
id: rotation
origin { x: image.sourceSize.width/2;
y: image.sourceSize.height/2;
z: 0}
angle: 0
}
}
}
与:
更简洁的方法是使用属性或别名:
MapQuickItem
{
sourceItem: Image
{
id: image
property alias rotationAngle: rotation.angle
anchors.centerIn: parent
source: "image.png"
sourceSize.width: Screen.devicePixelRatio * 256
sourceSize.height: Screen.devicePixelRatio * 256
transform: Rotation {
id: rotation
origin { x: image.sourceSize.width/2;
y: image.sourceSize.height/2;
z: 0}
angle: 0
}
}
}
与:
MapQuickItem
{
sourceItem: Image
{
id: image
property alias rotationAngle: rotation.angle
anchors.centerIn: parent
source: "image.png"
sourceSize.width: Screen.devicePixelRatio * 256
sourceSize.height: Screen.devicePixelRatio * 256
transform: Rotation {
id: rotation
origin { x: image.sourceSize.width/2;
y: image.sourceSize.height/2;
z: 0}
angle: 0
}
}
}
function updatePositionAndRotation(angleDeg)
{
image.rotationAngle = angleDeg
}