部件教程 - 子部件
我们可以将window
作为父部件传递给构造函数,从而为上一示例中创建的窗口添加一个子部件。在本例中,我们为窗口添加了一个按钮,并将其放置在特定位置:
#include <QtWidgets> int main(int argc, char *argv[]) { QApplication app(argc, argv); QWidget window; window.resize(320, 240); window.setWindowTitle (QApplication::translate("childwidget", "Child widget")); window.show(); QPushButton *button = new QPushButton( QApplication::translate("childwidget", "Press me"), &window); button->move(100, 100); button->show(); return app.exec(); } |
![]() |
按钮现在是窗口的子部件,将在窗口销毁时被删除。请注意,隐藏或关闭窗口并不会自动销毁窗口。它将在示例退出时被销毁。
© 2025 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.