1
votes

I want to reuse parts of Qt layout between different widgets/windows/dialogs and have the reused parts respect the layout spacing of the top-level widget's layout.

For reusing parts I currently create separate classes derived from QWidget for the parts I want to reuse (each having their own layout). However, when nesting widgets with their own layout instead the nested widgets get a content-margin of their own and have a spacing of their own. They will not respect the spacing of the parent widget's layout.

In Qt, it's possible to nested layouts, which gives a content-margin around the outside, and equal spacing between widgets. All widgets have the same parent (the main widget/window), and only the layouts are nested. Spacing is inherited to sub-layouts.

Is there a way to achieve this while reusing parts of the layout, so that the nested parts respect the spacing of the parent layout?

Alternatively, can I detect when the widget is inserted into a layout and apply the parent's layout's properties instead of using the defaults?

1
I think that there may be a way of lifting a whole layout from a widget, so that the widget is just an unwitting carrier of a layout. You may wish to try that.Kuba hasn't forgotten Monica

1 Answers

0
votes

I am guessing you are doing something like this in the main widget:

mainLayout->addWidget(subWidget1)
mainLayout->addWidget(subWidget2)

Did you try doing it like this:

mainLayout->addLayout(subWidget1->layout())
mainLayout->addLayout(subWidget2->layout())