简体   繁体   中英

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. It allocates a QLayoutItem, somehow associates it with the widget and then adds the QLayoutItem to the layout? 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.

Take a simple example: you have a vertical layout that is 300px wide. That means each layout item that you add will also be 300px wide. Now imagine adding a 50x50 widget into it. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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