title: Qt: Can child objects be composed in their parent object?
body: <p>In Qt, can I embed child widgets in their parent via composition, or do I have to create them with <code>new</code>?</p>
  
  <pre><code>class MyWindow : public QMainWindow
  {
      ...
  private:
      QPushButton myButton;
  }
  
  MyWindow::MyWindow ()
   : mybutton("Do Something", this)
  {
     ...
  }
  </code></pre>
  
  <p>The documentation says that any object derived from <code>QObject</code> will automatically destroyed when its parent is destroyed; this implies a call to <code>delete</code>, whcih in the above example would crash.</p>
  
  <p>Do I have to use the following?</p>
  
  <pre><code>QPushButton* myButton;
  
  myButton = new QPushButton("Do Something", this);
  </code></pre>
  
  <p><strong>EDIT</strong></p>
  
  <p>The answers are quite diverse, and basically boil down to three possibilities:</p>
  
  <ul>
  <li><strong>Yes</strong>, composition is ok. Qt can figure out how the object was allocated and only <code>delete</code> heap-allocated objects (How does this work?)</li>
  <li><strong>Yes</strong>, composition is ok, but don't specify a parent, since the parent would otherwise call <code>delete</code> on the object (But won't a parent-less widget turn into a top-level window?)</li>
  <li><strong>No</strong>, widgets always have to be heap-allocated.</li>
  </ul>
  
  <p>Which one is correct?</p>