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

QGridLayout Spacing issue

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

    Hello,

    please have a look at the following code:

    #include "MainWindow.h"
    #include "ui_MainWindow.h"
    #include <QGridLayout>
    #include <QGroupBox>
    #include <QLineEdit>
    
    MainWindow::MainWindow(QWidget *parent) :
       QMainWindow(parent),
       ui(new Ui::MainWindow)
    {
       ui->setupUi(this);
    
       QGridLayout* lay= new QGridLayout();
       lay->setSpacing(10);
       lay->setMargin(10);
       lay->setAlignment(Qt::AlignTop);
       lay->setOriginCorner(Qt::TopLeftCorner);
    
       QLineEdit* edit1 = new QLineEdit("edit1");
       edit1->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Fixed );
    
       QLineEdit* edit2 = new QLineEdit("edit2");
       edit2->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::MinimumExpanding );
       edit2->setMaximumSize(400,300);
    
       QGroupBox* group = new QGroupBox("Group");
       group->setSizePolicy( QSizePolicy::Preferred, QSizePolicy::Fixed );
    
       QGridLayout* boxLay = new QGridLayout(group);
       boxLay->setSpacing(10);
       boxLay->setMargin(10);
    
       QLineEdit* edit3 = new QLineEdit("edit3");
       edit3->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Fixed );
    
       boxLay->addWidget(edit3, 0,0);
    
       QLineEdit* edit4 = new QLineEdit("edit4");
       edit4->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Fixed );
    
       lay->addWidget(edit1,0,0);
       lay->addWidget(edit2,1,0);
       lay->addWidget(group,3,0,2,1);
       lay->addWidget(edit4,5,0);
    
       this->centralWidget()->setLayout(lay);
    }
    

    If you expand the window, edit2 grows to its maximumSize.
    After the maximumSize is reached, a space between the other widgets will inserted.

    What can I do to prevent this behaviour and create the space at the end?

    raven-worxR 1 Reply Last reply
    0
    • markcurlM markcurl

      Hello,

      please have a look at the following code:

      #include "MainWindow.h"
      #include "ui_MainWindow.h"
      #include <QGridLayout>
      #include <QGroupBox>
      #include <QLineEdit>
      
      MainWindow::MainWindow(QWidget *parent) :
         QMainWindow(parent),
         ui(new Ui::MainWindow)
      {
         ui->setupUi(this);
      
         QGridLayout* lay= new QGridLayout();
         lay->setSpacing(10);
         lay->setMargin(10);
         lay->setAlignment(Qt::AlignTop);
         lay->setOriginCorner(Qt::TopLeftCorner);
      
         QLineEdit* edit1 = new QLineEdit("edit1");
         edit1->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Fixed );
      
         QLineEdit* edit2 = new QLineEdit("edit2");
         edit2->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::MinimumExpanding );
         edit2->setMaximumSize(400,300);
      
         QGroupBox* group = new QGroupBox("Group");
         group->setSizePolicy( QSizePolicy::Preferred, QSizePolicy::Fixed );
      
         QGridLayout* boxLay = new QGridLayout(group);
         boxLay->setSpacing(10);
         boxLay->setMargin(10);
      
         QLineEdit* edit3 = new QLineEdit("edit3");
         edit3->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Fixed );
      
         boxLay->addWidget(edit3, 0,0);
      
         QLineEdit* edit4 = new QLineEdit("edit4");
         edit4->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Fixed );
      
         lay->addWidget(edit1,0,0);
         lay->addWidget(edit2,1,0);
         lay->addWidget(group,3,0,2,1);
         lay->addWidget(edit4,5,0);
      
         this->centralWidget()->setLayout(lay);
      }
      

      If you expand the window, edit2 grows to its maximumSize.
      After the maximumSize is reached, a space between the other widgets will inserted.

      What can I do to prevent this behaviour and create the space at the end?

      raven-worxR Offline
      raven-worxR Offline
      raven-worx
      Moderators
      wrote on last edited by raven-worx
      #2

      @markcurl
      since you have set the size-policy of all widgets accordingly.
      With a QVBoxLayout you could simply add a stretch to the layout after all widgets.

      Alternatively you can try adding a QSpacerItem at the end of the QGridLayout:

      QSpacerItem* vSpacerItem = new QSpacerItem( 0, 0, QSizePolicy::Minimum, QSizePolicy::Expanding );
      
      lay->addWidget(edit1,0,0);
      lay->addWidget(edit2,1,0);
      lay->addWidget(group,3,0,2,1);
      lay->addWidget(edit4,5,0);
      lay->addItem(vSpacerItem, 5, 0);
      

      I haven't tested it, but thats basically what QBoxLayout does when adding a stretch.

      --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
      If you have a question please use the forum so others can benefit from the solution in the future

      markcurlM 1 Reply Last reply
      2
      • raven-worxR raven-worx

        @markcurl
        since you have set the size-policy of all widgets accordingly.
        With a QVBoxLayout you could simply add a stretch to the layout after all widgets.

        Alternatively you can try adding a QSpacerItem at the end of the QGridLayout:

        QSpacerItem* vSpacerItem = new QSpacerItem( 0, 0, QSizePolicy::Minimum, QSizePolicy::Expanding );
        
        lay->addWidget(edit1,0,0);
        lay->addWidget(edit2,1,0);
        lay->addWidget(group,3,0,2,1);
        lay->addWidget(edit4,5,0);
        lay->addItem(vSpacerItem, 5, 0);
        

        I haven't tested it, but thats basically what QBoxLayout does when adding a stretch.

        markcurlM Offline
        markcurlM Offline
        markcurl
        wrote on last edited by
        #3

        @raven-worx

        Thanks, works fine :)

        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