Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. how to dynamically adding rows to QVBoxLayout
Forum Updated to NodeBB v4.3 + New Features

how to dynamically adding rows to QVBoxLayout

Scheduled Pinned Locked Moved Unsolved General and Desktop
4 Posts 3 Posters 1.5k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    stdave
    wrote on 31 Mar 2021, 14:05 last edited by
    #1

    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

    J 1 Reply Last reply 31 Mar 2021, 14:24
    0
    • S stdave
      31 Mar 2021, 14:05

      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

      J Offline
      J Offline
      JonB
      wrote on 31 Mar 2021, 14:24 last edited by
      #2

      @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);
      
      1 Reply Last reply
      3
      • S Offline
        S Offline
        stdave
        wrote on 2 Apr 2021, 19:19 last edited by
        #3

        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;
        }

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on 2 Apr 2021, 19:34 last edited by
          #4

          Hi,

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

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0

          1/4

          31 Mar 2021, 14:05

          • Login

          • Login or register to search.
          1 out of 4
          • First post
            1/4
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved