Hareen Laks
Hareen Laks

Reputation: 1505

Parent-child relationship set to member variables in Qt

I was reading Qt Documentation here. I found the following sentence under the title 'Thread Affinity'.

Note: A QObject's member variables do not automatically become its children. The parent-child relationship must be set by either passing a pointer to the child's constructor, or by calling setParent().

I don't understand what it mentioned as set parent-child relationship between object and it's member variables. I only know the parent-child relationship between super class and sub class.

Can anyone explain this sentence to me? If you can provide a example it is more helpful.

Thanks for reading.

Upvotes: 0

Views: 2342

Answers (3)

A QObject is an object container. The objects it contains are called its children, the containing object is considered a parent to those children. This in itself says nothing about how and were the children are allocated. Let's look at some scenarios, in C++11:

void test() {
  QObject aParent;
  // children of automatic storage duration
  QObject aChild1{&aParent}, aChild2;
  aChild2->setParent(&aParent);
  // children of dynamic storage duration
  auto aChild3 = new QObject{&aParent};
  auto aChild4 = new QObject;
  aChild4->setParent(&aParent);
}

struct Parent : QObject {
  QObject aChild5 { this };
  QObject * aChild6 { new QObject };
  QObject notAChild;
  Parent() { aChild6->setParent(this); }
};

The test() function demonstrates how an object can be a parent to some children of automatic and dynamic storage duration. The parent object can be given in the constructor or as an argument to setParent method.

The Parent class demonstrates that member objects can be children of the parent class, but not necessarily so. The member values are of automatic storage duration, but not all child objects are. The object pointed to by aChild6 is of dynamic storage duration. Since QObject deletes all children in its destructor, the effective storage duration of the object at aChild6 is automatic: you don't have to worry about having to delete the object.

Upvotes: 1

ramtheconqueror
ramtheconqueror

Reputation: 1964

Member variables will NOT become child objects without explicitly setting parent property. A Object subclass normally takes another QObject as it's parent in the constructor.

class Test : public QObject
{
  Q_OBJECT
  public:
    Test(QObject* prnt) 
      : QObject(prnt),
        timerNoPrnt(), // Test object is NOT the parent. This won't be deleted when Test object gets deleted.
        timer(this)    // Test object is the parent here. This will be deleted when Test object gets deleted. 
    {
      timerNoPrnt->setParent(this); // now parent assigned.
    }
  private:
    QTimer*           timerNoPrnt;   // member variable
    QTimer*           timer;
}

Upvotes: 1

Gombat
Gombat

Reputation: 2084

A QObject (A) can have other QObjects (B's) as member variables. The B's are not automatically created with A a their parent and do not have to be. It is possible but not necessary nor automatically done.

There is not only the parent-child relationship between a class and it's super class (base class), but also between a QWidget and embedded widgets. It is used e.g. to destroy the widgets in the right order in the end.

See the doc here and an answer on SO regarding memory management here.

Upvotes: 1

Related Questions