了解 Qt 中的表单布局机制

发布于 2024-10-09 17:14:53 字数 349 浏览 0 评论 0 原文

Qt 具有灵活且强大的布局机制来处理桌面应用程序窗口的视图。

但它是如此灵活,以至于当出现问题并需要微调时,它几乎无法被理解。而且如此强大,以至于它可以击败任何试图压倒 Qt 关于表单外观的观点的人。

那么,谁能解释一下,或者提供一下Qt定位机制的文章或者来源吗?

我试图强制 QLabelQPushButtonQTableView(在其名称中用尾随下划线标记)比 高两倍QTextBrowser 具有下面的 verticalStretch = 1。如何正确处理小部件的高度?

Qt has a flexible and powerful layout mechanism to handle view of desktop application's windows.

But it is so flexible, that it nearly cannot be understood, when something goes wrong and needs fine-tuning. And so powerful, that it can beat anyone in their tries to overwhelm Qt's opinion of how form should look.

So, can anyone explain, or provide articles, or source of Qt's positioning mechanisms?

I'm trying to force the QLabel, QPushButton and QTableView, marked by trailing underscores in their names, be two times higher than QTextBrowser having verticalStretch = 1 below. How can I handle widget's height properly?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

记忆消瘦 2024-10-16 17:14:53

布局实际上很容易理解“我认为”。 :)
布局的简单解释可以在 QT 书籍“使用 QT 第 2 版进行 C++ Gui 编程"

有关布局及其大小策略的注意事项< /strong>

  • 大多数 Qt 小部件都有大小策略。此大小策略告诉系统小部件应如何拉伸或收缩。它来自 QSizePolicy 类。规模策略具有垂直和水平两个部分。
  • 大多数小部件也有尺寸提示。这个大小提示告诉系统小部件的首选大小
  • QSizePolicy 有一个拉伸因子,允许小部件以不同的速率增长

**I am only familiar with 4 size policies**

  • 固定大小策略 - 小部件的大小是固定的,不能拉伸。它仍保持其尺寸提示。
  • 最小尺寸策略 - 尺寸提示是小部件的最小可能尺寸,但如果需要,它仍然可以变大。
  • 首选尺寸策略 - 小部件可以缩小或增大到大于其尺寸提示。
  • 扩展尺寸策略 - 小部件可以缩小或变大,超过其尺寸提示:)

You may want to ask,

What is the difference between preferred and expanding?
**Answer:** Imagine a form with 2 widgets, one with preferred and another with expanding. Then any extra space will be given to the widget with the expanding policy. The widget with the preferred policy will remain at its size hint.

我建议(警告:我不是专家:))您购买并通读C++ GUI 编程与 QT 第二版”。我目前正在阅读它,它很有意义。看看这些图像,看看它们是否有意义。

解释尺寸政策​​
尺寸政策说明

一个简单的示例
这是一个简单的对话框,有 2 个按钮,其水平和垂直尺寸策略显示为水平和垂直拉伸。

example

这是最小尺寸的预览。
alt text

这是另一个更大尺寸的预览
alt text

[编辑://添加尺寸提示示例]

为什么您应该关心 SIZEHINT< br>
您可以看到每个小部件都有一个 sizeHint,这至关重要,因为 QT 的布局系统始终尊重 sizeHint。仅当小部件的默认大小不完全符合您的要求时,这才会出现问题。解决此问题的唯一方法是扩展(子类化)小部件并重新实现其 sizeHint() 成员函数。一个例子相当于1000字。为了节省空间,请参阅我的博客,其中有一个示例项目。

Layouts are actually easy to understand "I think". :)
A simple explanation of layouts can be found in the QT book "C++ Gui programming with QT 2nd edition"

What you should be aware of regarding layouts and their size policies

  • Most Qt widgets have a size policy. This size policy tells the system how the widget should stretch or shrink. It's got from the class QSizePolicy. A size policy has both vertical and horizontal components.
  • Most widgets also have a size Hint. This size hint tells the system a widgets preferred size
  • QSizePolicy has a stretch factor to allow widgets to grow at different rates

**I am only familiar with 4 size policies**

  • fixed size policy - The size of the widget is fixed and it can't be stretched. It remains at its size hint.
  • minimum size policy - The size hint is the smallest possible size of the widget, but it _can still_ grow bigger if necessary.
  • Preferred size policy - the widget can shrink or grow bigger than its size hint.
  • expanding size policy - the widget can shrink or grow bigger than its size hint :)

You may want to ask,

What is the difference between preferred and expanding?
**Answer:** Imagine a form with 2 widgets, one with preferred and another with expanding. Then any extra space will be given to the widget with the expanding policy. The widget with the preferred policy will remain at its size hint.

I recommend (WARNING: am not an expert :)) you buy and read through "C++ Gui programming with QT 2nd edition". I am currently reading it and it is making a lot of sense. Look at the images and see if they make some sense.

Explaining size policies
Size policy explained

A simple example
This is a simple dialog with 2 buttons whose horizontal and vertical size policies are shown as are the horizontal and vertical stretch.

example

Here is the preview at its smallest size.
alt text

Here is another preview at a larger size
alt text

[EDITED: //added size hint example]

WHY YOU SHOULD CARE ABOUT SIZEHINT
You can see that every widget has a sizeHint which is vital because QT's layout system always respects the sizeHint. This is only a problem if the default size of the widget is not exactly what you want. The only way around this problem is to extend (subclass) the widget and reimplement its sizeHint() member function. An example is worth 1000 words. To save space, see my blog where there is an example project.

短暂陪伴 2024-10-16 17:14:53

根据其 文档

当您将小部件添加到布局时,布局过程的工作原理如下:

  1. 所有小部件最初都会根据其 QWidget::sizePolicy() 和 QWidget::sizeHint() 分配一定的空间量。

  2. 如果任何小部件设置了拉伸因子,且拉伸因子的值大于零,则它们将按照拉伸因子的比例分配空间(如下所述)。

  3. 如果任何小部件的拉伸因子设置为零,则只有在没有其他小部件需要空间时,它们才会获得更多空间。其中,空间首先分配给具有扩展大小策略的小部件。

  4. 任何分配的空间小于其最小大小(如果未指定最小大小则为最小大小提示)的小部件都会分配它们所需的最小大小。 (小部件不必具有最小尺寸或最小尺寸提示,在这种情况下,拉伸因子是它们的决定因素。)

  5. 任何分配的空间超过其最大大小的小部件都会分配它们所需的最大大小空间。 (小部件不必具有最大尺寸,在这种情况下,拉伸因子是它们的决定因素。)

并且 sizeHint() 是 QWidget 的建议大小,而 Layout父窗口小部件的 > 将考虑 sizeHint()sizePolicy() 来确定子窗口小部件可以容纳的空间。

According to its docs:

When you add widgets to a layout, the layout process works as follows:

  1. All the widgets will initially be allocated an amount of space in accordance with their QWidget::sizePolicy() and QWidget::sizeHint().

  2. If any of the widgets have stretch factors set, with a value greater than zero, then they are allocated space in proportion to their stretch factor (explained below).

  3. If any of the widgets have stretch factors set to zero they will only get more space if no other widgets want the space. Of these, space is allocated to widgets with an Expanding size policy first.

  4. Any widgets that are allocated less space than their minimum size (or minimum size hint if no minimum size is specified) are allocated this minimum size they require. (Widgets don't have to have a minimum size or minimum size hint in which case the stretch factor is their determining factor.)

  5. Any widgets that are allocated more space than their maximum size are allocated the maximum size space they require. (Widgets do not have to have a maximum size in which case the stretch factor is their determining factor.)

And sizeHint() is the recommended size of a QWidget, and the Layout of the widget parent will take sizeHint() and sizePolicy() into consideration to determine the space of the child widget can hold.

我不在是我 2024-10-16 17:14:53

您可以使用 QT 样式表来控制小部件的高度
以及其他属性以简单的可定制方式。

http://doc.qt.io/archives/qt-4.7/stylesheet.html

至于布局,你需要明智而有力地使用它们
与垫片结合以使小部件表现良好
完全按照您希望的方式进行。

http://doc.qt.io/archives/qt-4.7/designer -layouts.html

You can use QT Style Sheets to control the widgets height
and other properties in an easy customizable way.

http://doc.qt.io/archives/qt-4.7/stylesheet.html

As for the layouts you need to use them wisely and strongly
coupled with spacers in order to make the widgets behave
exactly the way you want them to.

http://doc.qt.io/archives/qt-4.7/designer-layouts.html

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