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

Preet preet.desai at gmail.com
Sun Jul 10 02:20:23 CEST 2011


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.

I'd appreciate any advice!

-Preet

// sometype.h
class SomeType : public QObject
{
   Q_OBJECT
   Q_PROPERTY(QString myname READ GetName NOTIFY nameChanged)

public:
   SomeType(QObject* parent=0);
   QString GetName();
   Q_INVOKABLE QVariant GetChildren();

signals:
   void nameChanged();

private:
   QString m_name;
   QList<QObject*> m_children;
}

//----------------------------------
// sometype.cpp
...
QVariant SomeType::GetChildren()
{  return QVariant::fromValue(m_children);  }

//----------------------------------
// in main.cpp
QList<QObject*> m_list_of_someTypes;  // populate list
m_view_context->setContentProperty("list_outer_model",QVariant::fromValue(m_list_of_someTypes);

//----------------------------------
// in qml
ListView
{
   id: list_outer;
   model: list_outer_model;
   Component
   {
      id: delegate_outer;
      ListView
      {
         id: list_inner;
         model: CurrentDelegate.Object.GetChildren() // how do I do this?
         Component
         {
            id: delegate_inner;
         }
         delegate: delegate_inner;
      }
   }
   delegate: delegate_outer;
}
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.qt.nokia.com/pipermail/qt-qml/attachments/20110709/91fabbb9/attachment.html 


More information about the Qt-qml mailing list