【问题标题】:Offset rotation for QML ItemQML 项目的偏移旋转
【发布时间】:2016-12-13 21:12:00
【问题描述】:

在 QML 应用程序中,我有一个项目在屏幕上移动(不旋转)。我想显示一个围绕该项目旋转的指示器,指向远离屏幕中心,距离项目中心固定距离。

以下简化的 QML 应用程序通过使指示器成为项目的子项并将其转换到所需位置来实现此目标。但是,当我尝试旋转指示器(注释掉的代码)时,我找不到任何适用于 origin.x.y 的值。感觉就像 QML 场景图计算 X/Y 定位的方式与我所经历的不同。

import QtQuick 2.7
import QtQuick.Window 2.2

Window {
    id: win
    visible:true; width:600; height:300
    property real padding: 50
    property real angle: 0
    property real _rads: angle * Math.PI/180

    Timer {
      interval:50; running:true; repeat:true
      onTriggered:win.angle = (new Date/50) % 360
    }

    Rectangle {
        id:object; color:'blue'
        width:50; height:width
        property real xOffset: Math.cos(_rads)
        property real yOffset: Math.sin(_rads)
        x: win.width/2  + xOffset * (win.width/2 - padding*2)
        y: win.height/2 + yOffset * (win.height/2 - padding*2)

        Rectangle {
            id:indicator; color:'red'
            property real centerOffset: 40
            width:10; height:width*2
            x: object.width/2  + object.xOffset * centerOffset - width/2
            y: object.height/2 + object.yOffset * centerOffset - height/2
//          transform: Rotation { origin.x:0; origin.y:0; angle:win.angle }
        }
    }
}

我已尝试使指示器不是该项目的子项。我尝试在 transform 堆栈中使用 Translate 而不是 X/Y 位置。所有这些都会导致有趣但不正确的旋转。

我怎样才能简单地围绕它自己的中心旋转指示器,或者以其他方式实现我的目标?

【问题讨论】:

    标签: qt qml qtquick2


    【解决方案1】:

    你可以把它想象成一个时钟,然后为自己打造一个时钟指针。

    import QtQuick 2.7
    import QtQuick.Window 2.2
    
    Window {
        id: win
        visible:true; width:600; height:300
        property real padding: 50
        property real angle: 0
        property real _rads: angle * Math.PI/180
    
        Timer {
            interval:50; running:true; repeat:true
            onTriggered:win.angle = (new Date/50) % 360
        }
    
        Rectangle {
            id:object; color:'blue'
            width:50; height:width
            property real xOffset: Math.cos(_rads)
            property real yOffset: Math.sin(_rads)
            x: win.width/2  + xOffset * (win.width/2 - padding*2)
            y: win.height/2 + yOffset * (win.height/2 - padding*2)
    
    
            Text {
                width: 250
                height: 250
                x: -100
                y: -100
                text: '▲'
                color: 'red'
                font.pixelSize: 20
                horizontalAlignment: Qt.AlignHCenter
                verticalAlignment: Qt.AlignTop
    
                transform: Rotation {
                    angle: win.angle + 90
                    origin.x: 125
                    origin.y: 125
                }
            }
    
            Text {
                x: 15
                y: -125
                width: 20
                height: 20
    
                text: '▲'
                color: 'red'
                font.pixelSize: 20
                horizontalAlignment: Qt.AlignHCenter
                verticalAlignment: Qt.AlignVCenter
    
                transform: Rotation {
                    angle: win.angle + 90
                    origin.x: 10
                    origin.y: 150
                }
            }
    
    
            Rectangle {
                id: clockhand
                width: 1
                height: 100
                color: 'black'
                anchors {
                    centerIn: parent
                }
    
                rotation: win.angle + 90
    
                Text {
                    text: '▲'
                    color: 'red'
                    anchors {
                        horizontalCenter: parent.horizontalCenter
                        bottom: parent.top
                        bottomMargin: -5
                    }
                    font.pixelSize: 20
                }
            }
        }
    }
    

    只需将 Clockhand 变成 Item 并移除颜色,使其不可见。

    【讨论】:

    • 哎呀!我怎么一直错过Item.rotation 属性。谢谢! :)
    • 为了完整起见,我将再添加两个箭头,它们使用transform: Rotation...
    • 您的问题是,您更改了定位,同时保持旋转原点不变。你需要在旋转中补偿这个运动,或者放弃重新定位,只通过旋转重新定位......如果你能跟我...
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-12-17
    • 2011-12-20
    • 2014-01-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多