Get data from TextEdit in a QML ListView

Go To StackoverFlow.com

1

I have a problem and I can't find a solution to it, I hope you to find it here: I did a ListView with its model and its delegate and so on, like this:

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;
    }
}

the delegate includes Text, TextEdit, etc.... something like this:

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

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

Inside the same Item, "creation", there is also a ToolBar with two Buttons, something like this:

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

What I want is this: when I click on the second button, "Create", I want simply to show with console.log(arg1,...) what is written in the TextEdit called "seriesTextEdit" of every item in the listView. For example, if my listview contains 10 items and the user insert a valued in all the TexTedit of all the item, how can I access to those data? Thank you very much, Giammarco

2012-04-03 23:25
by Giammarco


6

The ListView could be the wrong class to use since it destroys items which are not in the visible area and creates new ones which get visible. If you don't want that use a Repeater.

Anyway you can access the ListView's child items with ListView.contentItem.children. I tried and it contained an additional element (maybe the header).

I would suggest to have an alias property for the TextEdit text (seriesName in my example) in the root of your list item delegate. Probably you need the same for the index because as said, the ListView might not contain elements for all your model items. Then you can use a loop to get the texts.

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);
}
2012-04-04 18:44
by blakharaz
Great, you have been very helpful, principally because I didn't know the Repeater class, now I am using Repeater. Is there a ListView:positionViewAtIndex equivalent for the Repeater? I can't find it. By the way, in the PhotoViewer example they do not use Repeater but ListView and items are not destroyed even if they are not visible. HMow can they do that? Thank you again for your hel - Giammarco 2012-04-20 23:40
@Giammarco If you found blakharaz's answer correct, please accept his answer first ; - anticafe 2012-06-06 04:13
Ads