Insert/remove/accessing items on QAbstractListModel
-
Hi everyone,
I need some help related to several operations that I want to do on QAbstractListModel.
Let's suppose that we have a model like this:
QAbstractListModel* myModel; // myModel is an array of structs that fills a QML ListViewThe questions:
-
How can I access the structs variables from, let's say, third element of myModel ?
QVariant qv = myModel->data(myModel->index(3, 0), Qt::DisplayRole); // this is how I access the third element, but how can I read the data from the structure inside "qv" ? -
How can I insert/remove elements in myModel ?
For removing elements I used this method:
myModel->removeRow(0, myModel->index(0, 0));
but apparently doesn't have any effect.
Can someone tell me what I do wrong?
Thanks a lot.
-
-
Hi,
@JohnRS said in Insert/remove/accessing items on QAbstractListModel:
How can I access the structs variables from, let's say, third element of myModel ?
QVariant qv = myModel->data(myModel->index(3, 0), Qt::DisplayRole); // this is how I access the third element, but how can I read the data from the structure inside "qv" ?Use custom roles. One for each field you want to expose.
@JohnRS said in Insert/remove/accessing items on QAbstractListModel:
How can I insert/remove elements in myModel ?
For removing elements I used this method:
myModel->removeRow(0, myModel->index(0, 0));
but apparently doesn't have any effect.You can add a method to your model that accepts an object of the type you store.