title: When can a QList NOT be replaced by a QVector
body: <p>After <a href="https://stackoverflow.com/questions/6602036/qvector-vs-qlist">reading</a> that the <code>QVector</code> should be the first choice (over <code>QList</code>) for containers in Qt, I merrily changed some of the <code>QList</code> in my code to <code>QVector</code> and went on with my business, pretty much forgetting about those changes.... until I started getting strange crashes which did not happen before.</p>
  <p>The crashes happened in my local copy of the <code>QQuickTreeModelAdaptor1</code> class implementation, that I renamed <code>TreeModelAdaptor</code>, which is used to display tree-like C++ models in a QML view.</p>
  <p>In the header, I replaced:</p>
  <pre><code>    QList&lt;TreeItem&gt; m_items;
      QList&lt;TreeItem *&gt; m_itemsToExpand;
  </code></pre>
  <p>by</p>
  <pre><code>    QVector&lt;TreeItem&gt; m_items;
      QVector&lt;TreeItem *&gt; m_itemsToExpand;
  </code></pre>
  <p>and in the method <code>TreeModelAdaptor::modelRowsAboutToBeMoved()</code></p>
  <pre><code>const QList&lt;TreeItem&gt; &amp;buffer = m_items.mid(startIndex, totalMovedCount);
  </code></pre>
  <p>by:</p>
  <pre><code>const QVector&lt;TreeItem&gt; &amp;buffer = m_items.mid(startIndex, totalMovedCount);
  </code></pre>
  <p>I couldn't figure out why the crashes happened, and then I read the following note on the <a href="https://doc.qt.io/qt-5/qvector.html" rel="nofollow noreferrer">QVector doc page</a>:</p>
  <pre><code>Note: Iterators into a QLinkedList and references into heap-allocating QLists 
  remain valid as long as the referenced items remain in the container. This is 
  not true for iterators and references into a QVector and non-heap-allocating QLists.
  </code></pre>
  <p>That's when I remembered the changes I made, and reverted to <code>QList</code>. After that, the crashes seemed to be gone.</p>
  <p>Can the crashes I experienced be understood in light of the above Note?
  The <a href="https://code.qt.io/cgit/qt/qtquickcontrols.git/tree/src/controls/Private/qquicktreemodeladaptor.cpp?h=dev" rel="nofollow noreferrer">source code</a> is a bit too much to post here, but I would appreciate it if someone could point out where and how in the code the above Note would be pertinent?</p>
  <p>Also, in light of the above, what about the <a href="https://www.qt.io/blog/qlist-changes-in-qt-6" rel="nofollow noreferrer">unification between <code>QList</code> and <code>QVector</code></a> that has been  introduced in Qt6, since it says:</p>
  <pre><code>QVector is used as the underlying implementation.
  </code></pre>
  <p>Does that mean that above code would crash in Qt6 regardless?</p>
  <p>EDIT: As @jarman rightfully suggested, the crashes happen at the following <code>ASSERT</code>, <em>almost</em> randomly. If I expand an item, then do some other expansions on other branches, etc, and toggle expansion on/off a while like that is usually when it happens.</p>
  <pre><code>void TreeModelAdaptor::expandPendingRows(bool doInsertRows)
  {
      while (!m_itemsToExpand.isEmpty()) {
          TreeItem *item = m_itemsToExpand.takeFirst();
  
          Q_ASSERT(item-&gt;expanded);
  
          const QModelIndex &amp;index = item-&gt;index;
          int childrenCount = m_model-&gt;rowCount(index);
          if (childrenCount == 0) {
              if (m_model-&gt;hasChildren(index) &amp;&amp; m_model-&gt;canFetchMore(index))
                  m_model-&gt;fetchMore(index);
              continue;
          }
          // TODO Pre-compute the total number of items made visible
          // so that we only call a single beginInsertRows()/endInsertRows()
          // pair per expansion (same as we do for collapsing).
          showModelChildItems(*item, 0, childrenCount - 1, doInsertRows, false);
      }
  }
  </code></pre>