QGroupBox not fit internal layout
-
Hi. I broke with QGroupBox, could you help me with the following:
Try to do a simple thing.
Add multiple QGroupBox into vertical layout.
Add layout to each box.Widgets inside box not fit, their cutted (see image)
Wigets inside is simple:
auto* lab = new QLabel("Test"); lab->setFixedSize(500, 200);
QGroupBox could be minimized. How to fit internal widgets?
I try adjust, try to change sizePolicy. No effect.groupBox->adjustSize();
Seems like layout problem, since widget size defined by layout.
Here is a full code:
auto* vlay = new QVBoxLayout(); for(int i = 0; i != 2; ++i) { auto* groupBox = new QGroupBox(cfgTagList[i], this); groupBox->setCheckable(true); groupBox->setAlignment (Qt::AlignLeft); auto* lay = new QVBoxLayout(this); auto* lab = new QLabel("Test"); lab->setFixedSize(500, 200); lay->addWidget(lab); groupBox->setLayout(lay); groupBox->adjustSize(); vlay->addWidget(groupBox); } this->adjustSize();
-
A
QGroupBox
has some specific features, and so has aQDialog
. They don't work together in your case, but hat doesn't constitute a bug.
The documentation ofQDialog
says directly at the beginning:A dialog window is a top-level window mostly used for short-term tasks and brief communications with the user.
It seems the use case is neither short term, nor brief. If it's all about plugins, there's also no need for the power of
QDialog
. You could just add two push buttons to a simpleQWidget
at the bottom of a layout.Without having researched it in Detail: A group box automatically has a
Preferred
size policy and it's a control type of its own.
E.g. the fusion style adds some space below the group box, which is accounted for inQGroupBox::minimumSizeHint()
. That could be a reason.@SergeyK12 said in QGroupBox not fit internal layout:
Try to combine with scroll (it will be good enough if internal widget in QScrollWidget could be any size it should be).
There is no such thing as a
QScrollWidget
. Do you mean aQScrollArea
? If you do, have you tried it in combination with group boxes in a dialog? -
Hi,
Which version of Qt ?
On which version of Windows ?
Can you please provide a minimal compilable example to reproduce this ?
The snippet you provided does not include the dialog creation nor the buttons so this might hide something else regarding your issue. -
Hi,
Which version of Qt ?
On which version of Windows ?
Can you please provide a minimal compilable example to reproduce this ?
The snippet you provided does not include the dialog creation nor the buttons so this might hide something else regarding your issue. -
The dialog is too small to fit two fixed size labels (plus a group box overhead for each). That’s why the group boxes are both cropped. Increase the dialog size and the issue goes away :-)
-
@SergeyK12
QDialog could adjust size to it content:
But if i push this QLabel into a layout into QGroupBox
Its not ajust QGroubBox layout.
But if i do the same withoun QDialog (test example with QMainWindow)
everything is fine -
As soon as the requested minimum size does not fit into the layout (as it seems in your example) the layout manager can't do anything.
This is working fine:int main(int argc, char **argv) { QApplication a(argc, argv); QWidget w; auto vlay = new QVBoxLayout(&w); for (int i = 0; i != 2; ++i) { auto groupBox = new QGroupBox("GroupBox"); groupBox->setCheckable(true); groupBox->setAlignment(Qt::AlignLeft); auto* lay = new QVBoxLayout; auto* lab = new QLabel("Test"); lab->setFixedSize(500, 200); lay->addWidget(lab); groupBox->setLayout(lay); groupBox->adjustSize(); vlay->addWidget(groupBox); } w.show(); return app.exec(); }
-
As soon as the requested minimum size does not fit into the layout (as it seems in your example) the layout manager can't do anything.
This is working fine:int main(int argc, char **argv) { QApplication a(argc, argv); QWidget w; auto vlay = new QVBoxLayout(&w); for (int i = 0; i != 2; ++i) { auto groupBox = new QGroupBox("GroupBox"); groupBox->setCheckable(true); groupBox->setAlignment(Qt::AlignLeft); auto* lay = new QVBoxLayout; auto* lab = new QLabel("Test"); lab->setFixedSize(500, 200); lay->addWidget(lab); groupBox->setLayout(lay); groupBox->adjustSize(); vlay->addWidget(groupBox); } w.show(); return app.exec(); }
@Christian-Ehrlicher
As you mention, Its perfectly work without QDialog.
In my previous post third example (it work with any number of boxes)
Thats mean that requested minimum size inside QGroupBox fit without QDialog.Why it doesn't with QDialog?
It also work with QDialog without Boxes (first imфge in previous post).
-
@Christian-Ehrlicher
As you mention, Its perfectly work without QDialog.
In my previous post third example (it work with any number of boxes)
Thats mean that requested minimum size inside QGroupBox fit without QDialog.Why it doesn't with QDialog?
It also work with QDialog without Boxes (first imфge in previous post).
@SergeyK12 said in QGroupBox not fit internal layout:
Why it doesn't with QDialog?
Because, as I wrote above, you did not set a minimum size on your QDialog but your QWidget or whatever so the layout constraints inside are greater than your minimum size.
-
@SergeyK12 said in QGroupBox not fit internal layout:
Why it doesn't with QDialog?
Because, as I wrote above, you did not set a minimum size on your QDialog but your QWidget or whatever so the layout constraints inside are greater than your minimum size.
@Christian-Ehrlicher
Sorry, i missunderstand.I set sizes only for internal widgets.
Im not set minimum size in other working examples. So you didnt in yours.
(it calc size for any number of wigets without QGroupBox).With QGroupBox there is additional layouts and thats the case.
I should set minimum size for QDialog?
Its some specific behaviour of QGroupBox or QDialog? (since its work with QMainWindow etc)
-
If size of internal widget not fixed by enything its ok, but seems like nothing.
Try to combine with scroll (it will be good enough if internal widget in QScrollWidget could be any size it should be).
But fixing internal leed to this post discussion.But its perfect (as it should be) without GroupBox (i cant understand this)
-
A
QGroupBox
has some specific features, and so has aQDialog
. They don't work together in your case, but hat doesn't constitute a bug.
The documentation ofQDialog
says directly at the beginning:A dialog window is a top-level window mostly used for short-term tasks and brief communications with the user.
It seems the use case is neither short term, nor brief. If it's all about plugins, there's also no need for the power of
QDialog
. You could just add two push buttons to a simpleQWidget
at the bottom of a layout.Without having researched it in Detail: A group box automatically has a
Preferred
size policy and it's a control type of its own.
E.g. the fusion style adds some space below the group box, which is accounted for inQGroupBox::minimumSizeHint()
. That could be a reason.@SergeyK12 said in QGroupBox not fit internal layout:
Try to combine with scroll (it will be good enough if internal widget in QScrollWidget could be any size it should be).
There is no such thing as a
QScrollWidget
. Do you mean aQScrollArea
? If you do, have you tried it in combination with group boxes in a dialog? -
The dialog is too small to fit two fixed size labels (plus a group box overhead for each). That’s why the group boxes are both cropped. Increase the dialog size and the issue goes away :-)
@Axel-Spoerl said in QGroupBox not fit internal layout:
Increase the dialog size and the issue goes away :-)
Isn't this what the outer layout is for?
@SergeyK12 In the short snippet you have showed
vlay
is not set as the layout of your dialog before you callthis->adjustSize()
. However, there is always a slight problem with this: Anything to do with the layout calculations only works after the dialog is shown. So, adjustSize() might work if you callthis->show()
directly before.In general, you should rarely have a need to use fixed sizes. Everything should be handled by layouts. There might be a problem (I'm not entirely sure) that the fixed size does not communicate its behavior to the layout. The minimum size and size hint might tell the layout something entirely different. I personally would use
setMinimumSize()
instead ofsetFixedSize()
. If you want to avoid for the label to be larger than the minimum size, you should set the size policy (both horizontal and vertical size policy) toQSizePolicy::Fixed
. If the user still tries to make the dialog larger, something needs to be resized. You can add a QSpacerItem with zero height/width some place in your layouts to account for that (for box layouts you can useaddStretch(0)
directly). -