2013-01-20 243 views
5

我需要一些幫助,添加元素爲QML列表視圖,我有一個textarea和一個按鈕,將增加textarea的文本被按下時,ListView項,這裏是我的嘗試:QML,動態地添加元素到ListView

Component { 
    id: delegate 
    Item { 
     width: 200; height: 28 
     Label { 
      text: score 
     } 
    } 
} 

ListView { 
    id: p1scores 
    model: p1model 
    delegate: delegate 
    anchors.top: p1name.bottom 
    anchors.topMargin: units.gu(1) 
} 

ListModel { 
    id: p1model 
    ListElement { score: "0" } 
} 

TextArea { 
    id: p1input 
    width: units.gu(8) 
    height: units.gu(3) 
    horizontalAlignment: TextEdit.AlignHCenter 
    inputMethodHints: Qt.ImhDigitsOnly 
    contentHeight: units.gu(60) 
    anchors.topMargin: units.gu(8) 
} 

Button { 
    id:p1button 
    text: i18n.tr("Add") 
    width: units.gu(8) 
    onClicked: { 
     p1model.append({"score": p1input.text}) 
     p1input.text = "" 
    } 
} 

我試圖追加它,但沒有顯示在列表視圖中......任何幫助?

+4

在p1button onClicked,您清除在文本區文本* *前添加文本到ListModel的,當然文字也不會顯示向上。 – Dickson

+0

該文本仍然不顯示,我認爲它與代表的問題...(編輯問題) – Hairo

+0

我不知道,但...更改您的委託組件id到別的東西,因爲當你在你的ListView中使用'delegate:delegate',它假設是'delegate:p1scores.delegate',所以它綁定回自己。 – Dickson

回答

8

嘗試沒有圍繞「分數」引號,像這樣:

onClicked: { 
    p1model.append({score: p1input.text}) 
    p1input.text = "" 
}