title: sourceModel()->createIndex() in QAbstractProxyModel sub-class
body: <p><strong>I am attempting to create a proxy model that dynamically maps items from source model.</strong></p>
  
  <p>Following the implementation of <code>QIdentityProxyModel</code> with the intention to go from there I have discovered that it is in fact impossible to replicate it by examining the 4 core functions: </p>
  
  <pre><code>mapFromSource()
  mapToSource()
  index()
  parent()
  </code></pre>
  
  <p>Consider this based on <code>QIdentityProxyModel</code>:</p>
  
  <p><em>mapFromSource()</em></p>
  
  <pre><code>QModelIndex ProxyModel::mapFromSource(const QModelIndex &amp;sourceIndex) const
  {
      if(sourceIndex.isValid())
          return createIndex(sourceIndex.row(), sourceIndex.column(), sourceIndex.internalPointer());
      else
          return QModelIndex();
  }
  </code></pre>
  
  <p><em>mapToSource()</em></p>
  
  <pre><code>QModelIndex ProxyModel::mapToSource(const QModelIndex &amp;proxyIndex) const
  {
      if(proxyIndex.isValid())
          return sourceModel()-&gt;createIndex(proxyIndex.row(), proxyIndex.column(), proxyIndex.internalPointer());
      else
          return QModelIndex();
  }
  </code></pre>
  
  <p><em>index()</em></p>
  
  <pre><code>QModelIndex ProxyModel::index(int row, int column, const QModelIndex &amp;parent) const
  {
      const QModelIndex sourceParent = mapToSource(parent);
      const QModelIndex sourceIndex = sourceModel()-&gt;index(row, column, sourceParent);
      return mapFromSource(sourceIndex);
  }
  </code></pre>
  
  <p><em>parent()</em></p>
  
  <pre><code>QModelIndex ProxyModel::parent(const QModelIndex &amp;index) const
  {
      const QModelIndex sourceIndex = mapToSource(index);
      const QModelIndex sourceParent = sourceIndex.parent();
      return mapFromSource(sourceParent);
  }
  </code></pre>
  
  <p><strong>THE ISSUE</strong></p>
  
  <p>Problem lies in <code>mapToSource()</code> line </p>
  
  <pre><code>return sourceModel()-&gt;createIndex(proxyIndex.row(), proxyIndex.column(), proxyIndex.internalPointer());
  </code></pre>
  
  <p>The <code>QAbstractItemModel::createIndex</code> is protected function and cannot be used unless the caller is declared friend. That is obviously not an option without modifying the <code>QAbstractItemModel</code> class directly. The only alternative is to use regular <code>QAbstractItemModel::index</code> but that requires parent in form of <code>QModelIndex</code> as one of the arguments. However doing this:</p>
  
  <pre><code>return sourceModel()-&gt;index(proxyIndex.row(), proxyIndex.column(), proxyIndex.parent());
  </code></pre>
  
  <p>causes an infinite loop due to the fact that <code>parent()</code> function relies on <code>mapToSource()</code> <strong>and vice versa now</strong>.</p>
  
  <p>The alternative approach shown <a href="http://blog.qt.digia.com/blog/2012/01/12/qt-commercial-support-weekly-10-sorting-filtering-and-advanced-manipulation-with-proxy-models-2/" rel="nofollow">for example here</a> relies on the stored map of <code>QPersistentModelIndex</code> objects that are queried in the above mentioned functions. This approach while viable has several disadvantages:</p>
  
  <ul>
  <li>It requires a lots of extra memory to store the indexes</li>
  <li>All persistent indexes need to be updated by the source model upon every structure change (as opposed to on demand index lookup with dynamic models)</li>
  <li>Queries to the map may be slow when iterating over kyes of the map (this can be remedied by making two identical maps in reverse at the expense of yet more memory)</li>
  </ul>
  
  <p>Hence my questions: </p>
  
  <p><em>Is there another way to handle the hierarchical proxy model dynamically without relying on createIndex() and without running into infinite loop of function calls?</em></p>
  
  <p><em>Is it in fact necessary to create and maintain the structure of the proxy model in a storage vis-à-vis the source structure or is there a way to create a dynamic proxy model?</em></p>
  
  <p>Thanks!</p>