ListView model binding to QAbstractListModel derived model
-
I have following QML ButtonMenu element:
@ Rectangle {
id: menuRoot
...
property Component model
...
Rectangle {
id: menu
...
ListView {
id: menuList
...
model: menuRoot.model;
...
@I use it like this:
@ButtonMenu {
id: buttonMenu
...
model: menuMainViewArea
...
@In C++ code I initiate model:
@ctxt->setContextProperty("menuMainViewArea",engine.getMenuMainViewArea());
@When I run my program i get following error:
Error: Cannot assign QObject* to QDeclarativeComponent*I tried to make model in menuRoot an alias to menuList model but when I called model.count when setting size of ButtonMenu I got model.count undefined.
Does anyone know what can be wrong??
-
Try this
@
Rectangle {
id: menuRoot
...
property alias model : menuList.model
...
Rectangle {
id: menu
...
ListView {
id: menuList
...
//model: menuRoot.model;
...
@ -
Thank you for a quick reply.
I haven't called qmlRegisterType() / qRegisterMetaType() have to read about them.getMenuMainViewArea() retrurns pointer to MenuMainViewArea.
@
...
MenuMainViewArea* iMainMenu;
...
MenuMainViewArea* Engine::getMenuMainViewArea() const
{
return iMainMenu;
}
@ -
your code as such should work with my proposed changes as long as your MenuMainViewArea is derived class of QAbstractListModel or QStringList
You don't need to use qmlRegisterType in this case.
-
Using alias works and data from my model is visible in ListView.
However my problem was a little bit different and it was caused by something else.My menu model changes its content depending from application state. In QML I have used
model.get(i).name and model.count to set appropriate size of my menu. My model from C++ doesn't have count property and get() function. I thought that they are added (like for normal QML model) somehow by framework, but they are not.I think that the most reasonable solution is to use ListModel and States in QML.
Thanks for your help :)
-
or your implement your own count property & get function inside your C++ class which returns model count.