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

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

问题描述

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

非常灵活,几乎无法理解,出现问题时需要进行微调。如此强大,它可以击败任何人试图压倒Qt对形式应该看起来的看法。

那么,任何人都可以解释或提供文章或来源Qt的定位机制?



我试图强制 QLabel QPushButton QTableView ,它们名称后面的下划线标记为比 QTextBrowser 高两倍, code> verticalStretch = 1 下方。

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

解决方案

布局实际上很容易理解我认为: )

一个简单的布局解释可以在qt book 使用QT第二版的C ++ Gui编程

您应该了解关于布局和尺寸策略的信息


  • 大多数Qt窗口小部件都有尺寸策略。这个尺寸策略告诉系统小部件应该如何伸展或缩小。它来自QSizePolicy类。尺寸政策同时包含纵向和横向组件。

  • 大多数小部件的大小也都有提示。这个大小提示告诉系统一个小部件优先大小

  • QSizePolicy有一个伸缩因子,允许小部件以不同的速度增长



我只熟悉4个尺寸政策


  • 固定尺寸​​政策 - 尺寸的小部件是固定的,它不能被拉伸。它仍然在它的大小提示。

  • 最小尺寸策略 - 尺寸提示是小部件可能的最小尺寸,但它仍然可以根据需要变大。

  • 首选大小策略 - 小部件可以缩小或增大大于其大小提示。

  • 扩展大小策略 - 小部件可以缩小或增大大于其大小的提示: li>


您可能会问,

首选和扩展?

回答:想象一个带有2个小部件的表单,其中一个具有首选项,另一个具有展开状态,然后通过扩展策略将任何额外空间赋予小部件。



我推荐(警告:我不是专家:))您购买并通读 strong> C ++ Gui使用QT第二版编程 。我目前正在阅读它,并且它很有意义。看看这些图像,看看它们是否有意义。



解释尺码政策



一个简单的例子

这是一个简单的对话框,包含2个按钮的水平和垂直尺寸政策显示为横向和纵向拉伸。




这是另一个预览, strong>



[ EDITED://添加大小提示示例]



为什么您需要关注SIZEHINT

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


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?

.ui file of my form on google docs. Search '____' in names, preview in QtDesigner

解决方案

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. Its 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 preffered 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 its making alot of sense. Look at the images and see if they make some sense.

Explaining size policies

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.

Here is the preview at its smallest size.

Here is another preview at a larger size

[EDITED: //added size hint example]

WHY SHOULD YOU 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天全站免登陆