위젯 튜토리얼 - 자식 위젯

이전 예제에서 만든 창에 생성자에 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();
}

이제 버튼은 창의 자식이며 창이 파괴되면 삭제됩니다. 창을 숨기거나 닫아도 자동으로 소멸되지 않는다는 점에 유의하세요. 예제가 종료될 때 삭제됩니다.

예제 프로젝트 @ code.qt.io

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