title: Creating and accessing elements of a QVector of pointers to QObjects
body: <p>I try adding pointers to objects to a QVector but when I access the QVector later it appears to be empty.</p>
  
  <p>Here is a stripped down version of the code with the problem where the Villain class has a QVector of Minion*.</p>
  
  <pre><code>void Widget::mainFunction()
  {
      int totalMinions = 5;
      for(int i = 0; i &lt; totalMinions; i++)
      {
          evilOrganisation.getVillain().addMinion();
      }
  
      for(int i = 0; i &lt; totalMinions; i++)
      {
          qDebug() &lt;&lt; "Minion " &lt;&lt; i &lt;&lt; " Has " &lt;&lt; evilOrganisation.getVillain().getMinion(i)-&gt;getNumItems()&lt;&lt; " items";
      }
  }
  </code></pre>
  
  <p>The ultimate goal is to get the number of items from each minion</p>
  
  <pre><code>#include &lt;QObject&gt;
  #include "villain.h"
  
  class EvilOrganisation : public QObject
  {
      Q_OBJECT
  public:
      explicit EvilOrganisation(QObject *parent = 0);
  
      Villain getVillain(){return villain;}
  
  private:
      Villain villain;
  
  }; 
  </code></pre>
  
  <p>__</p>
  
  <pre><code>#include &lt;QVector&gt;
  #include "minion.h"
  
  class Villain
  {
  public:
      Villain();
  
      void addMinion(){minions.append(new Minion());} 
  
      Minion* getMinion(int i){return minions.at(i);} 
  
  private:
      QVector&lt;Minion*&gt; minions;
  };
  </code></pre>
  
  <p>addMinion() successfully appends 1 Minion* but the next time the function is called minions is empty.</p>
  
  <p>getMinion(int i) crashes the program because minions.at(i) doesn't exist.
  Where the error message is - ASSERT failure in QVector::at: "index out of range"</p>
  
  <pre><code>#include &lt;QObject&gt;
  
  class Minion : public QObject
  {
      Q_OBJECT
  public:
      explicit Minion(QObject *parent = 0);
  
      int getNumItems(){return items;}
  
  private:
      int items = 1;
  };
  </code></pre>
  
  <p>So why does minions.append(new Minion()) appear to work but the minions QVector appear empty every time I try to access it?</p>