QWidget *parent 作为用户定义类的构造函数中的参数
在我之前的帖子中,这里的人告诉我,每个小部件都有一个父级,这在删除时会有所帮助,即当父级被删除时,子级会自动抛出到 /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;
};
这表明什么?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
发布评论
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
Qt 的 Widget,尤其是在组合它们时,定义了一个层次结构。让我们通过一个(简化的)示例来直观地了解这一点。
在我的应用程序中,我有一个中央 QWidget。最重要的是另外两个小部件。用于某些 3D 渲染的 QGLWidget 和用于控件的 QTabWidget。这两个小部件是我的中央 QWidget 的子部件。
QTabWidget 有选项卡,它们本身就是 QWidget。让它们成为 QTabWidget 的子级似乎是合乎逻辑的,对吧?
我可以继续。也许其中一个选项卡有一些按钮,另一个选项卡有一些文本字段,等等。
您在这里所做的是创建 QWidget 的层次结构。因此,在本示例中,它看起来有点像这样:
回到您的代码,您可以通过 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:
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.