On Mon, Feb 7, 2011 at 12:28 AM, D. Michael McIntyre <[email protected]> wrote: > On Sunday, February 06, 2011, Julie S wrote: > >> Since "this" is inherits from QGroupBox in this case, it appears Qt will >> take care of the details of deleting the QTimer. > > I think that's probably right. I'm not 100% sure myself. > --
I posted the following message in another list, perhaps it may be of help here too. ---------------------------------------------------------------------------------------------------------- Parenting is a very important issue in Qt and should be thoroughly understood by prospective Qt developers. http://doc.qt.nokia.com/4.7/objecttrees.html Every QObject in a Qt application can have a parent. Most prominently parenting deals with memory management. When a parent is destroyed, it destroys its children too in a cascade process. For instance, this is used when destroying a dialog to destroy all its widgets. If you don't set a parent for an object, explicitly or implicitly, you must take care of destroying it yourself. You can change a QObject parent in many ways: in its constructor, using the setParent method... It is usually done by certain Qt classes' methods that imply reparenting, like QLayout::addWidget(): this is well documented and should be taken in consideration. For instance: http://doc.qt.nokia.com/4.7/layout.html#tips-for-using-layouts --------------------------------------------- Tips for Using Layouts When you use a layout, you do not need to pass a parent when constructing the child widgets. The layout will automatically reparent the widgets (using QWidget::setParent()) so that they are children of the widget on which the layout is installed. --------------------------------------------- Whenever you add an object to another one using methods like add*, insert*, etc. or remove it you have to be aware of whether it is reparented or not. See, for instance, QStackedWidget::addWidget and removeWidget. http://doc.qt.nokia.com/4.7/qstackedwidget.html For instance, typically widgets that are going to be inserted in a layout will be instantiated with a null parent, since they will be reparented when they are added to the layout. QWidgets have a parenting hierarchy of themselves. A QWidget's parent must be another QWidget. Here is a short example of how parenting should be handled: #include <QApplication> #include <QMainWindow> #include <QLabel> int main(int argc, char *argv[]) { // 'app' and 'window' are a declared objects that live in the stack: // they will be created automatically and destroyed when main() ends // and they go out of scope. Thus they don't need a parent. // Be careful not to make a declared object the parent of // objects that should last beyond the scope they are declared in, // since it will delete its children and we'll get invalid pointers. QApplication app(argc, argv); QMainWindow window; // 'label' is created in the heap, note it is a pointer. // Who will delete it when it is no longer needed? QLabel *label = new QLabel("Dad!"); // Creating a label with parent null qDebug("Sad label is an orphan since its parent is %p", label->parent()); // QMainWindow::setCentralWidget is one of Qt's reparenting methods. // 'label' will be deleted by the stack-allocated object 'window' when // it is itself destroyed at the end of main(). window.setCentralWidget(label); qDebug("Happy label is now the proud child of %p", label->parent()); window.show(); return app.exec(); } ------------------------------------------------------------------------------ The modern datacenter depends on network connectivity to access resources and provide services. The best practices for maximizing a physical server's connectivity to a physical network are well understood - see how these rules translate into the virtual world? http://p.sf.net/sfu/oracle-sfdevnlfb _______________________________________________ Rosegarden-devel mailing list [email protected] - use the link below to unsubscribe https://lists.sourceforge.net/lists/listinfo/rosegarden-devel
