Individual content margin in layout
-
wrote on 4 Jan 2021, 15:51 last edited by
Is it possible to have individual content margins for each widget in layout?
I've try to find the answer and what I found was that "probably not". Looking at https://doc.qt.io/qt-5/layout.html there is only little info about margins. In https://doc.qt.io/qt-5/qwidget.html#setContentsMargins there is more info, which says that the margins set to widget should be respected in layout. But it doesn't work for me.
For instance, let have QHBoxLayout. I add 3 buttons there. The first button should have left margin 5, others 0. The second button should have left and right margin 2, others 0. The third button should have right margin 5, others zero. But when I use
setContentsMargins ()
the margins are ignored or widgets are "shrinked" respectively.Using the CSS for margins break the layout in some instances.
Using embedded layouts hack as helpers is slow if there are lot of widgets.
-
Hi,
I do not think that you can have this with the set of default layouts. However you can create your own to support that.
Out of curiosity, what kind of UI are you building with these different margins requirements ?
-
Hi,
I do not think that you can have this with the set of default layouts. However you can create your own to support that.
Out of curiosity, what kind of UI are you building with these different margins requirements ?
wrote on 4 Jan 2021, 19:41 last edited byThanks for the reply, @SGaist .
I'm loading the wxWidgets resources (.xrc) in Qt, where you can have individual borders around widgets (the borders are used for separating important widgets, squeezing some rows of widgets, ...). So I want the "output" to be as close as possible as in wxWidgets.
-
Do you have an image of such an interface ? It might give ideas on how it could be redone.
-
wrote on 4 Jan 2021, 20:56 last edited by
Unfortunately I cannot provide the images of the work I'm working on. But I've made some simple "form".
It's nothing fancy. Just every widget could have margin on each of it side or none at all.
In the image, all of the widgets don't have "top" margin set. The selected widget has only right and bottom margin set. The top and and left is disabled.
-
Is it possible to have individual content margins for each widget in layout?
I've try to find the answer and what I found was that "probably not". Looking at https://doc.qt.io/qt-5/layout.html there is only little info about margins. In https://doc.qt.io/qt-5/qwidget.html#setContentsMargins there is more info, which says that the margins set to widget should be respected in layout. But it doesn't work for me.
For instance, let have QHBoxLayout. I add 3 buttons there. The first button should have left margin 5, others 0. The second button should have left and right margin 2, others 0. The third button should have right margin 5, others zero. But when I use
setContentsMargins ()
the margins are ignored or widgets are "shrinked" respectively.Using the CSS for margins break the layout in some instances.
Using embedded layouts hack as helpers is slow if there are lot of widgets.
@Trigve said:
For instance, let have QHBoxLayout. I add 3 buttons there. The first button should have left margin 5, others 0. The second button should have left and right margin 2, others 0. The third button should have right margin 5, others zero. But when I use setContentsMargins () the margins are ignored or widgets are "shrinked" respectively.
Using
setContentsMargins()
directly on a widget is kinda hit and miss. I don't think it's a very much used feature and I found several problems with it on some widgets. It's much more common to change margins and spacing of the encompassing layout.The layout you describe (5, button, 2, button, 2, button, 5) is easily achieved this way:
hboxlayout->addWidget(button1); hboxlayout->addWidget(button2); hboxlayout->addWidget(button3); hboxlayout->setContentsMargins(5,0,5,0); // left and right = 5, top and bottom = 0 hboxlayout->setSpacing(2); //spacing between buttons = 2
It's not a direct translation from wxWidgets, but Qt has a bit different philosophy of the layouts. In Qt the widgets are not usually responsible for how they are laid out in their surroundings. It's the layout's role. There is the
setContentsMargins()
directly on widgets too, but, like I said, IMO it's a bit of a misfeature and I suggest to stay away from it. -
@Trigve said:
For instance, let have QHBoxLayout. I add 3 buttons there. The first button should have left margin 5, others 0. The second button should have left and right margin 2, others 0. The third button should have right margin 5, others zero. But when I use setContentsMargins () the margins are ignored or widgets are "shrinked" respectively.
Using
setContentsMargins()
directly on a widget is kinda hit and miss. I don't think it's a very much used feature and I found several problems with it on some widgets. It's much more common to change margins and spacing of the encompassing layout.The layout you describe (5, button, 2, button, 2, button, 5) is easily achieved this way:
hboxlayout->addWidget(button1); hboxlayout->addWidget(button2); hboxlayout->addWidget(button3); hboxlayout->setContentsMargins(5,0,5,0); // left and right = 5, top and bottom = 0 hboxlayout->setSpacing(2); //spacing between buttons = 2
It's not a direct translation from wxWidgets, but Qt has a bit different philosophy of the layouts. In Qt the widgets are not usually responsible for how they are laid out in their surroundings. It's the layout's role. There is the
setContentsMargins()
directly on widgets too, but, like I said, IMO it's a bit of a misfeature and I suggest to stay away from it.wrote on 5 Jan 2021, 07:41 last edited by@Chris-Kawa, thanks for the reply.
The example I've posted is just example. Using layout's
setContentsMargins()
could be used only for some simple stuff.I'll try to look at custom layouts as the @SGaist suggested.
1/7