8.2.2.1. Exercises: QObject's Child Managment

  1. In Example 8.4, what local stack objects were destroyed when growBunch() returned?

  2. Notice that Alice does not appear in the dumpObjectTree() output. When does Alice get destroyed?

      Alice does not get destroyed until after the program terminates.

  3. Write your own function,

    void showTree(QObject* theparent)

    to main.cpp. The output of this function, after all objects have been created, should look like this:

    Member: Mike - Parent: A Stack Object
    Member: Greg - Parent: Mike
    Member: Peter - Parent: Mike
    Member: Bobby - Parent: Mike
    Member: Carol - Parent: A Stack Object
    Member: Marcia - Parent: Carol
    Member: Jan - Parent: Carol
    Member: Cindy - Parent: Carol
    

     

    Example 8.5. src/iteration/java/showtree.cpp

    [ . . . . ]
    
    void showTree(QObject* theparent) {
    
        QObjectList kids = theparent->children();
    
        QListIterator<QObject*> itr (kids); 1
        while (itr.hasNext()) {  2
            QObject* ptr = itr.next();
            qDebug() << QString("Brady member: %1 - Parent: %2")
                   .arg(ptr->objectName())
                   .arg(ptr->parent()->objectName());
            showTree(ptr);
        }
    }
    

    1

    read-only java-style iteration

    2

    Java-Style iterators point between elements. The first call to next() returns the first element and advances the iterator

    <include src="src/iteration/java/showtree.cpp" href="src/iteration/java/showtree.cpp" role="solution" mode="cpp" segid="showtree"/>


  4. Modify your showTree() function so that it produces the same output as dumpObjectTree().

[ fromfile: children.xml id: None ]