带有 QGridLayout 的 QDockWidget - 布局内的小部件未与顶部对齐
我正在尝试创建一个可停靠的工具栏(类似于您在 Photoshop 中使用的工具栏),它将包含 2 x (n) 的按钮网格。我的想法是使用以空白 QWidget 为父级的 QGridLayout,将其添加到 QDockWidget,并将按钮添加到 QGridLayout。除了对齐之外,这似乎有效。
我已经设置了按钮的对齐...
myLayout->addWidget(button1,0,0,1,1,Qt::AlignTop);
myLayout->addWidget(button2,0,1,1,1,Qt::AlignTop);
myLayout->addWidget(button3,1,0,1,1,Qt::AlignTop);
myLayout->addWidget(button4,1,1,1,1,Qt::AlignTop);
...但是网格正在扩展到 QDockWidget 的整个高度,如下所示:
这些按钮也水平扩展,以填充整个空间。我想我可以限制水平调整大小的能力(如果可能的话?)。
我在文档中忽略了一个函数来更好地控制 GridLayout 来限制它填充父小部件的整个宽度/高度吗?作为一个附带问题,有没有办法防止 QDOckWidget 在某个方向上调整大小?
I'm attempting to create a dockable toolbar (similar to what you use in Photoshop) that will hold a 2 x (n) grid of buttons. My idea is to use a QGridLayout parented to a blank QWidget, which is added to a QDockWidget, and add buttons to the QGridLayout. This seems to be working except for the aligning.
I've set the align for the buttons...
myLayout->addWidget(button1,0,0,1,1,Qt::AlignTop);
myLayout->addWidget(button2,0,1,1,1,Qt::AlignTop);
myLayout->addWidget(button3,1,0,1,1,Qt::AlignTop);
myLayout->addWidget(button4,1,1,1,1,Qt::AlignTop);
...however the grid is expanding to the full height of the QDockWidget, as seen below:
The buttons are also expanding horizontally as well, to fill the entire space. I figure I can just restrict the ability to re-size it horizontally (if this is possible?).
Is there a function I'm overlooking in the doc to control the GridLayout a little better to restrict it filling the entire width/height of the parent widget? And as a side-question, is there a way to prevent a QDOckWidget from being re-sized a certain direction?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
来自文档:
因此,最简单的方法是使用固定大小的可停靠小部件 QWidget::setFixedSize 。一般来说,我不会推荐它(管理布局),但它非常适合您的情况,原因如下:
例如,添加按钮时
尝试并验证修改后停靠和取消停靠小部件时不会出现问题。您应该注意,打破约束也会打破
布局。
一般来说,QLayout::SizeConstraint、QWidget::minimumSizeHint, QWidget: :minimumSize,您可以在文档中找到它。
QWidget::minimumSize
。当它出现时,它会压倒一切QWidget::minimumSizeHint
QWidget::minimumSizeHint
无效(意味着可以用鼠标将其大小调整为 0),否则使用由布局定义。QLayout::SizeConstraint
保存它*直接*管理的小部件的默认布局行为。如果您将布局A
嵌套在布局B
中,则添加到A
的所有小部件都将使用其属性。此外,如果B
中的小部件W
定义了自己的布局,则此布局约束将应用于小部件W
。From the documentation:
So the easiest way would be to use a fixed size of the dockable widget QWidget::setFixedSize. In general, I would not recommend it (managing the layout ) but it is perfect for your case for the following reasons:
For example, when adding a button
Try it and verify things don't get awry when you dock and undock the widget after modification. You should note that breaking the constraint also break the ones of the
layout.
In general there is hierarchy between QLayout::SizeConstraint, QWidget::minimumSizeHint, QWidget::minimumSize, and you can find it in the documentation.
QWidget::minimumSize
is not set by default. When it is, it prevails overQWidget::minimumSizeHint
QWidget::minimumSizeHint
is invalid if the widget is not in a layout (meaning that it can be resized to 0 with the mouse), otherwise use the one defined by the layout.QLayout::SizeConstraint
holds the default layout behavior of the widgets it *directly * manage. If you nest a layoutA
within a layoutB
, all widgets added toA
will use its property. Also if a widgetW
inB
define its own layout, then this layout constraints are the one to be applied for the widgetW
.使用 QVBoxLayout,首先添加 QGridLayout,然后添加如下所示的拉伸:
或者,您可以告诉 QGridLayout 最后一行应扩展至最大尺寸,这将向上推按钮。
在您的情况下,它将是:
文档链接:
QBoxLayout
QGridLayout
Use a QVBoxLayout where you add first your QGridLayout and then add a stretch like this:
Alternatively you can tell your QGridLayout that the last row should expand to a maximum size, which will push up the buttons.
In your case it would be:
Links to documentation:
QBoxLayout
QGridLayout
在下面插入垂直垫片。
Insert a Vertical Spacer below.