代码之家  ›  专栏  ›  技术社区  ›  adviner

QML旋转图像

  •  6
  • adviner  · 技术社区  · 7 年前

    当我在图像控件中显示图像时,我正在尝试旋转图像。当前图像显示如下:

    enter image description here

    我想把它向右旋转90度。 我当前的代码如下所示:'

    Image {
        id: imagePhoto
        anchors.fill: parent
        width: parent.width
        height: parent.height
    
    
        transform: Rotation{                       
            id: rotateImagePhoto
            angle: 0
            origin.x: 0
            origin.y: 0
    
        }
    }
    

    所以我试着玩角度:

    transform: Rotation{                       
        id: rotateImagePhoto
        angle: 90
        origin.x: 700
        origin.y: 700
    
    }
    

    正确显示:

    enter image description here

    我不明白的是为什么我必须把这些值放在x/y上。 我已经看过以下内容 site ,但我不明白。

    1 回复  |  直到 6 年前
        1
  •  11
  •   eyllanesc Yonghwan Shin    7 年前

    缩放和旋转变换以一个名为transformOrigin的点作为参考,在您的情况下,它是图像旋转的点,因此您可能正在建立图像的中心,如果更改位置,旋转将不同。

    在您的情况下,任何尺寸的一般解决方案是:

    transform: Rotation{
        id: rotateImagePhoto
        angle: 90
        origin.x: imagePhoto.width/2
        origin.y: imagePhoto.height/2
    }
    

    或更好地使用 rotation

    Image {
        id: imagePhoto
        anchors.fill: parent
        width: parent.width
        height: parent.height
        transformOrigin: Item.Center
        rotation: 90
    }
    

    该点是一个固定点,也就是说它在旋转之前将保持不变,例如,让我们在左上角建立该点并旋转30度:

    transform: Rotation{
        id: rotateImagePhoto
        angle: 30
        origin.x: 0
        origin.y: 0
    }
    

    enter image description here

    请注意 topLeft 点未移动,是旋转的中心。

    总之,在旋转的情况下,原点是旋转的中心。