简体   繁体   English

更改Qt布局中的调整大小行为

[英]Change resize behavior in Qt layouts

I want my custom widgets to gain extra space when the dialog is resized. 我希望我的自定义小部件在调整对话框大小时获得额外的空间。 This was working when I only had a handful of widgets, but after adding several more columns of these same widgets and putting them in a QGridLayout, the extra space merely goes in as padding between the widgets. 当我只有一些小部件时,这是有效的,但是在添加了几个这些相同小部件的列并将它们放入QGridLayout之后,额外的空间仅仅作为小部件之间的填充。

I've had trouble with this in the past and here are some of the things I've found: 我过去遇到过麻烦,以下是我发现的一些事情:

  1. First make sure all the widgets you want to expand have sizePolicy set to "Expanding". 首先确保要扩展的所有小部件都将sizePolicy设置为“Expanding”。

  2. Make sure the widgets that make up your custom widgets are in a layout that allows for expanding. 确保构成自定义窗口小部件的窗口小部件采用允许扩展的布局。 You can check this by just adding one of your custom widgets to the window and seeing that it expands as expected. 您可以通过将一个自定义小部件添加到窗口并看到它按预期扩展来检查这一点。

  3. Make sure any widgets on the form that you do not want to expand have a fixed (minimum=maximum) size in the dimension you want them to stay static. 确保表单上您不想展开的任何窗口小部件在您希望它们保持静态的维度中具有固定的(最小=最大)大小。

  4. Sometimes the grid layout causes some weird spacing issues because rows are resized based on the largest widget in the entire row and similarly for columns. 有时,网格布局会导致一些奇怪的间距问题,因为行会根据整行中最大的窗口小部件进行调整,类似于列。 For some layouts, it is better to use a vertical layout that contains horizontal layouts or vica versa to create a grid-like effect. 对于某些布局,最好使用包含水平布局的垂直布局,或者反之亦然,以创建类似网格的效果。 Only this way, each sub-layout is spaced independently of the other rows or columns. 只有这样,每个子布局才独立于其他行或列间隔开。

Controlling grid expansion programatically 以编程方式控制网格扩展

I've found that you can easily control which columns/rows expand and which columns/rows stay fixed in width by using QGridLayout::setColumnStretch() and QGridLayout::setRowStretch() . 我发现你可以通过使用QGridLayout::setColumnStretch()QGridLayout::setRowStretch()轻松控制哪些列/行扩展以及哪些列/行保持宽度固定。 You'll need to provide weights to the specific columns (0 for no stretch). 您需要为特定列提供权重(0表示无拉伸)。

For example, if you want column 0 to not take up any room and column 1 to take the rest of the window's room, do this: 例如,如果您希望第0列不占用任何房间而第1列占用窗口的其余部分,请执行以下操作:

QGridLayout* layout ;
// Set up the layout
layout->setColumnStretch( 0, 0 ) ; // Give column 0 no stretch ability
layout->setColumnStretch( 1, 1 ) ; // Give column 1 stretch ability of ratio 1

Controlling grid expansion using Qt Designer 使用Qt Designer控制网格扩展

You can do what I described above if you're using Designer. 如果你正在使用Designer,你可以做我上面描述的。 Just look for the widget properties layoutRowStretch and layoutColumnStretch . 只需查看小部件属性layoutRowStretchlayoutColumnStretch即可 It'll contain a comma-separated list of integers. 它将包含以逗号分隔的整数列表。

Another option is inside of QT Creator, to specify in the top level layout widget of the section you want fixed size a layoutSizeConstraint of "SetFixedSize". 另一个选项是在QT Creator中,在你想要固定大小的部分的顶级布局小部件中指定layoutSizeConstraint“SetFixedSize”。 You must also remove all spacers from beneath that widget. 您还必须从该窗口小部件下方删除所有间隔符。 In my case, I had a dialog with a TreeWidget, a Table, and some color selection stuff. 在我的例子中,我有一个TreeWidget,Table和一些颜色选择的对话框。 I wanted the color selection controls to remain the same size horizontally, so they were in a VerticalLayout. 我希望颜色选择控件在水平方向上保持相同的大小,因此它们位于VerticalLayout中。 I imagine you can do the same with a HorizontalLayout too if you want things to stay the same height. 我想如果你想让东西保持相同的高度,你也可以使用Horizo​​ntalLayout做同样的事情。 IF you really need spacers inside the layout, you can probably use blank labels with fixed size. 如果你真的需要在布局中使用垫片,你可以使用固定尺寸的空白标签。

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

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