Nesting Layouts



  • Hi,
    I have the following layout in a QScrollArea:

    QScrollArea *scroll = new QScrollArea(this);
        scroll->setVerticalScrollBarPolicy (Qt::ScrollBarAlwaysOn);
        scroll->setHorizontalScrollBarPolicy (Qt::ScrollBarAlwaysOn);
    
        QLabel *spacer = new QLabel;
        QLabel *test = new QLabel("Test");
    
    
        QWidget *viewport  = new QWidget(this);
        scroll->setWidget (viewport);
        scroll->setWidgetResizable (true);
    
        QFormLayout *layout = new QFormLayout(this);
        viewport->setLayout (layout);
        QHBoxLayout *boxlayout = new QHBoxLayout(this);
    
        layout->addRow (Title);//Adding the Title
        layout->addRow(spacer,spacer);
        layout->addRow(spacer,spacer);
        layout->addRow(spacer,spacer);
    	boxlayout->insertItem (5,test); 
    
        layout->addRow (Label_ID,ID_Display); //Adding ID number
        layout->addRow (Label_Name,LineEdit_Name);//Adding Name
    
        QFormLayout *dialog_flayout = new QFormLayout(this);
        this->setLayout (dialog_flayout);
        this->layout ()->addWidget (scroll);
        this->show();
    
    

    Where the line
    boxlayout->insertItem (5,test);
    is I need to add 4 widgets to the line instead of 2. How can I nest a QHBoxLayout into this row?
    Thank you for your help.


  • Moderators

    @gabor53 said:

    I need to add 4 widgets to the line instead of 2. How can I nest a QHBoxLayout into this row?

    QFormLayout::addRow() has an overload that accepts other QLayouts.

    // Set up your horizontal layout of 4 widgets
    auto rowOfFour = new QHBoxLayout;
    rowOfFour->addWidget(new QLabel("One"));
    rowOfFour->addWidget(new QLabel("Two"));
    rowOfFour->addWidget(new QLabel("Three"));
    rowOfFour->addWidget(new QLabel("Four"));
    
    // Add the horizontal layout to your main layout
    auto topLevelLayout = new QFormLayout;
    topLevelLayout->addRow(rowOfFour);
    


  • Thank you. It solved my problem.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.