且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Qt:布局和小部件有什么区别?

更新时间:2023-11-14 16:29:04

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.