Reputation: 2655
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.
Upvotes: 5
Views: 2554
Reputation: 29966
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.
Upvotes: 5