了解Qt中的表单布局机制 [英] Understanding form layout mechanisms in Qt
问题描述
Qt
有一个灵活而强大的布局机制来处理桌面应用程序窗口的视图。
非常灵活,几乎无法理解,出现问题时需要进行微调。如此强大,它可以击败任何人试图压倒Qt对形式应该看起来的看法。
那么,任何人都可以解释或提供文章或来源Qt的定位机制?我试图强制 。我的表单在google文档上的ui文件。在名称中搜索____,在QtDesigner中预览 布局实际上很容易理解我认为: ) 您应该了解关于布局和尺寸策略的信息 我只熟悉4个尺寸政策 您可能会问, 首选和扩展? 我推荐(警告:我不是专家:))您购买并通读 strong> C ++ Gui使用QT第二版编程 。我目前正在阅读它,并且它很有意义。看看这些图像,看看它们是否有意义。 解释尺码政策 一个简单的例子 这是另一个预览, strong> [ EDITED://添加大小提示示例] 为什么您需要关注SIZEHINT 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 .ui file of my form on google docs. Search '____' in names, preview in QtDesigner Layouts are actually easy to understand "i think" :) What you should be aware of regarding layouts and their size policies I am only familiar with 4 size policies You may want to ask, What is the difference between preferred and expanding? 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 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 这篇关于了解Qt中的表单布局机制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋! QLabel
, QPushButton
和
下方。 QTableView
,它们名称后面的下划线标记为比 QTextBrowser
高两倍, code> verticalStretch = 1
一个简单的布局解释可以在qt book 使用QT第二版的C ++ Gui编程
回答:想象一个带有2个小部件的表单,其中一个具有首选项,另一个具有展开状态,然后通过扩展策略将任何额外空间赋予小部件。
这是一个简单的对话框,包含2个按钮的水平和垂直尺寸政策显示为横向和纵向拉伸。
您可以看到每个窗口小部件有一个很重要的尺寸提示,因为QT的布局系统始终尊重尺寸提示。这只是一个问题,如果小部件的默认大小不完全是你想要的。解决这个问题的唯一方法是扩展(子类)小部件并重新实现它的sizeHint()成员函数。一个例子值1000字。为了节省空间,请参阅我的博客,其中有一个示例项目。Qt
has a flexible and powerful layout mechanism to handle view of desktop application's windows.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?
A simple explanation of layouts can be found in the qt book "C++ Gui programming with QT 2nd edition"
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.
This is a simple dialog with 2 buttons whose horizontal and vertical size policies are shown as are the horizontal and vertical stretch.
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.