Qt:布局和小部件有什么区别? [英] Qt : what is the difference between layout and widget?

查看:29
本文介绍了Qt:布局和小部件有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我认为布局只是一个小部件,可以在里面保留更多小部件.但是现在我发现我无法使用 addWidget 将它添加到其他小部件.例如如何向 QSplitter 添加布局?

I thought that layout is just a widget that keeps more widgets inside. But now I find that I can't add it to other widgets using addWidget. For instance how to add a layout to QSplitter?

推荐答案

QWidget 有通过 layout() 内置对布局的支持和 setLayout(...) 函数.布局对象控制可以添加到布局中的不同子小部件的定位.此外,它确保根据 约束 指定.如果布局还没有父小部件,那么只要布局通过 setLayout() 附加到小部件,它就会设置该父小部件的大小.

QWidget has built in support for layouts through the layout() and setLayout(...) functions. The layout object controls the positioning of different child widgets that may be added to the layout. In addition, it makes sure to correctly size its parent widget based on the constraints specified. If the layout does not yet have a parent widget, then as soon as the layout is attached to a widget with setLayout(), it will set the size of that parent widget.

但是,有些小部件更像是一个布局管理器而不是小部件,例如 QSplitterQTabWidget.例如,考虑 QSplitter.虽然是一个小部件,但它提供了两个可以使用的不同区域.在这种情况下,两个不同区域的单个布局管理器实际上没有意义.与 QSplitter 一样,QTabWidget 也有一些行为,这使得单个布局不仅没有必要而且没有用.

But, some widgets are more like a layout manager than a widget, such as QSplitter and QTabWidget. Consider, for example, QSplitter. Although a single widget, it presents two different areas that may be worked with. In this case, a single layout manager for two different areas doesn't really make sense. Like QSplitter, QTabWidget has some behaviors which make a single layout not only unnecessary but not useful.

我认为正是上述布局和小部件的融合使得布局和小部件的分离有时令人困惑.

I think it's the above melding of layout and widget that makes the separation of layout and widget sometimes confusing.

这篇关于Qt:布局和小部件有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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