title: QTreeWidgetItem destructor and QTreeWidgetItemIterator validity
body: <p>Why does deleting a QTreeWidgetItem* automatically advance iterators associated with the same QTreeWidget?  This behavior does not appear to be <a href="http://doc.qt.io/qt-5/qtreewidgetitem.html#dtor.QTreeWidgetItem" rel="nofollow noreferrer">documented</a> anywhere that I looked.  I would expect that after deleting a QTreeWidget* the iterator would be unaffected (other than now pointing to an invalid value).  Instead it seems that the QTreeWidget destructor automatically advances all iterators for the QTreeWidget.</p>
  
  <p>Looking briefly into the qt sources, I think this works by having all iterators register themselves with the model when they are created.  Then in <a href="https://github.com/radekp/qt/blob/master/src/gui/itemviews/qtreewidget.cpp#L1477" rel="nofollow noreferrer">QTreeItem::~QTreeItem</a> the call to <a href="https://code.woboq.org/qt5/qtbase/src/widgets/itemviews/qtreewidget.cpp.html#_ZN10QTreeModel16beginRemoveItemsEP15QTreeWidgetItemii" rel="nofollow noreferrer">QTreeModel::beginRemoveItems()</a> calls <a href="https://code.woboq.org/qt5/qtbase/src/widgets/itemviews/qtreewidgetitemiterator.cpp.html#_ZN30QTreeWidgetItemIteratorPrivate19ensureValidIteratorEPK15QTreeWidgetItem" rel="nofollow noreferrer">QTreeWidgetItemIteratorPrivate::ensureValidIterator()</a> which advances all of the iterators.</p>
  
  <p>Other than directly inspecting all of the QT sources, how are you supposed to know this happens?  Do all QT iterators act this way?  I have more experience with STL iterators than with QT and perhaps that is why I found this so unexpected.</p>
  
  <p>The code below demonstrates this behavior:</p>
  
  <pre><code>    #include &lt;QTreeWidget&gt;
      #include &lt;QDebug&gt;
      #include &lt;QApplication&gt;
  
      int main(int argc, char **argv)
      {
          QApplication a( argc, argv );
          QTreeWidget * tw_1 = new QTreeWidget();
          QTreeWidget * tw_2 = new QTreeWidget();
          for(int i = 1; i &lt;=5; i++)
          {
                  tw_1-&gt;addTopLevelItem(new QTreeWidgetItem(QStringList(QString("Item %1").arg(i))));
                  tw_2-&gt;addTopLevelItem(new QTreeWidgetItem(QStringList(QString("Item %1").arg(i))));
          }
  
          {
          qDebug() &lt;&lt; "Elements in Tree Widget 1";
          QTreeWidgetItemIterator it(tw_1);
          while(*it)
          {
                  qDebug() &lt;&lt; "\t" &lt;&lt; (*it)-&gt;text(0);
                  ++it;
          }
  
          //Delete the items while iterating over the tree
          QTreeWidgetItemIterator ii(tw_1);
          while(*ii)
          {
                  delete *ii;
          }
  
          qDebug() &lt;&lt; "Elements remaining in Tree Widget 1";
          QTreeWidgetItemIterator itt(tw_1);
          while(*itt)
          {
                  qDebug() &lt;&lt; (*itt)-&gt;text(0);
                  ++itt;
          }
          }
  
          {
          qDebug() &lt;&lt; "Elements in Tree Widget 2";
          QTreeWidgetItemIterator it(tw_2);
          while(*it)
          {
                  qDebug() &lt;&lt; "\t" &lt;&lt; (*it)-&gt;text(0);
                  ++it;
          }
  
          //Delete the items while iterating over the tree
          QTreeWidgetItemIterator ii(tw_2);
          while(*ii)
          {
                  delete *ii;
                  ++ii;
          }
             qDebug() &lt;&lt; "Elements remaining in Tree Widget 2";
          QTreeWidgetItemIterator itt(tw_2);
          while(*itt)
          {                                                                                                                     
                  qDebug() &lt;&lt; (*itt)-&gt;text(0);                                                                                  
                  ++itt;                                                                                                        
          }                                                                                                                     
          }                                                                                                                     
  }  
  </code></pre>
  
  <p>This produces the output:</p>
  
  <pre><code>Elements in Tree Widget 1 
       "Item 1" 
       "Item 2" 
       "Item 3" 
       "Item 4" 
       "Item 5" 
  Elements remaining in Tree Widget 1 
  Elements in Tree Widget 2 
       "Item 1" 
       "Item 2" 
       "Item 3" 
       "Item 4" 
       "Item 5" 
  Elements remaining in Tree Widget 2 
       "Item 2" 
       "Item 4"  
  </code></pre>