1

I've created a custom widget containing a few widgets in a horizontal layout:

custom widget

enter image description here

And the goal is to display several of these in a list so I've added them dynamically to a parent vertical layout. The problem I'm having is that there's too much space in between my custom widgets when they're added to the vertical layout: too much space

I want them to be tightly packed so that there's only a small space in between. I've added a spacer at the bottom and played around with the size policies etc but to no avail. Below is the code to add the widgets. Any and all help appreciated.

    // Draw the nodes area
QVBoxLayout* nodeVLayout = new QVBoxLayout;
NodeWidget* node1 = new NodeWidget;
NodeWidget* node2 = new NodeWidget;
QSpacerItem* spacer = new QSpacerItem(1, 1, QSizePolicy::Expanding, QSizePolicy::Expanding);

nodeVLayout->setSpacing(1);
nodeVLayout->addWidget(node1);
nodeVLayout->addWidget(node2);
nodeVLayout->addSpacerItem(spacer);

ui->scrNodes->setLayout(nodeVLayout);
bananamana
  • 485
  • 1
  • 9
  • 19
  • Add a growing/expanding spacer beneath them, to push them up. And set their size polizy to QSizePolicy::Minimum. Should do do the trick I'd expect. – Jesper Juhl Dec 15 '17 at 16:12
  • Hi. There was already a spacer at the bottom but I did set the size policy to minimum as you said but it didn't change anything. – bananamana Dec 15 '17 at 16:30

1 Answers1

0

In the layout options check that the margins and spacing are set appropriately.

By default the margins on the top and bottom were set to 9 pixels which was causing the problem.

bananamana
  • 485
  • 1
  • 9
  • 19