C++ 更改 Qt 布局中的调整大小行为
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1336561/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Change resize behavior in Qt layouts
提问by Corey D
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 后,额外的空间仅作为小部件之间的填充。
回答by Jason B
I've had trouble with this in the past and here are some of the things I've found:
过去我遇到过这个问题,以下是我发现的一些问题:
First make sure all the widgets you want to expand have sizePolicy set to "Expanding".
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.
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.
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.
首先确保您要扩展的所有小部件都将 sizePolicy 设置为“扩展”。
确保组成自定义小部件的小部件位于允许扩展的布局中。您可以通过将自定义小部件之一添加到窗口并查看它是否按预期展开来检查这一点。
确保表单上您不想展开的任何小部件在您希望它们保持静态的维度中具有固定(最小=最大)大小。
有时网格布局会导致一些奇怪的间距问题,因为行会根据整行中最大的小部件调整大小,列也类似。对于某些布局,最好使用包含水平布局的垂直布局,反之亦然,以创建类似网格的效果。只有这样,每个子布局才能独立于其他行或列。
回答by swongu
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()
. You'll need to provide weights to the specific columns (0 for no stretch).
我发现,您可以轻松地控制哪些列/行扩大和列/行保持通过使用固定的宽度QGridLayout::setColumnStretch()
和QGridLayout::setRowStretch()
。您需要为特定列提供权重(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. Just look for the widget properties layoutRowStretchand layoutColumnStretch. It'll contain a comma-separated list of integers.
如果您使用 Designer,则可以执行我上面描述的操作。只需查找小部件属性layoutRowStretch和layoutColumnStretch。它将包含一个以逗号分隔的整数列表。
回答by Chris Benesch
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". 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. I wanted the color selection controls to remain the same size horizontally, so they were in a VerticalLayout. I imagine you can do the same with a HorizontalLayout too if you want things to stay the same height. IF you really need spacers inside the layout, you can probably use blank labels with fixed size.
另一个选项是在 QT Creator 内部,在您想要固定大小的部分的顶级布局小部件中指定“SetFixedSize”的 layoutSizeConstraint。您还必须从该小部件下方移除所有间隔物。就我而言,我有一个包含 TreeWidget、表格和一些颜色选择内容的对话框。我希望颜色选择控件在水平方向上保持相同的大小,因此它们处于 VerticalLayout 中。我想如果你想让东西保持相同的高度,你也可以用 HorizontalLayout 做同样的事情。如果您确实需要在布局内使用垫片,您可以使用固定尺寸的空白标签。