Qt QML:如何使用状态将对象从先前设置的位置移动

Qt QML:如何使用状态将对象从先前设置的位置移动,qt,qml,Qt,Qml,我有一个对象,我想从它以前设置的位置移动,每次特定的状态设置。我尝试创建一个名为xPos的单独属性来绕过绑定循环错误,该错误是在设置状态后由对象的新位置x设置的,然后进入一个默认状态,以便能够再次切换回该特定状态,因为调用同一状态没有任何作用,但似乎不起作用 以下是我的代码片段: property int xPos: 0 states: [ St

我有一个对象,我想从它以前设置的位置移动,每次特定的状态设置。我尝试创建一个名为xPos的单独属性来绕过绑定循环错误,该错误是在设置状态后由对象的新位置x设置的,然后进入一个默认状态,以便能够再次切换回该特定状态,因为调用同一状态没有任何作用,但似乎不起作用

以下是我的代码片段:

property int xPos: 0

states: [                                                                       
    State {
        name: "nextStep"
        PropertyChanges {
            target: progressBar_Id
            x: -1*(progressBar_Id.step.width) + xPos
        }
    },
    State {
        name: "previousStep"
        PropertyChanges {
            target: progressBar_Id
            x: progressBar_Id.step.width + xPos
        }
    },
    State {
        name: "default"
        PropertyChanges {
            target: progressBar_Id
            x: xPos
        }
    }
]

transitions: [
    Transition {
        from: "*"
        to: "nextStep"
        NumberAnimation {properties: "x"; easing.type: Easing.Linear; duration: 1000}
        onRunningChanged: {
            if(!running) {
                xPos = progressBar_Id.x;
                console.info("xPos = " + xPos);
                state = "default";
            }
        }
    },
    Transition {
        from: "*"
        to: "previousStep"
        NumberAnimation {properties: "x"; easing.type: Easing.Linear; duration: 1000}
        onRunningChanged: {
            if(!running) {
                xPos = progressBar_Id.x;
                console.info("xPos = " + xPos);
                state = "default";
            }
        }
    }
]

xPos似乎是第一次从控制台输出获得设置,但从未将新的xCoord应用于对象。

明确指定要设置其状态的项的id,例如:

idOfComponent.state = "default"

所有QML项和派生项都有一个名为
state
的属性,因此代码需要更加具体

实际上提出了一个更好的选择,使用ListView

ListView {
    id: listView_Id
    width: contentWidth
    height: bubbleSize
    anchors.centerIn: parent
    layoutDirection: Qt.RightToLeft
    orientation: ListView.Horizontal
    spacing: 0

    delegate: Item {
        width: bubbleSize
        height: width

        ProgressBubble {
            stateText: stateNumber
            currentState: state
        }
    }
    model: ListModel { id: progressModel_Id }
}
另一个qml文件:

progressModel.insert(0, {stateNumber: number++, state: "current"});
但是我遇到了另一个问题,在委托被添加到ListView之后,是否仍然需要更改委托中的状态


我已经尝试过progressModel.set和progressModel.setProperty,但似乎不起作用。

状态是qml类型的属性,因此当您为“ProgressBubble”将“状态”指定给“currentState”时,它将取当前存在“ProgressBubble”的状态的值

将“state”重命名为类似“presentState”的其他名称,然后重试

此外,ListView模型的id(progressModel_id)和用于在不同文件中插入模型元素的id(progressModel)是不同的,两者必须引用相同的id

在这些更改之后,可以尝试设置模型的属性。示例代码段:

import QtQuick 2.0

Rectangle {
    id: root
    width: 360
    height: 360

    property int number: 1

    ListView {
        id: listView_Id
        width: 100 //contentWidth // this creates property binding issue 
        height: 100 // bubbleSize // I was not sure about this value
        anchors.centerIn: parent
        layoutDirection: Qt.RightToLeft
        orientation: ListView.Horizontal
        spacing: 0
        delegate: Item {
            width: 100 // bubbleSize // I was not sure about this value
            height: width
            ProgressBubble {
                state: "default"
                stateText: stateNumber
                currentState: presentState

                MouseArea {
                    anchors.fill: parent
                    onClicked: {
                        progressModel_Id.set(index,{stateNumber: stateNumber, presentState: "not current"})
                    }
                }
            }
        }
        model: ListModel { id: progressModel_Id }
    }
    Component.onCompleted:
    {
        progressModel_Id.insert(0, {stateNumber: number++, presentState: "current"});
    }
}