title: Using a QAbstractTableModel with a Qml TableView only displays the 1st column
body: <p>I'm trying to use a (class derived from) QAbstractTableModel with a Qml TableView;</p>
  
  <p>However, only the 1st column is displayed.</p>
  
  <p>The reason is QVariant MyModel::data(const QModelIndex &amp;index, int role) isn't called for non-zero columns, but I don't understand why.</p>
  
  <p>A QTableView works fine however.</p>
  
  <p>I've made a separate, simple project that reproduce my issue:</p>
  
  <p>MyModel.h :</p>
  
  <pre><code>#ifndef MYMODEL_H
  #define MYMODEL_H
  
  #include &lt;QObject&gt;
  #include &lt;QAbstractTableModel&gt;
  #include &lt;QList&gt;
  #include &lt;QString&gt;
  #include &lt;QDebug&gt;
  
  struct SimpleData
  {
      QString m_one;
      qint32 m_two;
      qreal m_three;
  };
  
  class MyModel : public QAbstractTableModel
  {
      Q_OBJECT
  public:
      explicit MyModel();//MyData *the_data);
      int rowCount(const QModelIndex &amp; parent = QModelIndex()) const Q_DECL_OVERRIDE;
      int columnCount(const QModelIndex &amp;parent = QModelIndex()) const Q_DECL_OVERRIDE;
      QVariant data(const QModelIndex &amp;index, int role = Qt::DisplayRole) const Q_DECL_OVERRIDE;
  
      QHash&lt;int, QByteArray&gt; roleNames() const Q_DECL_OVERRIDE;
  signals:
  
  public slots:
      void theDataChanged();
  
  private:
     QList&lt;SimpleData&gt; m_the_data;
  
  };
  
  #endif // MYMODEL_H
  </code></pre>
  
  <p>mymodel.cpp:
      #include "mymodel.h"</p>
  
  <pre><code>MyModel::MyModel() : QAbstractTableModel(0)
  {
      m_the_data &lt;&lt; SimpleData{"Alpha", 10, 100.0}
                 &lt;&lt; SimpleData{"Beta", 20, 200.0}
                 &lt;&lt; SimpleData{"Gamma", 30, 300.0}
                 &lt;&lt; SimpleData{"Delta", 40, 400.0};
  }
  
  int MyModel::rowCount(const QModelIndex &amp;parent) const
  {
      Q_UNUSED(parent)
      return m_the_data.size();
  }
  
  int MyModel::columnCount(const QModelIndex &amp;parent) const
  {
      Q_UNUSED(parent)
      return 3;
  }
  
  QVariant MyModel::data(const QModelIndex &amp;index, int role) const
  {
      // Check DisplayRole
      if(role != Qt::DisplayRole)
      {
          return QVariant();
      }
  
      // Check boudaries
      if(index.column() &lt; 0 ||
              columnCount() &lt;= index.column() ||
              index.row() &lt; 0 ||
              rowCount() &lt;= index.row())
      {
          qDebug() &lt;&lt; "Warning: " &lt;&lt; index.row() &lt;&lt; ", " &lt;&lt; index.column();
          return QVariant();
      }
  
      // Nominal case
       qDebug() &lt;&lt; "MyModel::data: " &lt;&lt; index.column() &lt;&lt; "; " &lt;&lt; index.row();
      switch(index.column())
      {
          case 0:
              return m_the_data[index.row()].m_one;
          case 1:
              return  m_the_data[index.row()].m_two;
          case 2:
              return  m_the_data[index.row()].m_three;
          default:
              qDebug() &lt;&lt; "Not supposed to happen";
              return QVariant();
      }
  }
  
  QHash&lt;int, QByteArray&gt; MyModel::roleNames() const
  {
      QHash&lt;int, QByteArray&gt; roles;
      roles[0] = "one";
      roles[1] = "two";
      roles[2] = "three";
      return roles;
  
  }
  
  void MyModel::theDataChanged()
  {
      //TODO
  }
  </code></pre>
  
  <p>main.qml:</p>
  
  <pre><code>import QtQuick 2.1
  import QtQuick.Controls 1.0
  
  ApplicationWindow {
      title: qsTr("Hello World")
      width: 640
      height: 480
  
      menuBar: MenuBar {
          Menu {
              title: qsTr("File")
              MenuItem {
                  text: qsTr("Exit")
                  onTriggered: Qt.quit();
              }
          }
      }
  TableView {
  
      anchors.fill: parent
  
      TableViewColumn {title: "1"; role: "one"; width: 70 }
      TableViewColumn {title: "2"; role: "two"; width: 70   }
      TableViewColumn {title: "3"; role: "three"; width: 70 }
  
      model: theModel
  
  }
  
  }
  </code></pre>
  
  <p>main.cpp:</p>
  
  <pre><code>#include &lt;QtQml&gt;
  #include &lt;QQmlApplicationEngine&gt;
  #include &lt;QApplication&gt;
  #include &lt;QQuickWindow&gt;
  #include &lt;QTableView&gt;
  
  #include "mymodel.h"
  
  int main(int argc, char *argv[])
  {
      // Application :
      QApplication app(argc, argv);
  
      // Data stuff :
      //MyData data(&amp;app);
      MyModel model;
  
  
  
   // UI :
      QQmlApplicationEngine engine;
      engine.rootContext()-&gt;setContextProperty("theModel", &amp;model);
  
      engine.load(QUrl("qrc:/qml/main.qml"));
      QList&lt;QObject*&gt; temp = engine.rootObjects();
      QObject *topLevel = temp.value(0);
      QQuickWindow *window = qobject_cast&lt;QQuickWindow *&gt;(topLevel);
      if ( !window ) {
          qWarning("Error: Your root item has to be a Window.");
          return -1;
  
  
   }
  
      // Display the main.qml, which show the model:
      window-&gt;show();
  
      // Same, using a QTableView:
      QTableView view;;
      view.setModel(&amp;model);
      view.show();
  
      return app.exec();
  }
  </code></pre>
  
  <p>qDebug output about the TableView (row, then column):</p>
  
  <pre><code>MyModel::data:  0 ;  0 
  MyModel::data:  0 ;  0 
  MyModel::data:  0 ;  1 
  MyModel::data:  0 ;  1 
  MyModel::data:  0 ;  2 
  MyModel::data:  0 ;  2 
  MyModel::data:  0 ;  3 
  MyModel::data:  0 ;  3 
  </code></pre>
  
  <p>qDebug output about the QTableVierw:</p>
  
  <pre><code>MyModel::data:  0 ;  0 
  MyModel::data:  0 ;  0 
  MyModel::data:  0 ;  0 
  MyModel::data:  1 ;  0 
  MyModel::data:  2 ;  0 
  MyModel::data:  0 ;  1 
  MyModel::data:  1 ;  1 
  MyModel::data:  2 ;  1 
  MyModel::data:  0 ;  2 
  MyModel::data:  1 ;  2 
  MyModel::data:  2 ;  2 
  MyModel::data:  0 ;  3 
  MyModel::data:  1 ;  3 
  MyModel::data:  2 ;  3 
  </code></pre>
  
  <p>Notes / stuff I tried:</p>
  
  <ul>
  <li><p>In the code I gave, I could use a QQmlListProperty; however my actual code is more complex</p>
  
  <ul>
  <li>the data is actually queried,</li>
  <li>I don't have a SimpleData class</li>
  <li>I'm basically using the QAbstractTableModel  as a proxy.
  As a consequence, I can't switch to a QQmlListProperty (or equivalent)</li>
  </ul></li>
  <li><p>there was a project to check models, ModelTest, however it does not work with Qt 5.2.</p></li>
  <li><p>the type of data (QString, qreal) isn't an issue: swapping still display only the 1st column.</p></li>
  </ul>
  
  <p>Relevant links:</p>
  
  <p><a href="http://qt-project.org/doc/qt-5/QAbstractTableModel.html" rel="noreferrer">http://qt-project.org/doc/qt-5/QAbstractTableModel.html</a></p>
  
  <p><a href="http://qt-project.org/doc/qt-5/qtquick-modelviewsdata-cppmodels.html#qabstractitemmodel" rel="noreferrer">http://qt-project.org/doc/qt-5/qtquick-modelviewsdata-cppmodels.html#qabstractitemmodel</a></p>
  
  <p>Thanks in advance!</p>
  
  <p>Specs: Windows 7, Qt 5.2, Mingw 4.8, Qt Creator 3.0</p>