简体   繁体   English

QLayoutItem和QWidget之间的Qt布局关系

[英]Qt Layout relationship between QLayoutItem and QWidget

this isn't well clarified in the documentation, but what really happens when for instance, I call QBoxLayout::insertWidget. 这在文档中还没有很好地阐明,但是例如当我调用QBoxLayout :: insertWidget时会发生什么。 It allocates a QLayoutItem, somehow associates it with the widget and then adds the QLayoutItem to the layout? 它分配一个QLayoutItem,以某种方式将其与小部件关联,然后将QLayoutItem添加到布局中? Why the need for such an indirection? 为什么需要这种间接方式? I'm making a custom layout and want to be able to insert widgets at any index of that layout, however I am not fully aware of the mechanics. 我正在制作自定义布局,希望能够在该布局的任何索引处插入小部件,但是我并不完全了解其机制。

Because layouts operate layout items, not widgets. 因为布局操作布局项目,而不是小部件。 QLayoutItem contains a list of it's own functions that are used to position the layoutitem inside the layout, and properly resize/align it. QLayoutItem包含它自己的函数的列表,这些函数用于将layoutitem放置在布局内,并适当调整其大小/对齐。

Take a simple example: you have a vertical layout that is 300px wide. 举一个简单的例子:您有一个300px宽的垂直布局。 That means each layout item that you add will also be 300px wide. 这意味着您添加的每个布局项目也将为300px宽。 Now imagine adding a 50x50 widget into it. 现在,假设将50x50小部件添加到其中。 Since the layout has it's own geometry, sizeHing, and other stuff, you will be able to properly insert the widget (the layoutItem will stay 300px wide, and the widget will stay 50px wide, nothing will break), which would be hard/impossible if you operated widgets directly. 由于布局具有自己的几何形状,sizeHing和其他内容,因此您将能够正确地插入小部件(layoutItem的宽度保持为300px,小部件的宽度保持为50px,什么都不会破坏),这很难/不可能如果您直接操作小部件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM