title: How to edit QQmlListProperty in QML
body: <p>If we want to use a list of custom C++ objects in QML we can use <code>QQmlListProperty</code></p>
  
  <p>While registering it in QML we need to specify a function that QML will read the list with.</p>
  
  <p>The documentation states that for fully functional list we need to use this function:</p>
  
  <pre><code>QQmlListProperty::QQmlListProperty(QObject *object, void *data, AppendFunction append,
                                     CountFunction count, AtFunction at, ClearFunction clear)
  </code></pre>
  
  <p>Here is example of how do I write it in C++:</p>
  
  <p><em>classwithlist.h</em></p>
  
  <pre><code>#ifndef CLASSWITHLIST_H
  #define CLASSWITHLIST_H
  
  #include &lt;QObject&gt;
  #include &lt;QQmlListProperty&gt;
  #include "element.h"
  
  class Element;
  
  class ClassWithList : public QObject
  {
      Q_OBJECT
      Q_PROPERTY(QQmlListProperty&lt;Element&gt; elements  READ getElements  NOTIFY elementsChanged)
  
  
  public:
      explicit ClassWithList(QObject *parent = 0);
  
      QQmlListProperty&lt;Element&gt; getElements();
      void appendElements(QQmlListProperty&lt;Element&gt; *list, Element *e);
      static int elementsCount(QQmlListProperty&lt;Element&gt; *list);
      static Element* elementsAt(QQmlListProperty&lt;Element&gt; *list, int i);
      static void elementsClear(QQmlListProperty&lt;Element&gt; *list);
  
  signals:
      void elementsChanged(QQmlListProperty&lt;Element&gt;);
  
  private:
      QList&lt;Element *&gt; m_elements;
  
  };
  
  #endif // CLASSWITHLIST_H
  </code></pre>
  
  <p><em>classwithlist.cpp</em></p>
  
  <pre><code>#include "classwithlist.h"
  
  ClassWithList::ClassWithList(QObject *parent) : QObject(parent)
  {
  
  }
  
  QQmlListProperty&lt;Element&gt; ClassWithList::getElements()
  {
      return QQmlListProperty&lt;Element&gt;(this, m_elements, &amp;appendElements,&amp;elementsCount,&amp;elementsAt,&amp;elementsClear);
  }
  void ClassWithList::appendElements(QQmlListProperty&lt;Element&gt; *list, Element *e)
  {
      ClassWithList *cwl = qobject_cast&lt;ClassWithList*&gt;(list-&gt;object);
      if (cwl &amp;&amp; e) {
          cwl-&gt;m_elements.append(e);
      }
  }
  int ClassWithList::elementsCount(QQmlListProperty&lt;Element&gt; *list)
  {
      ClassWithList *cwl = qobject_cast&lt;ClassWithList*&gt;(list-&gt;object);
      if (cwl)
          return cwl-&gt;m_elements.count();
      return 0;
  }
  Element *ClassWithList::elementsAt(QQmlListProperty&lt;Element&gt; *list, int i)
  {
      ClassWithList *cwl = qobject_cast&lt;ClassWithList*&gt;(list-&gt;object);
      if (cwl)
          return cwl-&gt;m_elements.at(i);
      return 0;
  }
  void ClassWithList::elementsClear(QQmlListProperty&lt;Element&gt; *list)
  {
      ClassWithList *cwl = qobject_cast&lt;ClassWithList*&gt;(list-&gt;object);
      if (cwl) {
          cwl-&gt;m_elements.clear();
      }
  }
  </code></pre>
  
  <p>And after exposing the class to QML I have following code in QML</p>
  
  <pre><code>ClassWithList {
      Component.onCompleted: {
          console.log(elements.length) // works
          console.log(elements[0])     // works
  
  //        elements.push()      // does not work
  //        elements.append()    // does not work
  //        elements.clear()     // does not work
  //        elements.at()        // does not work
      }
  }
  </code></pre>
  
  <p>Can I use functions that will add items to the list or clear it? As seen above I can use functions <code>CountFunction</code> and <code>AtFunction</code> by using <code>.length</code> and brackets. Can I somehow use <code>ClearFunction</code> and <code>AppendFunction</code> too?</p>
  
  <p>Maybe I cannot do this with <code>QQmlListProperty</code> and I should use <code>QAbstractListModel</code>?</p>