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. Nesting Layouts
Qt 6.11 is out! See what's new in the release blog

Nesting Layouts

Scheduled Pinned Locked Moved Solved General and Desktop
3 Posts 2 Posters 1.1k 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.
  • G Offline
    G Offline
    gabor53
    wrote on last edited by
    #1

    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.

    JKSHJ 1 Reply Last reply
    0
    • G gabor53

      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.

      JKSHJ Offline
      JKSHJ Offline
      JKSH
      Moderators
      wrote on last edited by
      #2

      @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);
      

      Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

      1 Reply Last reply
      1
      • G Offline
        G Offline
        gabor53
        wrote on last edited by
        #3

        Thank you. It solved my problem.

        1 Reply Last reply
        0

        • Login

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