2012-05-03 3 views
2

ListModel 및 ListView에 약간의 문제가 있습니다. 특히 ListModel의 이동 기능과 관련하여 특히 그렇습니다. 이미지 "내용/사진/화살표 up.png"을 클릭하면 그래서QML ListView 및 ListModel 인덱스

Item {  
    property alias nameText: nameTextBox.text 
     id: delegateItem 
     width: listView.width; height: 55 
     clip: true 
     Row { 
      anchors.verticalCenter: parent.verticalCenter 
      spacing: 10 
      Column { 
       Image { 
        source: "content/pics/arrow-up.png" 
        MouseArea { 
         anchors.fill: parent; 

         onClicked: { 

          var voice 
          var nameInModel 
          var nameInView 

          voice = listView.contentItem.children[1] 
          nameInModel = fruitModel.get(1).name 

          nameInView = voice.nameText 

          console.log("name before move in model at 1: "+nameInModel) 
          console.log("name before move in list at 1: "+nameInView) 

          fruitModel.move(index, index-1, 1) 

          voice = listView.contentItem.children[1] 

          nameInView = voice.nameText 

          nameInModel = fruitModel.get(1).name 
          console.log("name after move in model at 1: "+nameInModel) 
          console.log("name after move in list at 1: "+nameInView) 

         } 
        } 
       } 
.... 

: 코드 : 난 그냥 디버깅을 일부 수정과 함께 SDK에 포함 된 DynamicList 예에서 당신에게 코드 조각을 보여줍니다 "Apple"- "Banana"- "Cumqat"- "Durian"의 순서로 4 개의 항목이있는 예에서 모델의 항목 하나가 한 위치 위로 이동했습니다. 이는 consol.log의 결과입니다. if 내가 처음 위치로 해당 항목을 이동 "바나나"의 "최대"버튼을 클릭 :

이름 1에서 모델에서 이동하기 전에 : 1의 모델에서 이동 후 애플 이름 : 1의 목록에서 이동하기 전에 바나나 이름 : Apple,363,210 명 (1)에서 목록에서 이동 후 : 애플

이 내가 첫번째 위치로 이동하는 (즉, 지금은 2 위이다) 애플을 클릭하면 발생하는 것입니다 :

이름 모델의 이동에 이전 1 : 애플 이름 1에서 목록에서 이동하기 전에 : 1의 목록에서 이동 한 후 바나나 이름 : 1의 모델에서 이동 후 애플 이름 애플

그래서, 우선은 분명 그 첫 번째 인덱스 ListView의 첫 번째 인덱스가 1 인 동안 ListModel이 0 인 경우 모델의 항목은 이동되었지만 목록의 항목은 이동하지 않았 음이 분명합니다. 자, 이것이 내 문제입니다. "이름"은 TextEdit의 텍스트이므로 ListView 대리자는 TextEdit을 포함하며 사용자가 편집 할 수 있습니다. 난 그 텍스트를 가지고 싶다면 내가이 작업을 수행 할 수 있다는 사실을 알고 : 코드 :

for (var i = 0; i <= myListView.count; i++){ 
    myItem= myListView.contentItem.children[i] 
    myName = myListView.name 

그러나 여기에서 문제를 내의 ListView에서의 ListModel의 이동 기능을 사용하여 항목을 이동 할 수있는 가능성도 있습니다 그러나 항목을 이동하고 이전주기를 사용하여 "이름"을 가져 오면 아무 것도 바뀌지 않습니다 (DynamicList 예제와 동일). 한편 , 나는 "이름"을 가지고가는 경우에 사용하여 : 나는 텍스트 편집기에서 텍스트를 수정하고 나는 "이름"을 취할 때

myName= myListModel.get(i).name 

다음, 아무것도 보인다

변경,

그럼, TextEdit으로 ListView를 얻으려면 어떻게해야합니까? 그리고 어떤 변화를 기록 할 수있는 itemsof를 움직일 수있는 가능성이 있습니까?

난 그냥이 사용, 매우 Giammarco

답변

6

당신은 잘못하고 있습니다 감사합니다, 나는 명확했다 희망 :

import QtQuick 2.0; 

Rectangle { 
    width: 400; 
    height: 300; 

    ListView { 
     id: listTest; 
     clip: true; 
     currentIndex: -1; 
     model: ListModel { 
      id: modelTest; 

      ListElement { name: "Banana"; } 
      ListElement { name: "Apple"; } 
      ListElement { name: "Orange"; } 
      ListElement { name: "Pear"; } 
     } 
     delegate: Item { 
      id: item; 
      height: 60; 
      anchors { 
       left: parent.left; 
       right: parent.right; 
      } 

      property bool isCurrent : (model.index === listTest.currentIndex); 
      onIsCurrentChanged: { 
       if (isCurrent) { 
        input.forceActiveFocus(); 
       } 
       else { 
        input.focus = false; 
       } 
      } 

      Text { 
       id: label; 
       font.pixelSize: 14; 
       text: model.name; 
       visible: !item.isCurrent; 
       anchors { 
        left: parent.left; 
        right: btnUp.left; 
        margins: 20; 
        verticalCenter: parent.verticalCenter; 
       } 
      } 
      TextInput { 
       id: input; 
       text: model.name; 
       visible: item.isCurrent; 
       onTextChanged: { modelTest.setProperty (model.index, "name", text); } 
       anchors { 
        left: parent.left; 
        right: btnUp.left; 
        margins: 20; 
        verticalCenter: parent.verticalCenter; 
       } 

       Rectangle { 
        z: -1; 
        radius: 5; 
        antialiasing: true; 
        border { 
         width: 1; 
         color: "blue"; 
        } 
        anchors { 
         fill: parent; 
         margins: -5; 
        } 
       } 
      } 
      MouseArea { 
       id: clicker; 
       anchors.fill: parent; 
       visible: !item.isCurrent; 
       onClicked: { listTest.currentIndex = model.index; } 
      } 
      MouseArea { 
       id: btnUp; 
       width: height; 
       anchors { 
        top: parent.top; 
        right: parent.right; 
        bottom: parent.verticalCenter; 
       } 
       onClicked: { 
        if (model.index > 0) { 
         modelTest.move (model.index, model.index -1, 1); 
        } 
       } 

       Text { 
        text: "V"; 
        color: "gray"; 
        rotation: -180; 
        anchors.centerIn: parent; 
       } 
      } 
      MouseArea { 
       id: btnDown; 
       width: height; 
       anchors { 
        top: parent.verticalCenter; 
        right: parent.right; 
        bottom: parent.bottom; 
       } 
       onClicked: { 
        if (model.index < modelTest.count -1) { 
         modelTest.move (model.index, model.index +1, 1); 
        } 
       } 

       Text { 
        text: "V"; 
        color: "gray"; 
        anchors.centerIn: parent; 
       } 
      } 
      Rectangle { 
       height: 1; 
       color: "lightgray"; 
       anchors { 
        left: parent.left; 
        right: parent.right; 
        bottom: parent.bottom; 
       } 
      } 
     } 
     anchors.fill: parent; 
    } 
} 
+0

FWIW을, 라인 (33)'입력 = false로,'주는 'input'에 대한 에러가 좌변 값이 아닙니다. 나는 그 라인이 무엇을하고 있는지 또는 그것이 무엇을해야 하는지를 아직 이해하지 못한다. –

+0

@ SteveFallows에게 고맙습니다. – TheBootroo