Tutorial sobre widgets: widgets secundarios

Podemos añadir un widget secundario a la ventana creada en el ejemplo anterior pasando window como padre a su constructor. En este caso, añadimos un botón a la ventana y lo colocamos en una ubicación específica:

#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();
}
Ventana con un botón como elemento secundario

El botón es ahora un elemento secundario de la ventana y se eliminará cuando esta se destruya. Ten en cuenta que ocultar o cerrar la ventana no la destruye automáticamente. Se destruirá cuando se salga del ejemplo.

Proyecto de ejemplo en code.qt.io

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