[Qt-qml] Nested ListViews using QList<QObject*> model

bea.lam at nokia.com bea.lam at nokia.com
Thu Jul 14 08:17:54 CEST 2011


Hi Preet,

On 10/07/2011, at 10:20 AM, ext Preet wrote:

> Hiya,
> 
> I'm trying to create a nested ListView from a simple QList model in C++, of type QList<SomeType*>. The SomeType object can have children of its own type as well. So each SomeType object has a QList<SomeType*> in it. Starting with an 'outer list' of QList<SomeType*>s, I wanted to make a ListView in QML like this:
> 
> SomeTypeA
>   child1SomeTypeA
>   child2SomeTypeA
> SomeTypeB
>   child1SomeTypeB
>   child2SomeTypeB
>   child3SomeTypeB
> ... etc.
> 
> I kept running into problems with my attempts to do this:
> * I get metatype errors when trying to convert QList<QObject*> into a QVariant to have it accessible to the QML engine with Q_INVOKABLE. This doesn't happen when I'm using setContextProperty in the same way... why is this? 
> * I'm not sure how to get the current object from within the delegate, or even if I'm approaching this the right way.
> 


A QList<QObject*> has no way of notifying the view when its contents have changed.  Instead you could use a QDeclarativeListProperty for the children. E.g.

class SomeType : public QObject
{
	Q_PROPERTY(QDeclarativeListProperty<QObject> children READ children NOTIFY childrenChanged)


There are some examples of using QDeclarativeListProperty in the C++ extensions tutorial: http://doc.qt.nokia.com/4.7/declarative-tutorials-extending-chapter5-listproperties.html

The inner ListView can then use "children" as its model which will reference the 'children' property of the current delegate object (although you might want to use a name other than "children" to avoid confusion with ordinary child items in QML).


regards,

Bea



More information about the Qt-qml mailing list