My widget has two layouts. The main layout is outerLayout, and it should always occupy the entire widget. This contains a QLabel and a second layout named innerLayout.
innerLayout should be centered inside outerLayout and it should only be large enough to contain its widgets. I've set size constraints for both layouts, but in the following code, innerLayout expands to fill outerLayout.
outerLayout = new QVBoxLayout();
outerLayout->addWidget(label);
outerLayout->setSizeConstraint(QLayout::SetMaximumSize);
innerLayout = new QGridLayout();
innerLayout->setAlignment(Qt::AlignHCenter);
innerLayout->setSizeConstraint(QLayout::SetFixedSize);
outerLayout ->addLayout(innerLayout);
setLayout(outerLayout);
If I set the size constraint for both layouts to SetFixedSize, both layouts will be reduced to their minimum size. But I want the outer layout to occupy the entire space and the inner layout to use the minimum possible space. Any ideas?
Use spacers as extra widgets of outerlayout. In other words: the spacers will be siblings of innerlayout and will push inner layout to it's minimal needed surface. You will need horizontal and vertical spacers.
Related
I have a small program which contains a QGroupBox with other widgets like this:
I tried many ways to manage the size of the QGroupBox to make the height as the same as the rest of the parts. Except for the way of using setMaximumHeight, because I want the size to change dynamically with the window size too. what else can I do to manage the layout?
Right now there are three items in a layout. The layout will try to fill the available space. QLineEdit and QSpinBox (or whatever your second widget is) have SizePolicy.vertical == fixed, so all extra space goes to the QGroupBox.
You have these choices:
Add a vertical spacer as fourth item below the groupbox to your layout.
Set maximum height of your groupbox - then the remaining space will be evenly spaced between the items.
Adjust the size of your window / widget / dialog (in Qt Designer or via code).
For exemple, I want the value of the layoutLeftMargin property to be equal to 1/3 of the parent widget. So when I will resize the windows, the ratio of the widget will still stay the same.
Else, if it's not possible with QtDesigner, how can I do it with code ?
No, Margins are specified in pixels, they can't be relative to the parent widget's size.
However, You can do that in the designer by putting your whole current layout in a Horizontal Layout, add a Horizontal Spacer to the left of it, assign suitable layoutStretch values in the horizontal layout (In your example, this should be 1,2, meaning the original layout will take up twice the space taken up by the spacer, so that the spacer gets 1/3 of the parent widget).
I'm confused as to why setting the Label.setAlignment(Pos.CENTER_RIGHT) does not affect the alignment of a label that is then added into a GridPane? The only way to do it is seemingly through the grid (e.g. ColumnConstraints) or by e.g. adding the Label to a HBox that has right alignment.
Why does setting the alignment of the label to CENTER_RIGHT have no effect? I can see the API says: "Specifies how the text and graphic within the Labeled should be aligned when there is empty space within the Labeled." But how do I get empty space in a label?
TL:DR version: instead of label.setAlignment(Pos.CENTER_RIGHT); use GridPane.setHalignment(label, HPos.RIGHT);.
JavaFX uses a top-down layout. So the scene sizes the root node to the size of the scene, the root node sizes and positions each of its child nodes depending on its layout strategy and the amount of space the scene gave it, each child node then sizes and positions its child nodes depending on its own layout strategy and the amount of space it has available, and so on down the scene graph.
According to the documentation for Label, the alignmentProperty
Specifies how the text and graphic within the Labeled should be aligned when there is empty space within the Labeled.
Of course, the amount of space available to the label is determined by its parent, which in this case is the grid pane. You can of course find out about the grid pane's layout strategy and how to configure it by reading its documentation. In brief, though:
By default, a grid pane will allocate each node its preferred size, and, if the cell it's placed in has additional space, will align the node in the top left of the grid cell. The preferred size for a label is of course the computed size: just big enough to hold its content. Hence you see that simply placing a label into a grid pane and setting alignment on the label will not have any effect, because the label is sized just large enough to hold its content, and there is no extra space to align the text/graphic. You can visualize this by placing a background color or border on the label.
So you could set the alignment on the label to CENTER_RIGHT, and then instruct the grid pane to let the label grow. This needs two things: first, tell the grid pane to let the label fill the width of the cell:
GridPane.setFillWidth(label, true);
and, since the grid pane will also respect the child node's maximum size, and the label by default uses its preferred size as its maximum size, allow the label to grow:
label.setMaxWidth(Double.MAX_VALUE);
Then the label will grow to the size of the cell, and so if you also have
label.setAlignment(Pos.CENTER_RIGHT);
it will align its own content to the right.
A more sensible approach is probably just to tell the grid pane how to align the label in the cell:
GridPane.setHalignment(label, HPos.RIGHT);
Then let the label take on its default size and alignment and everything will work.
You can also use a ColumnConstraints object to set the default alignment for all labels in a particular column, which is by far the more convenient approach if you are building a form.
I'm developing an app with a complex hierarchy of widgets and layouts, but in short it has a central widget with a formulary as upper widget and a QScrollArea as buttom widget (by means of a QVBoxLayout).
That QScrollArea represents a list (grid layout indeed) of QPushButtons which can contain a huge number of buttons (or not).
I want my app fits the following constraints:
Both (form and list) consume all available horizontal space, redistributing its contents to fill all horizontal space (nor SpaceItems neither contents margins).
Both must save as vertical space as possible, in order to make "lines" close to each other.
I've solve partially my problem making use of setSizeConstraint(QLayout::SetFixedSize) on the form, which shrinks it vertically, but also horizontally, causing that both, list and form, have different widths, wich doesn't look like very well.
How can I achieve that? I mean, how can specify something like grow horizontally to fill the widget but shrink vertically has much as possible?
Add a spacer as the last item to the layout:
gridLayout->addItem(new QSpacerItem(10, 10, QSizePolicy::Expanding, QSizePolicy::Expanding), lastrow, 0);
I think this is what you want:
If you know how many columns you will have (and it doesn't change), insertStretch() in the last column (although it might give you the same effect as using a spacer).
int columnCount = gridLayout()->columnCount();
gridLayout->insertStretch( columnCount(), 1 ); // Default stretch for other
Note that this will resize your buttons to the size Qt thinks they should be unless you are explicitly changing their widths.
I am having some trouble making a new QT widget fill all the available space.
I want to add a widget (a QGLWidget) to the verticle layout (mVisual) and have the layout resize so that mVisual occupies the maximal amount of area.
My layout looks as follows:
The code I have is
ui.setupUi(this);
mWidget = new glStream();
//mWidget->setMinimumHeight(480);
//mWidget->setMinimumWidth(480);
ui.mVisual->addWidget(mWidget);
The problem is that if I don't manually set the width or height I can't even see my widget when I add it!
The only thing I can think of is to perform some sort or arithmetic like parentSize-lytControlsSize, but this feels dirty and like MFC.
How can I layout my widgets so that one layout (mVisual) is minimal, while the other is maximal?
if you put your widget in a Layout, widget fill all available space in their parent area, and if you want to fill without margins, set Layout margins to 0.