Marcin Plebanek
Marcin Plebanek

Reputation: 121

How to append new ListElement in QML?

So, I need a button that would add a new ListElement to existing list. I need to fill in one value, taken from TextField. I have no idea how to do this. I tried something like

onClicked:{
nameOfList.append(JSON.parse([nameOfList.nameOfColumn,myTextField.text]));
}

Obviously it didn't work. Do I have to send these two values to C++, then make a QJsonObject and send it back, or is there an easier way?

Upvotes: 2

Views: 3617

Answers (2)

arhzu
arhzu

Reputation: 570

While the question does not describe the type of nameOfList, I assume it is ListModel since the question is about adding a new ListElement. In that case, appending would be straightforward:

onClicked:{
 nameOfList.append({"nameOfColumn": myTextField.text})}
}

However, if nameOfList.nameOfColumn is not constant, you'd need to make a temporary:

var temp = {}
temp[nameOfList.nameOfColumn] = myTextField.text
nameOfList.append(temp)

Upvotes: 2

Andrei R.
Andrei R.

Reputation: 2452

It can depend on what exactly nameOfList is, but here you will most probably find an answer.

Upvotes: 1

Related Questions