You have a few solutions, these come to mind:
- If it is as simple case as your question shows, then just remove
cb2
from theQGridLayout
usingQLayout::removeWidget()
and re-add it to new position. - Instead of using
QGridLayout
directly for rows, make each row a nestedQHBoxLayout
(main layout can beQGridLayout
with just 1 column, orQVBoxLayout
). It supports inserting in the middle. - If you want to use
QGridLayout
, and real use case is more complex than shown in the question, then just re-create the layout from scratch when you need to insert widgets. Write a method, which deletes current layout (if it exists, ie. not first call), then creates a new layout and puts all widgets in it. Then you call this from constructor, and also when you add widgets, without duplicating code.