2012-04-03 56 views
1

我有一個問題,我找不到解決方案,我希望你在這裏找到它:我做了一個ListView與它的模型和它的委託等等,就像這樣:從QML ListView中的TextEdit獲取數據

Item { 
id: creation; width: parent.width ; height: parent.height 
..... 
ListView { 
     id: mainViewLiist 
     model: CreationModel {id: modelCreation} 
     delegate: delegateCreation 
     width: parent.width; height: parent.height; x: -(screen.width * 1.5); 
     cacheBuffer: 100; 
    } 
} 

委託包括文本,文本編輯,等等....這樣的事情:

Component { 
id: creationDelegate 
Item { 
    id: itemCreate 
    .... 
    Row { 
       id: rowSerie 
       spacing: 5 
       Text { 
        id: seriesLabel 
        text: "Series:" 
        .... 
       } 

       TextEdit { 
        id: seriesTextEdit 
        text: "" 
        .... 
        } 
      } 
    .... 
    } 
.... 
} 

內同一個項目,「創造」中,也有兩個按鈕的工具欄,東西像這樣:

ToolBar { id: toolBarCreation; height: 40; 
    width: parent.width; 
    opacity: 1.0 
    button1Label: "Back" 
    button2Label: "Create" 
    onButton1Clicked: 
    { 
     ... 
    } 
    onButton2Clicked: 
    { 
     ... 
    } 
    } 

我想要的是:當我點擊第二個按鈕「創建」時,我只想簡單地用console.log(arg1,...)顯示在每個項目的TextEdit中稱爲「seriesTextEdit」的內容在listView中。例如,如果我的列表視圖包含10個項目,並且用戶在所有項目的所有TexTedit中插入一個值,那麼我如何訪問這些數據? 非常感謝, Giammarco

回答

6

ListView可能是錯誤的類使用,因爲它銷燬不在可見區域的項目,並創建新的可見的項目。如果你不想使用中繼器。

無論如何,你可以通過ListView.contentItem.children訪問ListView的子項。我試過了,它包含一個額外的元素(也許是頭)。

我會建議有一個別名屬性爲TextEdit文本(我的例子中的seriesName)在您的列表項目委託的根。可能你需要相同的索引,因爲如上所述,ListView可能不包含所有模型項目的元素。 然後你可以使用循環來獲取文本。

print(mainViewLiist.contentItem.children.length); 
for (var i = 0, l = mainViewLiist.contentItem.children.length; i < l; i++) { 
    var child = mainViewLiist.contentItem.children[i]; 
    print(i, ": ", child, "=", child.text); 
} 
+0

大,你非常有幫助,主要是因爲我不知道Repeater類,我現在用的中繼器。是否有一個與Repeater相同的ListView:positionViewAtIndex?我找不到它。順便說一下,在PhotoViewer示例中,它們不使用Repeater,但使用ListView,即使它們不可見,項目也不會被銷燬。 HMow他們能做到嗎?再次感謝您的幫助 – Giammarco 2012-04-20 23:40:59

+0

@Giammarco如果您發現blakharaz的回答正確,請先接受他的回答;) – anticafe 2012-06-06 04:13:44