title: How to sort a QTableView by a column?
body: <p>I am using the <code>QTableView</code> to display a QAbstractTableModel:</p>
  
  <pre><code>#include &lt;QtGui/QApplication&gt;
  #include &lt;QAbstractTableModel&gt;
  #include &lt;QTableView&gt;
  
  class TestModel : public QAbstractTableModel
  {
  public:
      int rowCount(const QModelIndex &amp;parent = QModelIndex()) const
      {
          return 2;
      }
      int columnCount(const QModelIndex &amp;parent = QModelIndex()) const
      {
          return 2;
      }
      QVariant data(const QModelIndex &amp;index, int role) const
      {
          switch (role)
          {
          case Qt::DisplayRole:
          {
              return 4 - index.row() + index.column();
          }
          }
          return QVariant();
      }
  };
  
  int main(int argc, char *argv[])
  {
      QApplication a(argc, argv);
  
      QTableView table;
      TestModel model;
      table.setModel(&amp;model);
      table.setSortingEnabled(true);
      table.sortByColumn(0, Qt::AscendingOrder);
      table.reset();
      table.show();
  
      return a.exec();
  }
  </code></pre>
  
  <p><img src="https://i.stack.imgur.com/moz8l.png" alt="The displayed widget"></p>
  
  <p>The problem is that the result is exactly the same when I use:</p>
  
  <pre><code>table.sortByColumn(0, Qt::AscendingOrder);
  </code></pre>
  
  <p>or</p>
  
  <pre><code>table.sortByColumn(0, Qt::DescendingOrder);
  </code></pre>
  
  <p>or</p>
  
  <pre><code>table.sortByColumn(1, Qt::AscendingOrder);
  </code></pre>
  
  <p>or</p>
  
  <pre><code>table.sortByColumn(1, Qt::DescendingOrder);
  </code></pre>
  
  <p>What am I doing wrong?</p>