2012-05-03 134 views
2

我有一個ListModel和ListView的小問題,特別是有關ListModel的移動功能。我會告訴你的代碼片段從包含在與一些修改SDK中的DynamicList例子只是爲了調試它: 代碼: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) 

         } 
        } 
       } 
.... 

所以,當點擊圖像「內容/圖片/箭頭up.png」時,模型中的一個項目向上移動一個位置,現在,在該示例中有四個項目按順序排列:「Apple」 - 「Banana」 - 「Cumqat」 - 「榴蓮」,這是consol.log的結果if我點擊「向上」的「香蕉」移動該項目的第一位置:

在1模型行動前名:1模型移動後蘋果 名稱:香蕉 名稱列表中1行動之前:蘋果在列表中招後,名:蘋果

這是,如果我在蘋果點擊(現在是在第2位),將它移動到第一的位置會發生什麼:

模型在行動前名1:蘋果 名字在1列表中招前:1在列表中招後香蕉 名稱:蘋果 名在模型1的舉動後蘋果

所以,首先很明顯的第一個索引ListModel爲0,而ListView的第一個索引爲1,那麼顯然模型中的項目已被移動,但列表中的項目沒有。 現在,這是我的問題:想象一下,「名稱」是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