了解 Qt 中的表单布局机制 [英] Understanding form layout mechanisms in Qt

查看:27
本文介绍了了解 Qt 中的表单布局机制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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.

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

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

我试图强制 QLabelQPushButtonQTableView,它们的名称中以尾随下划线标记,比它们高两倍QTextBrowser 下面有 verticalStretch = 1.如何正确处理小部件的高度?

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?

我在google上的表单的.ui文件.在名称中搜索____",在 QtDesigner 中预览

推荐答案

布局实际上很容易理解我认为".:)
布局的简单解释可以在 QT 书中找到使用 QT 2nd 版进行 C++ Gui 编程"

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"

关于布局及其尺寸政策的注意事项

  • 大多数 Qt 小部件都有大小策略.此大小策略告诉系统小部件应如何拉伸或收缩.它来自 QSizePolicy 类.尺寸策略同时包含垂直和水平组件.
  • 大多数小部件也有大小提示.这个尺寸提示告诉系统小部件的首选尺寸
  • QSizePolicy 有一个拉伸因子,允许小部件以不同的速率增长
  • 固定大小策略 - 小部件的大小是固定的,不能被拉伸.它保持其大小提示.
  • 最小尺寸策略 - 尺寸提示是小部件的最小可能尺寸,但如果需要,它_仍然可以_变大.
  • 首选尺寸政策 - 小部件可以缩小或增长到比其尺寸提示更大的尺寸.
  • 扩大尺寸政策 - 小部件可以缩小或增长到比它的尺寸提示大:)

我推荐(警告:我不是专家:))您购买并通读 "C++ Gui 编程与 QT 第二版".我目前正在阅读它,它很有意义.查看图片,看看它们是否有意义.

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.

解释尺寸政策​​

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

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.

这是最小尺寸的预览.

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

为什么你应该关心尺寸提示
您可以看到每个小部件都有一个 sizeHint,这很重要,因为 QT 的布局系统始终尊重 sizeHint.如果小部件的默认大小不是您想要的,这只是一个问题.解决此问题的唯一方法是扩展(子类化)小部件并重新实现其 sizeHint() 成员函数.一个例子值 1000 字.为了节省空间,请参阅我的博客,其中有一个示例项目.

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.

这篇关于了解 Qt 中的表单布局机制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆