Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Does addwidgets means the make a widget become the parent of another widget



  • Hi all. sorry if my quesion silly, I am confuze about the relation between the different widgets.

    does the following coda means the same thing in a QT widget application ?

    MainWindow::MainWindow(QWidget *parent)
        : QMainWindow(parent)
        , ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
       QGridLayout *layout = new QGridLayout;
       QCustomWidgets *qcw = new QCustomWidget();   
        layout->addWidget(qcw);
        ui->awidget->setLayout(layout);
    }
    
    MainWindow::MainWindow(QWidget *parent)
        : QMainWindow(parent)
        , ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
       QGridLayout *layout = new QGridLayout;
       QCustomWidgets *qcw = new QCustomWidget(this);   
        layout->addWidget(qcw);
        ui->awidget->setLayout(layout);
    }
    
    

    when I called the

    ui->awidget->setLayout(layout)
    

    in first code, the *qcw 's parent widget becomes ui->awidget, so I can operating it as the ui->awidget->*qcw(maybe not, I have to store it in mainwidow's h file?).

    In second code, the *qcw 's parent widget becomes ui->awidget from MainWindow.

    I get it, I am correct.


  • Lifetime Qt Champion

    Hi,

    When you add a widget to a layout the ownership is transferred to the layout. When you call setLayout, the owner of the layout becomes the widget on which you call setLayout.

    If you want to access a widget that you add in the constructor of your designer based widget, then yes, make it a member variable of your class.


Log in to reply