Search code examples
qtqwidget

QWidget *parent as an argument in the user defined class's constructor


In my previous thread, people here told me, that every widget has a parent which helps in cases of deletion, i.e. when the parent gets deleted, the child gets automatically thrown out to /dev/null.

Now I see QWidget *parent being passed to the user defined class's constructor here, in this link: http://doc.qt.io/archives/qt-4.7/tutorials-addressbook-part1.html class AddressBook : public QWidget { Q_OBJECT

 public:
     AddressBook(QWidget *parent = 0);

 private:
     QLineEdit *nameLine;
     QTextEdit *addressText;
 };

What does this indicate?


Solution

  • Qt's Widgets, especially when combining them, define a hierarchy. Let's look at this visually with a (simplified) example.

    In my application I have a central QWidget. On top of that are two other widget. A QGLWidget for some 3D rendering and a QTabWidget for controls. Those two widgets are the children of my central QWidget.

    The QTabWidget has tabs, which are QWidgets themselves. And it seems logical to have those be the children of the QTabWidget, right?

    And I can go on. Perhaps one of the tabs has some buttons, another tab some textfields, whatever.

    What you are doing here is create a hierarchy of QWidgets. So in case of this example it will look somewhat like this:

    QWidget (the Central Widget)
    |
    |-QGLWidget (my rendering window)
    |-QTabWiget (the tab widget for my controls)
     |
     |-QWidget (the first tab)
     ||
     ||-QPushButton (a button on the first tab)
     |
     |-QWidget (the second tab)
      |
      |-QLineEdit (a widget to provide some input)
    

    Coming back to your code, you can define this hierarchy via the constructor of a QWidget (or any derived widget). If I create my QGLWidget, in its constructor I will provide a pointer my central QWidget as its parent.

    There are several reasons for this hierarchy. You mention creation/deletion. But also imagine if you switch tabs. What element should be shown on top of the tab? Its children of course. And if you get going with Qt, you'll learn about Layouts. Setting a Layout to a widget will affect all the children of that widget. There are several other reasons (and things going on behind the scenes) but this should suffice for now.

    My explanation is fairly conceptual. Perhaps this is not the most technically detailed and correct explanation, but it should give you some pointers.

    Coming back to your code example. The AddressBook is derived from a QWidget. If you place it in your GUI, you might want to put it on top of another Widget. As its child. So if you add this AddressBook, you can tell it what its parent is. In this example, it is not used. The AddressBook is itself the top most widget. But it could just as easily be part of a hierarchy.

    Have a look at some of the samples that come with Qt. You should get an idea of how things are structured. If you don't see how they connect, draw a diagram as I have done above. It should start making sense to you.