July 14, 2011

michal.k michal.k
Lab Rat
70 posts

ListView model binding to QAbstractListModel derived model

 

I have following QML ButtonMenu element:

  1.     Rectangle {
  2.     id: menuRoot
  3.     ...
  4.     property Component model
  5.     ...
  6.     Rectangle {
  7.          id: menu
  8.          ...
  9.          ListView {
  10.               id: menuList
  11.               ...
  12.               model: menuRoot.model;
  13.               ...

I use it like this:

  1. ButtonMenu {
  2.     id: buttonMenu
  3.     ...
  4.     model: menuMainViewArea
  5.     ...

In C++ code I initiate model:

  1. 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??

7 replies

July 14, 2011

Lukas Geyer Lukas Geyer
Dinosaur Breeder
2074 posts

What does getMenuMainViewArea() return?
Have you called qmlRegisterType() / qRegisterMetaType() for your model?

July 14, 2011

Vijay Bhaska Reddy Vijay Bhaska Reddy
Lab Rat
399 posts

Try this

  1. Rectangle {
  2. id: menuRoot
  3. ...
  4. property alias model : menuList.model
  5. ...
  6. Rectangle {
  7.      id: menu
  8.      ...
  9.      ListView {
  10.           id: menuList
  11.           ...
  12.           //model: menuRoot.model;
  13.           ...

July 14, 2011

michal.k michal.k
Lab Rat
70 posts

Thank you for a quick reply.
I haven’t called qmlRegisterType() / qRegisterMetaType() have to read about them.

getMenuMainViewArea() retrurns pointer to MenuMainViewArea.

  1. ...
  2.     MenuMainViewArea* iMainMenu;
  3. ...
  4. MenuMainViewArea* Engine::getMenuMainViewArea() const
  5. {
  6.     return iMainMenu;
  7. }

July 14, 2011

Vijay Bhaska Reddy Vijay Bhaska Reddy
Lab Rat
399 posts

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.

July 14, 2011

michal.k michal.k
Lab Rat
70 posts

Will check it later. Now I have to do something else :/

July 16, 2011

michal.k michal.k
Lab Rat
70 posts

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 :)

July 16, 2011

Vijay Bhaska Reddy Vijay Bhaska Reddy
Lab Rat
399 posts

or your implement your own count property & get function inside your C++ class which returns model count.

 
  ‹‹ [Moved] MouseArea/Loader Question      [SOLVED] Qt Quick Application: empty window ››

You must log in to post a reply. Not a member yet? Register here!