QWidget *parent 作为用户定义类的构造函数中的参数

发布于 11-06 03:42 字数 502 浏览 5 评论 0 原文

在我之前的帖子中,这里的人告诉我,每个小部件都有一个父级,这在删除时会有所帮助,即当父级被删除时,子级会自动抛出到 /dev/null。

现在我看到 QWidget *parent 被传递给用户定义的类的构造函数,在此链接中:http://doc.qt.io/archives/qt-4.7/tutorials-addressbook-part1.html 地址簿类:公共 QWidget { Q_OBJECT

 public:
     AddressBook(QWidget *parent = 0);

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

这表明什么?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

方觉久 2024-11-13 03:42:23

Qt 的 Widget,尤其是在组合它们时,定义了一个层次结构。让我们通过一个(简化的)示例来直观地了解这一点。

在我的应用程序中,我有一个中央 QWidget。最重要的是另外两个小部件。用于某些 3D 渲染的 QGLWidget 和用于控件的 QTabWidget。这两个小部件是我的中央 QWidget 的子部件。

QTabWidget 有选项卡,它们本身就是 QWidget。让它们成为 QTabWidget 的子级似乎是合乎逻辑的,对吧?

我可以继续。也许其中一个选项卡有一些按钮,另一个选项卡有一些文本字段,等等。

您在这里所做的是创建 QWidget 的层次结构。因此,在本示例中,它看起来有点像这样:

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)

回到您的代码,您可以通过 QWidget(或任何派生小部件)的构造函数来定义此层次结构。如果我创建我的 QGLWidget,在它的构造函数中我将提供一个指针我的中央 QWidget 作为它的父级。

这种层次结构有几个原因。你提到创建/删除。但也要想象一下如果你切换标签。什么元素应该显示在选项卡顶部?当然是它的孩子。如果您开始使用 Qt,您将了解布局。为小部件设置布局将影响该小部件的所有子项。还有其他几个原因(以及幕后发生的事情),但这现在应该足够了。

我的解释相当概念化。也许这不是技术上最详细和最正确的解释,但它应该给你一些指导。

回到你的代码示例。 AddressBook 派生自 QWidget。如果将其放置在 GUI 中,您可能希望将其放置在另一个 Widget 之上。作为它的孩子。因此,如果您添加此 AddressBook,您可以告诉它它的父级是什么。在此示例中,未使用它。 AddressBook 本身就是最上面的小部件。但它也可以很容易地成为层次结构的一部分。

看一下 Qt 附带的一些示例。您应该了解事物的结构。如果您看不到它们是如何连接的,请像我上面所做的那样画一个图表。它应该开始对你有意义。

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.

等你爱我 2024-11-13 03:42:23

构造函数建立小部件之间的父子关系。

除了内存管理和组织(对普通 QObject 也有效)和布局(这不是强制性的——您可以在父级布局之外拥有子窗口小部件)之外,QWidget 的父子关系有一个有趣的变化:

任何没有父级的 QWidget 都将成为一个窗口,并且在大多数平台上都会列在桌面的任务栏中。

(摘自窗口和对话框小部件,Qt 4.7 文档。)

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.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文