title: QProxyModel which will expose a Subtree only
body: <p>I need to expose a part of a tree model as a new tree model.</p>
  <p>So given a tree model like this:</p>
  <pre><code>- a
   |- b
     |- c
  - d
   |- e
  </code></pre>
  <p>i want to specify a root element, e.g. <code>a</code> which will yield a new model with the following structure</p>
  <pre><code>- b
   |- c
  </code></pre>
  <p>without actually copying the data.</p>
  <p>Therefore I was thinking that a proxy model is a natural fit for that. I'm aware that in my View classes I can set the RootIndex as desired. However I have a different use case, I want to use the part of the tree as a completion model for a <code>QCompleter</code>.</p>
  <p>I started by subclassing <code>QAbstractProxyModel</code>.</p>
  <pre><code>class SubTreeModel : public QAbstractProxyModel
  {
      Q_OBJECT
  public:
      SubTreeModel(QObject* parent = nullptr);
  
  public:
  
      QModelIndex RootIndex() const;
      void SetRootIndex(QModelIndex rootIndex);
      QModelIndex mapToSource(const QModelIndex &amp;proxyIndex) const override;
      QModelIndex mapFromSource(const QModelIndex &amp;sourceIndex) const override;
  QModelIndex index(int row, int column, const QModelIndex &amp;parent) const override;
      QModelIndex parent(const QModelIndex &amp;child) const override;
      int rowCount(const QModelIndex &amp;parent) const override;
      int columnCount(const QModelIndex &amp;parent) const override;
  
  private:
      QModelIndex m_rootIndex;
  };
  </code></pre>
  <p>The <code>m_rootIndex</code> then should be an index into the source model, which acts as the root of the Proxy model.</p>
  <p>In my understanding the most important thing is to get <code>mapToSource</code> and <code>mapFromSource</code> right and then just use them in the remaining functions. However I'm facing difficulties, this is what I came up with so far e.g. for <code>mapToSource</code>:</p>
  <pre><code>QModelIndex SubTreeModel::mapToSource(const QModelIndex&amp; proxyIndex) const
  {
      if (not sourceModel() or not m_rootIndex.isValid() or not proxyIndex.isValid())
      {
          return QModelIndex();
      }
  
      /* accessing the root element of the proxy must be redirected to the correct root element in the
       * whole tree */
      if (not proxyIndex.parent().isValid())
      {
          return sourceModel()-&gt;index(proxyIndex.row(), proxyIndex.column(), m_rootIndex);
      }
      return sourceModel()-&gt;createIndex(proxyIndex.row(), proxyIndex.column(),
                                        proxyIndex.parent().internalPointer());
  }
  </code></pre>
  <p>The last <code>return</code> does not compile since <code>createIndex</code> is a protected member.
  In the implementation of e.g. <code>QSortFilterProxyModel</code> or <code>QIdentityProxyModel</code> they are able to call the <code>createIndex</code> function of the source model, by using D-Pointer tricks. Do i need to do the same (this feels insane!). Is there another way of solving this, which is more elegant? Or can it be done without using the <code>createIndex</code> function?</p>