Important: Please read the Qt Code of Conduct -

Vertical spacing of items in a QGridLayout

  • I'm programming a simple backup utility which will list all the available settings that can be backed up on the main window. As you can see from the screenshot below, if there are not enough checkboxes within the "Personal Data" QGroupBox, the spacing between them is large. Therefore the look of the layout is messing. I'm using a QGridLayout in each of the two QGroupBoxes to place the checkboxes.

    Does anyone have any suggestions of how I could make the spacing the same as the spacing used in the "Application Settings" QGroupBox?


  • You could use a vertical spacer below the 2 checkboxes.

  • Sounds like a good idea but when I try to use a QSpacerItem, it has no effect.

    I create the QGridLayout and checkboxes. Place the two checkboxes. Then create the QSpacerItem and place it into the layout like this:

    @QSpacerItem *vSpacer = new QSpacerItem(100, 100, QSizePolicy::Ignored, QSizePolicy::Ignored);
    m_personalGridLayout->addItem(vSpacer, 3, 0, -1, -1);@

    I use QSizePolicy::Ignored because that way the spacer "will get as much space as possible":, but MinimumExpanding and Expanding should do as well. Unfortunately, all three enum values fail in changing anything.

    The QSpacerItem's preferred width and height are set to 100 because I have no preferred width and height - as long as the spacer fills up the space. I tried 1x1 but that didn't help.

    When adding the QSpacerItem to the layout I specify that the spacer can "expand horizontally and vertically": Sort of redundant.

    I know I must be missing something or something is overriding the QSpacerItem. Any ideas?

  • I was not clear enough, but i would use a spacer below the gridlayout, not in it;)

    Also i'm used to test layouts in a ui. Afterwards then you can have a look at the code in the generated ...ui.h file.

Log in to reply