TextField within ListView



  • I have created a listview with a delegate that produces a textfield. The effect is a flickable column of multiple text fields. It seems that Qt repaints these text fields as they appear and disappear from the viewable area which makes the program run a bit sluggish. I've had to place a few hacks to get the value and save it to the corresponding model, which led me thinking that I am perhaps going about this the wrong way. I haven't seen any examples with a textfield inside of a listview, so maybe I should use a different delegate?



  • How does your model look like? It should be quite straightforward to save the value to the model no?



  • This is my model:

    @ListModel {
    id: myModel
    ListElement {
    name: "Speed"
    units: "mm*100/s"
    group: "conveyor"
    mValue: "10"
    mid: 0
    }
    ....@

    and my delegate consist of a TextField with:
    @ onValueChanged: {
    // Save the input
    if(inputText != ""){
    myModel.set(mid, {"mValue": inputText});
    console.log(mid + "==" + inputText)
    }
    }@

    I use "mid" to identify the index and the inputText then to save. I have it working alright, but I couldn't find a way to grab the current delegate index as wel.



  • Hi,

    Your delegates have access to a special property "index".
    I refer to the link below for more explanation.

    https://qt-project.org/doc/qt-5-snapshot/qtquick-modelviewsdata-modelview.html#models

    But if you do something like, you can see it:

    @ onValueChanged: {
    console.log("index = " + index)
    }
    @

    Changing the value of the model can be done like so:

    @onAccepted: {console.log("accepted"); myModel.set(index, {...})}@

    Also ListView has a currentIndex and currentItem property if you need those.

    Good luck!


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.