Solved Using Combobox in delegate does to trigger model to change data
-
Hello,
Recently I decided to try QML, but I have some problems. QML is soo cool but it looks like to me at the same time it is tricky too :)
Well, let's look at my problem. I have a ListModel that in c++ and set to TableView in QML. The model consists of 4 roles. When I use TextField in delegate everything is working as expected.
But What I want is Add Combobox for delegate to make selection and change data. Whenever I start use combo box I am no longer to change data in the model.
Working code with TextField:
TableViewColumn { role: "name" title: "Name" width: 250 delegate: TextField { anchors.verticalCenter: parent.verticalCenter text: model.name onEditingFinished: model.name = text } }
Problematic code:
TableViewColumn { role: "name" title: "Name" width: 120 delegate: ComboBox { anchors.verticalCenter: parent.verticalCenter model: ["Alice", "Bob"] currentIndex: model.name === "Alice" ? 0 : 1 onCurrentTextChanged: { console.log("from component", currentText) model.name = currentText } } }
What is the reason causing this problem?
Thanks in advance.
-
ComboBox itself has model. When you access the model, it is changing the model of combobox not the parent model.
-
@dheerendra thank you for your response. So what is the solution then? I cannot use combobox in MVC?
What is the alternative way to solve this problem?
Thanks
-
@Celal Try this idea (no full code here):
delegate: Item { id: i ComboBox { oncurrentTextChanged: { i.model.name = currentText }
-
@Eeli-K It does not work in your way. It was giving "Type error"
However, I like your approach. So then I tried something else.
Instead of id, I define a property for each row then I use property to assign the new value then it works
here is the solution:
delegate: RowLayout { property var rmodel: model ComboBox { model: ListModel { ListElement {name : "9600"} ListElement {name : "112500"} } textRole: "name" currentIndex: rmodel.baudrate === "9600" ? 0 : 1 onCurrentTextChanged: { rmodel.baudrate = currentText } }
Thanks for help.
Celal