Widgets Tutorial - Kind-Widgets

Wir können dem im vorherigen Beispiel erstellten Fenster ein untergeordnetes Widget hinzufügen, indem wir window als übergeordnetes Widget an seinen Konstruktor übergeben. In diesem Fall fügen wir dem Fenster eine Schaltfläche hinzu und platzieren sie an einer bestimmten Stelle:

#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();
}

Die Schaltfläche ist nun ein Kind des Fensters und wird gelöscht, wenn das Fenster zerstört wird. Beachten Sie, dass das Ausblenden oder Schließen des Fensters es nicht automatisch zerstört. Es wird zerstört, wenn das Beispiel beendet wird.

Beispielprojekt @ 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.