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

Move widget down after top widget resize



  • Hi,

    I have a QVBoxLayout that contains a few widgets. When clicking on some button one of the widgets needs to grow downwards, but instead of the widget below it moving, it stays in the same location and they are one on top of the other.
    I've tried changing the parent widget size, but that didn't help.
    To be clear, I change the size of one of the myWidget in the following code -

        QWidget* cWidget = new QWidget();
        QWidget* headers = new QWidget(cWidget);
        initHeaders(headers);
    
        QVBoxLayout clsLay;
        clsLay.addWidget(headers);
        QSqlQuery q = getQuery(type);
        while (q.next()) {
            myWidget* r = new myWidget();
            clsLay.addWidget(r);
        }
        cWidget->setLayout(&clsLay);
        clsLay.setAlignment(Qt::AlignCenter);
        cWidget->setStyleSheet("font-family:Myriad pro; font-size:14px; border:1px solid gray; background-color:rgb(252, 252, 254, 230); margin:0px;");
        cWidget->show();
    

  • Moderators

    Your clsLay layout is a local variable. If this code is in some function then that variable goes out of scope and is destroyed so your widget has no layout.

    You should create the layout dynamically with new. The widget takes ownership of the layout so it will clean it up when destroyed.



  • Thanks, didn't think about it :) Solved the problem.


Log in to reply