title: QML/C++ model - view separation: data in C++, multiple lists displaying this data in QML
body: <p>I am trying to strictly separate the data (in C++) from the visualization (lists in QML). On the model side, data is stored in instances of a C++ class (Data.cpp) and pointers to that instances are arranged in two lists (completeList and selectedList) by another C++ class (Parser.cpp). Those two lists should be displayed in QML, with the following restrictions:</p>
  
  <ul>
  <li>the data in completeList is displayed in different QML ListViews (for example according to the name)</li>
  <li>this separation must be done in QML, not by Parses.cpp (which only provides the complete list)</li>
  <li>it must be possible to add list items from completeList to selectedList (for example by clicking on it)</li>
  <li>if values of Data are changed in one ListView, the Data itself and the other views displaying this Data should change accordingly</li>
  </ul>
  
  <p>Thanks to ksimons, <a href="https://stackoverflow.com/questions/19841328/view-edit-and-update-data-from-c-in-qml-with-multiple-views-while-the-dat/19844170?noredirect=1#comment29543902_19844170">I found a way to do half of it</a>, but I cant figure out the rest. Here is what I got so far:</p>
  
  <p>Data.h</p>
  
  <pre><code>class Data : public QObject
  {
      Q_OBJECT
      Q_PROPERTY(QString name READ name WRITE setName NOTIFY nameChanged)
  
  public:
      Data(const QString &amp;n) : _name(n) { }
      QString name() const { return _name; }
  
      void setName(const QString &amp;n) {
          if (_name == n)
              return;
          _name = n;
          emit nameChanged(n);
      }
  
  signals:
      void nameChanged(const QString &amp;n);
  
  private:
      QString _name;
  };
  </code></pre>
  
  <p>Parser.h</p>
  
  <pre><code>class Parser : public QObject
  {
      Q_OBJECT
      Q_PROPERTY(QQmlListProperty&lt;Data&gt; completeList READ completeList CONSTANT)
      Q_PROPERTY(QQmlListProperty&lt;Data&gt; selectedList READ selectedList CONSTANT)
  
  public:
      Parser(QObject *parent = 0) {
           Data* d1 = new Data(QStringLiteral("a1"));
           Data* d2 = new Data(QStringLiteral("a2"));
           Data* d3 = new Data(QStringLiteral("a3"));
           Data* d4 = new Data(QStringLiteral("b1"));
           Data* d5 = new Data(QStringLiteral("b2"));
  
          _completeList.append(d1);
          _completeList.append(d2);
          _completeList.append(d3);
          _completeList.append(d4);
          _completeList.append(d5);
  
          _selectedList.append(d1);
          _selectedList.append(d4);
      }
  
      QQmlListProperty&lt;Data&gt; completeList() {
          return QQmlListProperty&lt;Data&gt;(this, _completeList);
      }
      QQmlListProperty&lt;Data&gt; selectedList() {
          return QQmlListProperty&lt;Data&gt;(this, _selectedList);
      }
  
  private:
      QList&lt;Data*&gt; _completedList;
      QList&lt;Data*&gt; _selectedList;
  };
  </code></pre>
  
  <p>QML part:</p>
  
  <pre><code>ListView
  {
      id: startsWithA
      anchors.fill: parent
      delegate: delegateItem
  }
  ListView
  {
      id: startsWithB
      anchors.fill: parent
      delegate: delegateItem
  }
  ListView
  {
      id: selectedData
      anchors.fill: parent
      delegate: delegateItem
  }
  Item 
  {
       id: delegateItem
       height: 30
       width: parent.width
  
       Text { text: name }
  
       MouseArea { //change name of corresponding Data and other views accordingly
       anchors.fill: parent
       onClicked: model.name = "newName";} //does not work
  
       MouseArea { //add to selectedList in Parser
       anchors.fill: parent
       onClicked: ?????? }
  }
  Component.onCompleted:
  {
      selectedData.model = parser.seletedList;
      var listAll = parser.completeList;
      var listA, listB;
      for(var i=0; i&lt;listAll.length; i++)
      {
          if(listAll[i].name.charAt(0) == 'a')
          {
              //changing values in startsWithA does not affect anything else with that :(
              listA.append(listAll[i]); 
          }
          else if(listAll[i].name.charAt(0) == 'b')
          {
              //changing values in startsWithB does not affect anything else with that :(
              listB.append(listAll[i]); 
          }
          //...
      }
      startsWithA.model = listA;
      startsWithB.model = listB;
      //...
  }
  </code></pre>
  
  <p>I hope this question is not to specific and somebody can help me. Thank you in advance.</p>