title: how to sort QML TableView in QtQuick 2?
body: <p>I want to implement a sortable TableView with custom roles with Qt 5.1. But I don't know what to do more to make it sorted when user clicked on header.</p>
  
  <p>in my Qt .pro file, I added:</p>
  
  <pre><code>!android: !ios: !blackberry: qtHaveModule(widgets): QT += widgets
  </code></pre>
  
  <p>in main.cpp, I used <code>QtWidgets/QApplication</code> as global app instance, and used qmlRegisterType for my new model class(see below):</p>
  
  <pre><code>qmlRegisterType&lt;PositionModel&gt;("MyDataModule", 1, 0, "PositionModel");
  </code></pre>
  
  <p>PositionModel is declared following:</p>
  
  <pre><code>class PositionModel : public QAbstractTableModel
  {
      Q_OBJECT
  public:
      enum PositionRoles {
          CustomRol1 = Qt::UserRole + 1,
          CustomRow2,
          PositionRoleMaxPlus1
      };
  
      explicit PositionModel(QObject *parent = 0);
  
      int rowCount(const QModelIndex &amp;parent = QModelIndex()) const;
      int columnCount(const QModelIndex &amp;parent = QModelIndex()) const;
  
      QVariant data(const QModelIndex &amp;index, int role = Qt::DisplayRole) const;
      QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const;
  
      bool setData(const QModelIndex &amp;index, const QVariant &amp;value, int role);
  
      void setData(QList&lt;QObject*&gt; data);
  
  protected:
      QHash&lt;int, QByteArray&gt; roleNames() const;
  
  private:
      QList&lt;QObject*&gt; m_data;
  
  signals:
  
  public slots:
  
  };
  </code></pre>
  
  <p>and implemented as following:</p>
  
  <pre><code>PositionModel::PositionModel(QObject *parent) :
      QAbstractTableModel(parent)
  {
  }
  
  QHash&lt;int, QByteArray&gt;
  PositionModel::roleNames() const {
      QHash&lt;int, QByteArray&gt; roles;
      roles[CustomRole1] = "CustomRole1";
      roles[CustomRole2] = "CustomRole2";
      return roles;
  }
  
  
  int
  PositionModel::rowCount(
          const QModelIndex &amp;parent) const
  {
      return m_data.size();
  }
  
  int
  PositionModel::columnCount(
          const QModelIndex &amp;parent) const
  {
      return 2; // two custom rows
  }
  
  QVariant
  PositionModel::data(
          const QModelIndex &amp;index, int role) const
  {
      if (!index.isValid() || (role != Qt::DisplayRole &amp;&amp; role&lt;Qt::UserRole))
          return QVariant();
  
  
      MyObject* myobj=dynamic_cast&lt;MyObject*&gt;(m_data[index.row()]);
      switch(role){
      case CustomRole1:
      {
          return QVariant(myobj-&gt;attribute1());
          break;
      }
      case CustomRole2:
      {
          return QVariant(myobj-&gt;attribute2());
          break;
      }
      default:
      {
          break;
      }
  
      }
  
      return QVariant();
  }
  
  QVariant
  PositionModel::headerData(
          int section, Qt::Orientation orientation, int role) const
  {
      if (role != Qt::DisplayRole &amp;&amp; role&lt;Qt::UserRole)
          return QVariant();
  
  
      if (orientation == Qt::Horizontal)
      {
          switch (section)
          {
          case 0:
              return tr("CustomRole1");
          case 1:
              return tr("CustomRole2");
      }
      return QVariant();
  }
  
  bool 
  PositionModel::setData(const QModelIndex &amp;index, const QVariant &amp;value, int role)
  {
      return false;
  }
  
  void
  PositionModel::setData(QList&lt;QObject*&gt; data)
  {
      m_data=data;
  }
  </code></pre>
  
  <p>and my .qml file is:</p>
  
  <pre><code>import QtQuick 2.1
  import QtQuick.Controls 1.0
  import StockModule 2.0
  
  TableView {
      id: positionlisttable
      sortIndicatorVisible: true
      model: mydata.currentPosition // here "mydata" is an instance of MyData class and is set as a context property to QML. "currentPosition" is a Q_PROPERTY in MyData class, and its return type is QAbstractItemData*.
  
      TableViewColumn {
          role: "CustomRole1"
          title: "CustomRole1"
          width: 80
      }
      TableViewColumn {
          role: "CustomRole2"
          title: "CustomRole2"
          width: 80
      }
  }
  </code></pre>
  
  <p>and in MyData class:</p>
  
  <pre><code>/*QAbstractItemModel*/QObject*
  MyData::currentPosition() const
  {
      PositionModel* newmd=new PositionModel();
      newmd-&gt;setData(m_d-&gt;data());
      QSortFilterProxyModel *filterModel = new QSortFilterProxyModel();
      filterModel-&gt;setSourceModel(newmd);
      filterModel-&gt;setSortRole(PositionModel::CustomRole1);
      filterModel-&gt;setDynamicSortFilter(true);
      return filterModel;
  }
  </code></pre>
  
  <p>so now what should I continue to do to make it sortable? </p>