2017-06-22 51 views
0

我有一个对象,我希望每次设置该特定状态时都从之前设置的位置移动。我已经尝试制作一个名为xPos的独立属性来解决绑定循环错误,该错误是在状态设置后由对象的x的新位置设置的,然后进入默认状态,以便能够再次切换回该特定状态因为调用同一个状态什么都不做,但它似乎不起作用。QML:如何使用状态从上一个设置位置移动对象

这里是我的代码片段:

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对象。

回答

0

实际上想出了一个更好的选择使用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,但似乎没有工作。

0

明确指定的项目上,你要设置的状态ID,e.g:

idOfComponent.state = "default" 

所有QML项目和衍生品有一个名为state属性,因此代码需要更加具体。

0

状态是QML类型的属性,所以当你要分配“状态”为“currentState”为“ProgressBubble”,其采取的状态其中“ProgressBubble”是当前存在的价值。

将“状态”重命名为“presentState”之类的其他内容,然后尝试。的ListView模型的

此外ID(progressModel_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"}); 
    } 
} 
相关问题