QML ListView does not reflect updates from its C++ data model [Solved]
-
You are creating a model, but it goes out of scope immediately after constructor terminates (I suppose your last code snippet is in a constructor).
You should ensure that the model remains in existence while the QML view is showing, e. g. by allocating on heap:
@MyDataModel *myDataModel = new MyDataModel(this);
context->setContextProperty("myDataModel", myDataModel);@ -
You are using "it's" wrongly. Have a look at http://www.elearnenglishlanguage.com/blog/english-mistakes/its/ . You have it also in the title.
-
bq. I just tried inserting that, and my Application Crashed as soon as that code was invoked. Narrowed it down to endResetModel() being the culprit.
I have been using this approach in all my projects where ListView is used. It worked all the time.
Why are you using QDeclarativeView ? Aren't you using Qt 5 ? -
[quote author="kwisatz" date="1421877712"]:D Thanks for pointing that out!
[/quote]
It's good that you fixed it :D -
Sorry, should have made this clear earlier on, I'm using Qt 4.8.4.
[quote author="p3c0" date="1421902494"]bq. I just tried inserting that, and my Application Crashed as soon as that code was invoked. Narrowed it down to endResetModel() being the culprit.
I have been using this approach in all my projects where ListView is used. It worked all the time.
Why are you using QDeclarativeView ? Aren't you using Qt 5 ?[/quote] -
Well then I'm not sure why it crashes. Qt 4.8.4 still is quite old in Qt4 series. Can you try it on Qt 4.8.6 ?
-
Okay, I did manage to get this to work finally.
When clearing data, I used:
@ beginResetModel();
m_data.clear();
endResetModel();@When removing an individual item in the data, I used:
@ beginRemoveRows(QModelIndex(), index, index);
m_data.removeAt(index);
endRemoveRows();@And I worked on this bit of code separately from my main application. Will have to see what kind of fun awaits when I merge the changes with the rest of the application ;)
Appreciate the help folks!