Qt4 layout in a groupbox
I am trying to make a QGroupBox
to have the size of it's content, to fit the content.
I think I do not understand exactly how this works and I get in this strange situation:
I have 2 buttons in a horizontal layout and I placed this inside a QGroupBox
. I added a slot to print debugging info when clicking a button (so the window is fully loaded when the debugging code is run).
The problem is that the QGroupBox
's sizeHint
returns (-1,-1) but the horizontal layout inside the QGroupBox
has a correct sizeHint
.
Any idea why the sizeHint in the QGroupBox
is (-1,-1)? I think this problem causes all my other problems, because if the sizeHint
is invalid then the sizeConstraint
and sizePolicy
will have no effect on the QGroupBox
. I was expecting the parent widget to have a little larger sizeHint
than the children layout.
The widget开发者_运维技巧s and layouts are created with the Designer (maybe this is relevant, I have a complex interface so I must use the Designer).
Update: I looked at the code generated in the setupUi
method and it seems that a intermediary widget is created, the layout with the buttons is placed on the widget and that widget is placed on the QGroupBox
. I think this is for the feature that in the designer you can place a layout inside a widget and also can resize it.
Can I skip this widget when using the designer?
. I found the solution, in designer you can select the widgets you want layout and then click the layout in grid/form/horizontal etc button/menu . This option will create a invisible widget, put the selected widgets on the layout and set that layout to the invisible widget. To avoid this you cave to click the groupbox and hit the layout context menu and set the menu, this will align the widgets but the layout is not visible in the widgets tree.
I guess this because you didn't actually set the layout to your QGroupBox
widget. See if adding the line below into your window constructor would solve the problem:
ui->groupBox->setLayout(ui->horizontalLayout);
where groupBox and horizontalLayout are QGroupBox
and QHBoxLayout
in question.
精彩评论