title: Qt QML data model doesn't seem to work with C++
body: <p>I've been working with the examples in <a href="http://doc.qt.digia.com/4.7/qdeclarativemodels.html" rel="nofollow noreferrer">http://doc.qt.digia.com/4.7/qdeclarativemodels.html</a> which is the Qt page on QML declarative data models. In particular, I'm working with the <code>objectlistmodel</code> example that comes with the Qt SDK (in examples/declarative/modelviews/objectlistmodel). It all seems to work reasonably well, until I try to combine it with the QMLPageControl example at <a href="http://www.developer.nokia.com/Community/Wiki/How_to_create_a_Page_Control_component_in_QML" rel="nofollow noreferrer">http://www.developer.nokia.com/Community/Wiki/How_to_create_a_Page_Control_component_in_QML</a>.</p>
  
  <p>When I try to display a QML-based ListModel (populated with QML ListElements) with a QML ListView like this:</p>
  
  <pre><code>import QtQuick 1.0
  
  Rectangle {
     width: 200; height: 200
  
     ListModel {
         id: qmlModel
         ListElement { name: "qml entry1 (red)"; colour: "red" }
         ListElement { name: "qml entry2 (orange)"; colour: "orange" }
         ListElement { name: "qml entry3 (yellow)"; colour: "yellow" }
         ListElement { name: "qml entry4 (green)"; colour: "green" }
         ListElement { name: "qml entry5 (blue)"; colour: "blue" }
         ListElement { name: "qml entry6 (purple)"; colour: "purple" }
     }
  
  
     ListView {
  
         id: list_view
  
         anchors.fill: parent
         model: qmlModel
         delegate: Rectangle {
             height: 20
             width: 200
             color: colour
             Text { text: name }
  
         }
      }
  }
  </code></pre>
  
  <p>...everything works quite nicely. This works entirely as expected - a window pops up with some text across colored backgrounds in bands.</p>
  
  <p><img src="https://i.stack.imgur.com/0F4NF.png" alt="ListView displaying a QML ListModel"></p>
  
  <p>Then, I can do something a bit more complicated, like use a PathView:</p>
  
  <pre><code>import QtQuick 1.0
  
  Rectangle {
      width: 200; height: 200
  
      ListModel {
          id: qmlModel
          ListElement { name: "qml entry1 (red)"; colour: "red" }
          ListElement { name: "qml entry2 (orange)"; colour: "orange" }
          ListElement { name: "qml entry3 (yellow)"; colour: "yellow" }
          ListElement { name: "qml entry4 (green)"; colour: "green" }
          ListElement { name: "qml entry5 (blue)"; colour: "blue" }
          ListElement { name: "qml entry6 (purple)"; colour: "purple" }
      }
  
  
      //       ListView {
      //           id: list_view
      //           anchors.fill: parent
      //           model: qmlModel
      //           delegate: Rectangle {
      //               height: 20
      //               width: 200
      //               color: colour
      //               Text { text: name }
      //           }
      //       }
  
      PathView {
          id: my_path_view
  
          anchors.fill: parent
  
          Keys.onRightPressed: if (!moving &amp;&amp; interactive) incrementCurrentIndex()
          Keys.onLeftPressed: if (!moving &amp;&amp; interactive) decrementCurrentIndex()
  
          flickDeceleration: 500
  
          preferredHighlightBegin: 0.5
          preferredHighlightEnd: 0.5
          focus: true
          interactive: true
          model: qmlModel
  
          delegate: Rectangle {
              width: 100
              height: 100
              color: colour
              Text {
                  anchors.centerIn: parent
                  text: name
              }
          }
  
  
          path: Path {
              startX: - my_path_view.width * my_path_view.model.count / 2 + my_path_view.width / 2
              startY: my_path_view.height / 2
              PathLine {
                  x: my_path_view.width * my_path_view.model.count / 2 + my_path_view.width / 2
                  y: my_path_view.height / 2
              }
          }
      }
  }
  </code></pre>
  
  <p>Again, this all works as expected - a window pops up with a flickable, dragable list of colored boxes.</p>
  
  <p><img src="https://i.stack.imgur.com/fDwNB.png" alt="PathView displaying a QML ListModel"></p>
  
  <p><img src="https://i.stack.imgur.com/HwSIc.png" alt="PathView displaying a QML ListModel, in motion"></p>
  
  <p>Backing up, I can then define a data object in C++ like this:</p>
  
  <p>dataobject.h</p>
  
  <pre><code>#ifndef DATAOBJECT_H
  #define DATAOBJECT_H
  
  #include &lt;QObject&gt;
  
  class DataObject : public QObject
  {
      Q_OBJECT
  
      Q_PROPERTY( QString name READ name WRITE setName NOTIFY nameChanged )
      Q_PROPERTY( QString colour READ colour WRITE setColour NOTIFY colourChanged )
  
  
  public:
      DataObject( QObject * parent = 0 );
      DataObject( const QString &amp;_name, const QString &amp;_color, QObject * parent=0 );
  
      QString name() const;
      void setName(const QString &amp;);
  
      QString colour() const;
      void setColour(const QString &amp;);
  
  signals:
      void nameChanged();
      void colourChanged();
  
  
  private:
      QString m_name;
      QString m_colour;
  };
  
  
  #endif // DATAOBJECT_H
  </code></pre>
  
  <p>dataobject.cpp</p>
  
  <pre><code>#include "dataobject.h"
  #include &lt;QDebug&gt;
  
  DataObject::DataObject( QObject * parent )
      : QObject( parent )
  {
      qDebug() &lt;&lt; "DataObject::DataObject() has been called.\n";
  
  }
  
  DataObject::DataObject( const QString &amp;_name, const QString &amp;_colour, QObject * parent )
      : QObject( parent )
      , m_name( _name )
      , m_colour( _colour )
  {
      qDebug() &lt;&lt; "DataObject::DataObject(name, color) has been called.\n";
  
  }
  
  
  QString DataObject::name() const {
      qDebug() &lt;&lt; "name() has been called.\n";
      return m_name;
  }
  
  void DataObject::setName(const QString &amp;name) {
      qDebug() &lt;&lt; "setName has been called.\n";
      if ( name != m_name ) {
          m_name = name;
          emit nameChanged();
      }
  }
  
  QString DataObject::colour() const {
      qDebug() &lt;&lt; "colour() has been called.\n";
      return m_colour;
  }
  
  void DataObject::setColour(const QString &amp;colour) {
      qDebug() &lt;&lt; "setColour has been called.\n";
      if ( colour != m_colour ) {
          m_colour = colour;
          emit colourChanged();
      }
  }
  </code></pre>
  
  <p>And then I add it to the QML context:</p>
  
  <pre><code>#include &lt;QApplication&gt;
  #include &lt;QDialog&gt;
  #include &lt;QDeclarativeView&gt;
  #include &lt;QDeclarativeContext&gt;
  #include &lt;QLayout&gt;
  #include &lt;QDir&gt;
  #include "qmlapplicationviewer.h"
  #include "dataobject.h"
  
  int main(int argc, char *argv[])
  {
      QApplication app(argc, argv);
  
      QList&lt;QObject*&gt; dataList;
      dataList.append( new DataObject( "c++ entry1 (red)", "red" ) );
      dataList.append( new DataObject( "c++ entry2 (orange)", "orange" ) );
      dataList.append( new DataObject( "c++ entry3 (yellow)", "yellow" ) );
      dataList.append( new DataObject( "c++ entry4 (green)", "green" ) );
      dataList.append( new DataObject( "c++ entry5 (blue)", "blue" ) );
      dataList.append( new DataObject( "c++ entry6 (purple)", "purple" ) );
  
      QmlApplicationViewer viewer;
      viewer.rootContext()-&gt;setContextProperty( "cppModel", QVariant::fromValue(dataList) );
      viewer.setOrientation(QmlApplicationViewer::ScreenOrientationAuto);
  #if defined( Q_OS_MAC )
      viewer.setMainQmlFile("../Resources/qml/main.qml");
  #elif defined( Q_OS_WIN32 )
      viewer.setMainQmlFile("qml/main.qml");
  #else
  #error - unknown platform
  #endif
      viewer.showExpanded();
  
      return app.exec();
  }
  </code></pre>
  
  <p>And finally, in the QML, I add this C++ model to the ListView:</p>
  
  <pre><code>import QtQuick 1.0
  
  Rectangle {
      width: 200; height: 200
  
      ListModel {
          id: qmlModel
          ListElement { name: "qml entry1 (red)"; colour: "red" }
          ListElement { name: "qml entry2 (orange)"; colour: "orange" }
          ListElement { name: "qml entry3 (yellow)"; colour: "yellow" }
          ListElement { name: "qml entry4 (green)"; colour: "green" }
          ListElement { name: "qml entry5 (blue)"; colour: "blue" }
          ListElement { name: "qml entry6 (purple)"; colour: "purple" }
      }
  
  
             ListView {
  
                 id: list_view
  
                 anchors.fill: parent
                 //model: qmlModel
                 model: cppModel
                 delegate: Rectangle {
                     height: 20
                     width: 200
                     color: colour
                     Text { text: name }
  
                 }
             }
  
  }
  </code></pre>
  
  <p>Once again, this works just fine - a dialog with text against colored backgrounds arranged in bands appears. Displaying a ListView backed by a C++ model seems to work every bit as well as displaying a ListView backed by a QML ListModel. </p>
  
  <p><img src="https://i.stack.imgur.com/YUjqX.png" alt="ListView displaying a QList in C++"></p>
  
  <p>What I'd like to get working is a C++ model backing a PathView like this:</p>
  
  <pre><code>import QtQuick 1.0
  
  Rectangle {
      width: 200; height: 200
  
      ListModel {
          id: qmlModel
          ListElement { name: "qml entry1 (red)"; colour: "red" }
          ListElement { name: "qml entry2 (orange)"; colour: "orange" }
          ListElement { name: "qml entry3 (yellow)"; colour: "yellow" }
          ListElement { name: "qml entry4 (green)"; colour: "green" }
          ListElement { name: "qml entry5 (blue)"; colour: "blue" }
          ListElement { name: "qml entry6 (purple)"; colour: "purple" }
      }
  
  
  //    ListView {
  
  //       id: list_view
  
  //       anchors.fill: parent
  //       model: qmlModel
  //       //model: cppModel
  //       delegate: Rectangle {
  //           height: 20
  //           width: 200
  //           color: colour
  //           Text { text: name }
  
  //       }
  //    }
  
      PathView {
          id: my_path_view
  
          anchors.fill: parent
  
          Keys.onRightPressed: if (!moving &amp;&amp; interactive) incrementCurrentIndex()
          Keys.onLeftPressed: if (!moving &amp;&amp; interactive) decrementCurrentIndex()
  
          flickDeceleration: 500
  
          preferredHighlightBegin: 0.5
          preferredHighlightEnd: 0.5
          focus: true
          interactive: true
          //model: qmlModel
          model: cppModel
  
          delegate: Rectangle {
              width: 100
              height: 100
              color: colour
              Text {
                  anchors.centerIn: parent
                  text: name
              }
          }
  
  
          path: Path {
              startX: - my_path_view.width * my_path_view.model.count / 2 + my_path_view.width / 2
              startY: my_path_view.height / 2
              PathLine {
                  x: my_path_view.width * my_path_view.model.count / 2 + my_path_view.width / 2
                  y: my_path_view.height / 2
              }
          }
      }
  }
  </code></pre>
  
  <p>This DOESN'T work. What I see is the colored rectangles, but they can't be interacted with with the mouse and they aren't centered in the qmlviewer dialog. </p>
  
  <p><img src="https://i.stack.imgur.com/iX733.png" alt="C++ QList in a QML PathView"></p>
  
  <p>And on the debug console I see this:</p>
  
  <pre><code>QDeclarativeDebugServer: Waiting for connection on port 3768...
  QDeclarativeDebugServer: Connection established
  QPainterPath::lineTo: Adding point where x or y is NaN or Inf, ignoring call
  QPainterPath::lineTo: Adding point where x or y is NaN or Inf, ignoring call
  colour() has been called.
  
  name() has been called.
  
  colour() has been called.
  
  name() has been called.
  
  colour() has been called.
  
  name() has been called.
  
  colour() has been called.
  
  name() has been called.
  
  colour() has been called.
  
  name() has been called.
  
  colour() has been called.
  
  name() has been called.
  
  QPainterPath::lineTo: Adding point where x or y is NaN or Inf, ignoring call
  QPainterPath::lineTo: Adding point where x or y is NaN or Inf, ignoring call
  QPainterPath::lineTo: Adding point where x or y is NaN or Inf, ignoring call
  QPainterPath::lineTo: Adding point where x or y is NaN or Inf, ignoring call
  QPainterPath::lineTo: Adding point where x or y is NaN or Inf, ignoring call
  </code></pre>
  
  <p>It seems like a QList has a basic shape that's close enough to a QML ListModel/ListItem collection for a ListView to display, but not close enough for a PathView to display. </p>
  
  <p>Does anyone have any idea what might be going wrong? Unfortunately the QML class documentation isn't really put together with the goal of writing conformant C++ stand-ins. For example, the PathView object documentation at <a href="http://qt-project.org/doc/qt-4.8/qml-pathview.html" rel="nofollow noreferrer">http://qt-project.org/doc/qt-4.8/qml-pathview.html</a> doesn't say what properties its model needs to support. Moreover, the ListModel documentation isn't definitive - it doesn't state exactly what properties the ListModel supports and there's no clear documentation on how precisely a QList satisfies those requirements and how it doesn't.</p>
  
  <p>UPDATE: I've tried this with Qt 5 on Windows, and I'm still having the same problem.</p>