从QML ListView中的TextEdit获取数据

从QML ListView中的TextEdit获取数据,listview,qml,textedit,Listview,Qml,Textedit,我有一个问题,我找不到解决方案,我希望你能在这里找到它:我用它的模型和它的委托做了一个ListView,诸如此类: Item { id: creation; width: parent.width ; height: parent.height ..... ListView { id: mainViewLiist model: CreationModel {id: modelCreation} delegate: delegateCreatio

我有一个问题,我找不到解决方案,我希望你能在这里找到它:我用它的模型和它的委托做了一个ListView,诸如此类:

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:
    {
        ...
    }
  }
代表包括文本、文本编辑等。。。。大概是这样的:

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:
    {
        ...
    }
  }
在同一项目“创建”中,还有一个带有两个按钮的工具栏,如下所示:

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,…)显示listView中每个项目在名为“SeriesExtendit”的文本编辑中写入的内容。例如,如果我的listview包含10个项目,并且用户在所有项目的TexTedit中插入了一个值,那么我如何访问这些数据? 非常感谢你,
Giammarco

ListView可能是一个错误的类,因为它会销毁不在可见区域的项目,并创建新的可见项目。如果你不想那样,就用中继器

无论如何,您可以使用ListView.contentItem.children访问ListView的子项。我试过了,它包含了一个额外的元素(可能是标题)

我建议在列表项委托的根目录中为TextEdit文本(在我的示例中为seriesName)设置alias属性。可能索引也需要相同的元素,因为如上所述,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);
}
print(mainviewlist.contentItem.children.length);
对于(变量i=0,l=mainViewLiList.contentItem.children.length;i
ListView可能是错误的类,因为它会销毁不在可见区域中的项,并创建新的可见项。如果你不想那样,就用中继器

无论如何,您可以使用ListView.contentItem.children访问ListView的子项。我试过了,它包含了一个额外的元素(可能是标题)

我建议在列表项委托的根目录中为TextEdit文本(在我的示例中为seriesName)设置alias属性。可能索引也需要相同的元素,因为如上所述,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);
}
print(mainviewlist.contentItem.children.length);
对于(变量i=0,l=mainViewLiList.contentItem.children.length;i
太好了,您帮了我很大的忙,主要是因为我不知道Repeater类,现在我正在使用Repeater。中继器是否有等效的ListView:positionViewAtIndex?我找不到它。顺便说一下,在PhotoViewer示例中,他们不使用Repeater,但ListView和项目不会被销毁,即使它们不可见。他们怎么能做到?再次感谢您的支持help@Giammarco如果您发现blakharaz的答案正确,请先接受他的答案;)很好,你帮了我很大的忙,主要是因为我不知道Repeater类,现在我使用Repeater。中继器是否有等效的ListView:positionViewAtIndex?我找不到它。顺便说一下,在PhotoViewer示例中,他们不使用Repeater,但ListView和项目不会被销毁,即使它们不可见。他们怎么能做到?再次感谢您的支持help@Giammarco如果您发现blakharaz的答案正确,请先接受他的答案;)