Qt
具有灵活且强大的布局机制来处理桌面应用程序窗口的视图。
但它是如此灵活,以至于当出现问题并需要微调时,它几乎无法被理解。而且如此强大,以至于它可以击败任何试图压倒 Qt 关于表单外观的观点的人。
那么,谁能解释一下,或者提供一下Qt定位机制的文章或者来源吗?
我试图强制 QLabel
、QPushButton
和 QTableView
(在其名称中用尾随下划线标记)比 高两倍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?
发布评论
评论(3)
布局实际上很容易理解“我认为”。 :)
布局的简单解释可以在 QT 书籍“使用 QT 第 2 版进行 C++ Gui 编程"
有关布局及其大小策略的注意事项< /strong>
**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 个按钮,其水平和垂直尺寸策略显示为水平和垂直拉伸。
这是最小尺寸的预览。
这是另一个更大尺寸的预览
[编辑://添加尺寸提示示例]
为什么您应该关心 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
**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.
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
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 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.根据其 文档:
并且
sizeHint()
是 QWidget 的建议大小,而Layout
父窗口小部件的 > 将考虑sizeHint()
和sizePolicy()
来确定子窗口小部件可以容纳的空间。According to its docs:
And
sizeHint()
is the recommended size of a QWidget, and theLayout
of the widget parent will takesizeHint()
andsizePolicy()
into consideration to determine the space of the child widget can hold.您可以使用 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