[Qt-qml] Dummy model for QList<QObject*> model

Cornelius Hald hald at icandy.de
Mon Sep 13 13:38:10 CEST 2010


On Mon, 2010-09-13 at 11:24 +0200, Cornelius Hald wrote:
> I will now try to implement my own QAbstractItemModel like in this
> example[1]. However, I would prefer a more generic way. In fact the
> QObjectListModel really looks like the right thing. Do you have any idea
> what I'm doing wrong?

So I did my own model and first I got the exact same problem:

QMetaProperty::read: Unable to handle unregistered datatype
'MyListModel*' for property 'MyApp::myModel'.

The problems seems to be that I'm exposing a 'manager' class, e.g. MyApp
to QML and the data model is a property of this class.

If I expose the model directly everything is working. So, for example,
doing:
MyListModel* mod = new MyListModel();
mod->addObject(MyObject("Name 1", "bla 1"));
mod->addObject(MyObject("Name 2", "bla 2"));
ctxt->setContextProperty("myMod", mod);

I can use 'myMod' as model for ListViews.

However, if I have a class MyApp and MyApp has a property MyApp::myModel
that returns an instance of MyListModel and I expose it like:
ctxt->setContextProperty("myApp", app);

I should be able to use 'myApp.myModel' as model for a ListView.
Unfortunately this is not working. Instead I just get the error
mentioned above.

Do I have to use Q_DECLARE_METATYPE() with MyListModel? I'll give it a,
but I would be very grateful for additional hints.

Thanks!
Conny





More information about the Qt-qml mailing list