I have a Gridlayout containing a widget and I dynamically want to replace that item from the layout. I do know that if you delete the widget it will remove it from the layout but the space within that cell doesn’t update, so when I insert the new widget the space is still showing what was previously needed.
How do I in python code update the cells width after I insert the new item ?
To clarify my previous question which may have been confusing…
I keep making the mistake thinking a layout is a container which is not. Yes it does contain widgets but it’s the widgets that sets it’s size which then gets aligned within the layout. At least I think it’s doing this…
My issue was resolved by using a proper widget (QTableWidget) which will contain multiple other widgets and allow for its size to be set (row, col, setFixedSize). Then when I add the QTableWidget to the parent grids cell (setcellWidget) the parent grid sizes and aligns properly.
Hey @weave yes this can be confusing. Layouts expand to fill their container, but widgets don’t necessarily. Sizing policy is set at the widget level, although the layout ultimately decides how much space a widget gets (balanced with the other widgets in the layout) a widget doesn’t necessarily adapt to the size it’s given. You can see this if you try and put too many widgets in a layout, they’ll end up being half drawn rather than shrinking to oblivion.
A good way to think of it is that the layout is the arbiter between the widgets size policies. But it still catches me out from time to time.
As to your question, it’s a bit weird that the layout didn’t update when you added/removed something from it. This should happen automatically. If you have some example code I’d be interested in having a look & figuring out what is going on. But I’m glad you’ve found a solution.
I blew away the original code but I think I can describe where I was going wrong.
When I deleted the grid’s cell contents I replaced it with a Vertical Boxlayout and added a QLabel and QLineEdit. to it. When I added the Vertical to the Grid the widgets did not line up properly and after fiddling with margins for both I gave up and went with the table widget.