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

how to dynamically adding rows to QVBoxLayout



  • I am interested in dynamically adding layout rows to a QVBoxLayout - I have seen examples of this when there is a existing ui form, but I am add odds as to how to do this dynamically. Existing code follows, with a photo of output. I would like to be able to push my "Add" button, or maybe issue a control key to add rows ;

    #include <QApplication>
    #include <QPushButton>
    #include <QHBoxLayout>
    #include <QVBoxLayout>

    int main(int argc, char* argv[])
    {
    QApplication app(argc, argv);

    // Vertical layout with 3 buttons
    QVBoxLayout *vLayout = new QVBoxLayout;
    QPushButton *b4 = new QPushButton("Add");
    vLayout->addWidget(b4);
    
    // Horizontal layout with 3 buttons
    QHBoxLayout *hLayout = new QHBoxLayout;
    QPushButton *b1 = new QPushButton("A");
    QPushButton *b2 = new QPushButton("B");
    QPushButton *b3 = new QPushButton("C");
    hLayout->addWidget(b1);
    hLayout->addWidget(b2);
    hLayout->addWidget(b3);
    
    // Outer Layer
    QVBoxLayout *mainLayout = new QVBoxLayout;
    
    // Add the previous two inner layouts
    mainLayout->addLayout(vLayout);
    mainLayout->addLayout(hLayout);
    
    // Create a widget
    QWidget *w = new QWidget();
    
    // Set the outer layout as a main layout
    // of the widget
    w->setLayout(mainLayout);
    
    // Window title
    w->setWindowTitle("layouts");
    
    // Display
    w->show();
    
    // Event loop
    return app.exec();
    

    }

    Form looks like this;
    https://ibb.co/nMnbfCK



  • @stdave
    To add layouts to a layout use QBoxLayout::addLayout(). For example

    QVBoxLayout *vLayout = new QVBoxLayout;
    for (int i = 0; i < 3; i++)
        vlayout->addLayout(new QHBoxLayout);
    


  • OK - please forgive my newbie-isms, but here is what I have done - while putting the whole thing in a class, I have implemented your suggestion thus, without any different effect. Pressing the "Add" button puts me to a method which executes suggestion;

    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    #include <QHBoxLayout>
    #include <QVBoxLayout>
    #include <QPushButton>
    #include <QMessageBox>

    void on_button4_clicked()
    {
        QVBoxLayout *vLayout = new QVBoxLayout;
        for (int i = 0; i < 3; i++)
            vLayout->addLayout(new QHBoxLayout);
    
        QMessageBox msgBox;
        msgBox.setText("The document has been modified.");
        msgBox.exec();
     }
    

    MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
    {
    ui->setupUi(this);

    QWidget *widget = new QWidget;
    
    QPushButton *button1 = new QPushButton("A");
    QPushButton *button2 = new QPushButton("B");
    
    QHBoxLayout *hLayout = new QHBoxLayout;
    hLayout->addWidget(button1);
    hLayout->addWidget(button2);
    
    QPushButton *button4 = new QPushButton("Add",this);
    QVBoxLayout *vLayout = new QVBoxLayout;
    vLayout->addWidget(button4);
    
    QVBoxLayout *vMainLayout = new QVBoxLayout;
    
    vMainLayout->addLayout(vLayout);
    vMainLayout->addLayout(hLayout);
    
    widget->setLayout(vMainLayout);
    widget->show();
    connect(button4, &QPushButton::released, this, on_button4_clicked);
    
    setCentralWidget(widget);
    

    }

    MainWindow::~MainWindow()
    {
    delete ui;
    }


  • Lifetime Qt Champion

    Hi,

    You are just adding layouts to a newly created layout that has nothing to do with your widget.


Log in to reply