Automatic update of listView and c++ model
-
Hi,
I'm trying to write a c++ model like the one describe in the example (Animal model). I would like to add a function to modified the model. I've add a Q_INVOKABLE function in the AnimalModel class.
@ void AnimalModel::changeData(int number,QString type, QString size)
{
if ( number >= 0 && number < m_animals.count())
{
qDebug() <<"number:"<<number << " new type : "<< type<<" new size : "<< size;
m_animals[number].m_type = type;
m_animals[number].m_size = size;
emit dataChanged(index(0),index(m_animals.count()));
}
}@I see my message in the warning window but my view never update. I've read Model subclassing reference but I haven't found a thing to help me.
Thanks for reading me.
-
No idea what your AnimalModel is and where it comes from, but it looks to me like you are changing your underlying data structure (m_animals) and then tell the view that the model has changed. But you never update the model (or tell the model that the underlying data has changed).
-
Animal model is the one defined "here":http://doc-snapshot.qt-project.org/4.8/declarative-modelviews-abstractitemmodel.html
I've only change the Animal class to have all membres public.My model is a QList of Animal, which are modified line 6 and 7 and I guess I warn the model is updated line 8.
-
Sorry, that was a very confusing response from me...
Now that I see the code, I realise there's nothing wrong with your code, except for one tiny problem, and that is that when you emit dataChanged the second argument is out-of-range. It should be:
@emit dataChanged(index(0), index(m_animals.count() - 1));@
But that will invalidate the entire model, so really, you should just do:
@emit dataChanged(index(number), index(number));@ -
That's what I tried first but it didn't work, so I changed it just to see.
If I did all correctly I should raise it as a bug ?
-
Are you sure? I tried it, and it works just fine for me.
-
Yeah you're right that's working... I don't konw why this was'nt working the first time.
Thanks a lot. -
Great. You're welcome!