Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The <code>QObject</code> implementation of the <a href="http://en.wikipedia.org/wiki/Composite_pattern">Composite Design Pattern</a> has been tried and tested through the many versions of Qt. </p> <p>The pattern requires that the composite object takes ownership of the children so, as long as the parenting has been done, you can be assured that the child <code>QObjects</code> will be destroyed when the parent is destroyed.</p> <p>Standard practice is to create child objects in heap memory and parent them immediately. If you don't parent immediately, you can explicitly parent using the <code>setParent()</code> function, or else parenting will be done automatically when you add the widget to a parent widget, either using <code>addWidget()</code> or <code>addLayout()</code>. </p> <p><code>QLayout</code> objects are size and layout managers of other <code>QLayouts</code> and of <code>QWidgets</code>. They don't own the objects they manage. The parent is actually the <code>QWidget</code> that the <code>QLayout</code> is the child of. </p> <p>You have a choice to create the root parent in stack memory or in heap memory. </p> <p>If you feel more comfortable with smart pointers, there are two classes that are specifically for <code>QObjects</code>: <a href="http://qt-project.org/doc/qt-5.0/qtcore/qpointer.html">QPointer</a> and <a href="http://qt-project.org/doc/qt-5.0/qtcore/qsharedpointer.html">QSharedPointer</a>. Each has their pros and cons.</p> <pre><code>#include &lt;QApplication&gt; #include &lt;QLabel&gt; #include &lt;QHBoxLayout&gt; #include &lt;QWidget&gt; int main(int argc, char **argv) { QApplication app(argc, argv); QWidget widget; // Root parent so can create as a auto-deleting object on the stack QHBoxLayout *layout = new QHBoxLayout(&amp;widget); // Create on the heap and parent immediately QLabel *label = new QLabel("label", &amp;widget); // Create on the heap and parent immediately layout-&gt;addWidget(label); // widget remains label's parent widget.setLayout(layout); // widget is changed to layout's parent if necessary, as well // as any widgets that layout manages widget.show(); return app.exec(); // layout and label are destroyed when widget is destroyed } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload