简单记录下:
当然不同版本的qt提供了不同的api,比如5.15版本提供了
Item itemAtIndex(index)
而,5.10版本则没有,还是要先看看是否qt版本提供了便利的apI
方法1:
ListView {
id: list
model:10
delegate: {
TextField {
id: textfield
}
}
}
function getAllText(){
for(var i=0; i<listview.count; ++i)
console.log(listview.itemAt(0, listview.delegate.height * i).text)
}
function getText(index){
console.log(listview.itemAt(0, listview.delegate.height * index).text)
}
方法二:
比较笨的方法,在外面声明一个数组,每次delegate属性变化时,实时的来记录状态,
我的场景是,有很多CheckBox,要记录有哪些CheckBox被选中,并且限制了选中数量
比如我的delegate
Component {
id:imgDelegate
CheckBox
{
anchors.left : parent.left
anchors.leftMargin: 10
anchors.verticalCenter: parent.verticalCenter
anchors.verticalCenterOffset: 6
checked: false
onCheckStateChanged:
{
if(checked)
{
addPrintItem(index);
}
else
{
delPrintItem(index);
}
}
}
}
property var printCheckedArray : new Array
property var printMax : 10
//去重
function unique(arr){
for(var i=0; i<arr.length; i++){
for(var j=i+1; j<arr.length; j++){
if(arr[i]===arr[j]){ //第一个等同于第二个,splice方法删除第二个
arr.splice(j,1);
j--;
}
}
}
return arr;
}
//是否选中数到达了限制
function checkIsMax()
{
if(printCheckedArray.length >= printMax)
return true;
return false;
}
//选中
function addPrintItem(index)
{
printCheckedArray.push(index);
unique(printCheckedArray);
console.log(printCheckedArray.length,printCheckedArray);
}
//取消选中
function delPrintItem(index)
{
var curPos = printCheckedArray.indexOf(index)
console.log(printCheckedArray.length,index,curPos);
if(curPos>=0)
{
printCheckedArray.splice(curPos,1);
}
console.log(printCheckedArray.length,printCheckedArray);
}