带有 QGridLayout 的 QDockWidget - 布局内的小部件未与顶部对齐

发布于 2025-01-03 03:23:53 字数 678 浏览 1 评论 0原文

我正在尝试创建一个可停靠的工具栏(类似于您在 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:
enter image description here

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

烧了回忆取暖 2025-01-10 03:23:53

来自文档

QGridLayout 获取可用的空间(通过其父布局
或者通过parentWidget()),将其分为行和列,并且
将其管理的每个小部件放入正确的单元格中。

列和行的行为相同;我们将讨论列,但是
行有等效的函数。

每列都有最小宽度和拉伸因子。最低
width 是使用 setColumnMinimumWidth() 设置的最大宽度,并且
该列中每个小部件的最小宽度。拉伸因子为
使用 setColumnStretch() 设置并确定可用的数量
列的空间将超过其必要的最小值。

因此,最简单的方法是使用固定大小的可停靠小部件 QWidget::setFixedSize 。一般来说,我不会推荐它(管理布局),但它非常适合您的情况,原因如下:

  • 所有按钮都具有相同的大小,因此您可以根据按钮的数量大致了解工具栏的大小。
  • 您不需要管理布局行为。
  • 您可以使用任一维度或两者来完成

例如,添加按钮时

  void addButton(QWidget* w,QGridLayout* wl, QButton* button, posParams...){
       // w is the widget inside the QDockWidget (edit)
       //wl is w layout
       //break the constraints, the widget can be resized
       w->setFixedSize(QSize(QWIDGETSIZE_MAX,QWIDGETSIZE_MAX));
       // only line to change if you want to hide, remove widget
       wl->addWidget(button, params);
       //fit to contents 
       w->adjustSize();
       // can be swapped to w->setFixedHeight, setFixedWidth
       w->setFixedSize(w->size());
  }

尝试并验证修改后停靠和取消停靠小部件时不会出现问题。您应该注意,打破约束也会打破
布局。

一般来说,QLayout::SizeConstraintQWidget::minimumSizeHint, QWidget: :minimumSize,您可以在文档中找到它。

  • 默认情况下不设置QWidget::minimumSize。当它出现时,它会压倒一切
    QWidget::minimumSizeHint
  • 如果小部件不在布局中,则 QWidget::minimumSizeHint 无效(意味着可以用鼠标将其大小调整为 0),否则使用由布局定义。
  • QLayout::SizeConstraint 保存它*直接*管理的小部件的默认布局行为。如果您将布局 A 嵌套在布局 B 中,则添加到 A 的所有小部件都将使用其属性。此外,如果 B 中的小部件 W 定义了自己的布局,则此布局约束将应用于小部件 W

From the documentation:

QGridLayout takes the space made available to it (by its parent layout
or by the parentWidget()), divides it up into rows and columns, and
puts each widget it manages into the correct cell.

Columns and rows behave identically; we will discuss columns, but
there are equivalent functions for rows.

Each column has a minimum width and a stretch factor. The minimum
width is the greatest of that set using setColumnMinimumWidth() and
the minimum width of each widget in that column. The stretch factor is
set using setColumnStretch() and determines how much of the available
space the column will get over and above its necessary minimum.

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:

  • All buttons have the same size, so you know roughly the size of the toolbar depending on the number of buttons.
  • you don't need to manage the layout behavior.
  • you can do it with either dimension, or both

For example, when adding a button

  void addButton(QWidget* w,QGridLayout* wl, QButton* button, posParams...){
       // w is the widget inside the QDockWidget (edit)
       //wl is w layout
       //break the constraints, the widget can be resized
       w->setFixedSize(QSize(QWIDGETSIZE_MAX,QWIDGETSIZE_MAX));
       // only line to change if you want to hide, remove widget
       wl->addWidget(button, params);
       //fit to contents 
       w->adjustSize();
       // can be swapped to w->setFixedHeight, setFixedWidth
       w->setFixedSize(w->size());
  }

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 over
    QWidget::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 layout A within a layout B, all widgets added to A will use its property. Also if a widget W in B define its own layout, then this layout constraints are the one to be applied for the widget W.
桃扇骨 2025-01-10 03:23:53

使用 QVBoxLayout,首先添加 QGridLayout,然后添加如下所示的拉伸:

my_vboxlayout->addLayout( my_gridlayout );
my_vboxlayout->addStretch( 1 );

或者,您可以告诉 QGridLayout 最后一行应扩展至最大尺寸,这将向上推按钮。
在您的情况下,它将是:

mygridlayout->setRowStretch( 2, 1 ); // give 3rd row maximum space

文档链接:
QBoxLayout
QGridLayout

Use a QVBoxLayout where you add first your QGridLayout and then add a stretch like this:

my_vboxlayout->addLayout( my_gridlayout );
my_vboxlayout->addStretch( 1 );

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:

mygridlayout->setRowStretch( 2, 1 ); // give 3rd row maximum space

Links to documentation:
QBoxLayout
QGridLayout

鹿童谣 2025-01-10 03:23:53

在下面插入垂直垫片。
输入图片此处描述

Insert a Vertical Spacer below.
enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文