开发者

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 use开发者_运维问答r 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?


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)

Anyway, you catch my drift. 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.

Quite a lot of text, but I hope it explains the concepts somewhat. 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. If anyone has corrections or more technical insight that might be useful, feel free to comment or correct me.

Hope that helps.


The constructor establishes the parent-child relationship between widgets.

Besides memory management and organization (which are valid for plain QObjects too) and layout (which is not mandatory -- you can have child widgets outside of the parent's layout), the parent-child relationship takes an interesting twist for QWidgets:

Any QWidget that has no parent will become a window, and will on most platforms be listed in the desktop's task bar.

(Taken from Window and Dialog Widgets, Qt 4.7 documentation.)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜