2017-06-22 54 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"}); 
    } 
} 
相關問題