title: Looping over all model indexes and their children causes stack overflow error
body: <p>I created following functions to loop over all indexes in a table or a tree and find the one that contains required string:</p>
  
  <pre><code>#include &lt;QModelIndex&gt;
  #include &lt;QAbstractItemModel&gt;
  #include &lt;QAbstractItemView&gt;
  
  QModelIndex findIndexByString(const QAbstractItemModel* model, const QString&amp; text,  const QModelIndex&amp; parent = QModelIndex());
  QModelIndex findIndexByString(const QModelIndex&amp; index, const QString&amp; text) {
      // First check this particular index
      if(index.data().toString() == text)
          return index;
      // Check if it has children and loop
      const QAbstractItemModel* model = index.model();
      if(model==nullptr)
          return QModelIndex();
      if (!model-&gt;hasChildren(index))
          return QModelIndex();
      // STACK OVERFLOW HERE (model and index keep repeating)
      return findIndexByString(model, text, index);
  }
  QModelIndex findIndexByString(const QAbstractItemModel* model, const QString&amp; text,  const QModelIndex&amp; parent) {
      int rows = model-&gt;rowCount(parent);
      int cols = model-&gt;columnCount(parent);
      for (int i = 0; i &lt; rows; ++i)
        for (int j = 0; j &lt; cols; ++j) {
          QModelIndex index_child = findIndexByString(model-&gt;index(i, j, parent), text);
          if(index_child.isValid())
              return index_child;
        }
      return QModelIndex();
  }
  QModelIndex findIndexByString(const QAbstractItemView* view, const QString&amp; text) {
      //const QModelIndex root = view-&gt;rootIndex();
      const QAbstractItemModel* model = view-&gt;model();
      return findIndexByString(model, text);
  }
  </code></pre>
  
  <p>This code is self contained and doesn't have any dependencies outside Qt library.</p>
  
  <p>I encountered stack overflow error when looping over some model in foreign code.</p>
  
  <p>I am trying to create generic correct algorithm to do this. Is the algorithm above correct?</p>