简体   繁体   English

Qt如何将自定义小部件添加到垂直布局以占用最少的垂直空间(自定义小部件之间没有空间)

[英]Qt How to add custom widgets to a vertical layout to occupy the least amount of vertical space (no space between custom widgets)

I've created a custom widget containing a few widgets in a horizontal layout: 我创建了一个自定义窗口小部件,其中包含一些水平布局的窗口小部件:

自定义小部件

在此处输入图片说明

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: 我遇到的问题是,自定义窗口小部件添加到垂直布局后,它们之间的空间太大: 太多空间

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

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. 默认情况下,顶部和底部的边距设置为9像素,这导致了问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM