title: Qt findChild returning 0
body: <p>I'm currently working on a nice way to use a GUI to modify object contents at run time, project uses Qt.</p>
  
  <p>So I thought of passing a <code>QLayout</code> to the objects, to let them create their own GUI. As POC, I created this ("display" is the name of the <code>QVBoxLayout*</code> parameter):</p>
  
  <pre><code>QPushButton* button = new QPushButton();
  button-&gt;setText("foo");
  button-&gt;setObjectName("bar");
  display-&gt;addWidget(button);
  </code></pre>
  
  <p>which works just as fine as expected. But I will need to read what the user typed into the GuI, so this is what I did next:</p>
  
  <pre><code>QPushButton *button2 = display-&gt;findChild&lt;QPushButton *&gt;();
  if(button2)
      std::cout &lt;&lt; button2-&gt;objectName().toStdString() &lt;&lt; std::endl;
  </code></pre>
  
  <p>here nothing is put out - if() statement is false!</p>
  
  <pre><code>QList&lt;QWidget *&gt; widgets = display-&gt;findChildren&lt;QWidget *&gt;();
  foreach (QWidget* b, widgets) {
      std::cout &lt;&lt; b-&gt;objectName().toStdString() &lt;&lt; std::endl;
  }
  </code></pre>
  
  <p>similarly, the <code>widgets</code> list is empty.</p>
  
  <p>I tried looking through the full member list at: <a href="http://doc.qt.digia.com/qt/qvboxlayout-members.html" rel="nofollow noreferrer">http://doc.qt.digia.com/qt/qvboxlayout-members.html</a>, but findChild/findChildren seems like the best fit to my needs... </p>
  
  <p><strong><em>SOLUTION BELOW</em></strong></p>
  
  <p>This is how I handle it now:</p>
  
  <p>instead of passing a <code>QVBoxLayout</code> to the GUI creation, a <code>QWidget</code> should be passed, in following named "display" as above.</p>
  
  <pre><code>QVBoxLayout* layout = new QVBoxLayout();
  display-&gt;setLayout(layout);
  
  QPushButton* button = new QPushButton();
  button-&gt;setText("foo");
  button-&gt;setObjectName("bar");
  
  layout-&gt;addWidget(button);
  </code></pre>
  
  <p>now to <code>findChild</code> / <code>findChildren</code>:</p>
  
  <pre><code>QPushButton *button2 = display-&gt;findChild&lt;QPushButton *&gt;("bar");
  if(button2)
      std::cout &lt;&lt; button2-&gt;objectName().toStdString() &lt;&lt; std::endl;
  
  QList&lt;QWidget *&gt; widgets = display-&gt;findChildren&lt;QWidget *&gt;();
  foreach (QWidget* b, widgets) {
      std::cout &lt;&lt; b-&gt;objectName().toStdString() &lt;&lt; std::endl;
  }
  </code></pre>
  
  <p>both methods work for me as expected! (Plus, now the layout can be chosen freely by the GUI creation!)</p>