Qt layouting: default size constraints vertically, setFixedSize horizontally - qt

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.

Related

Layout management in Qt

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).

Is it possible to give a size by ratio to qt designer ?

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).

Qt layout is larger than it should be

I had a layout all nicely designed in Qt, but as soon as I clicked on the parent window and set it to a grid layout, things got all wonky. I've read every tutorial I can find as well as the Qt designer manual and just cannot figure out why this is happening. I have attached a screenshot to show the problem:
As you can see, the vertical layout on the left insists on being wider than the children it contains. Both the label and the treeview are set to sizePolicy maximum, and the maximum width is set to 260px. The children themselves stay the correct size, but the vertical layout that contains them doesn't.
The vertical layout in the middle is set to expanding, and the one on the far right is setup the same as the one on the left, only that one appears to work. How do I make the first vertical layout conform to it's children's width?
Also, if I may sneak a second question in, I have a QTextEdit inside the tab widget in the lower right, but it will not fill to take up the space of the full tab view. You can't see that in the screenshot, but if I pull the tabview up, the textedit within it doesn't stretch with it. How do I make it conform to the size of the tab? It's already set to sizePolicy expanding, but that doesn't seem to help.
The problem is most likely that you need to experiment with "stretching" the layout. Stretch sets the size of layout cells in relation to each other. The default is 0, which means no stretching occurs.
In your case, I believe you want to set the stretch of the first column (column 0) to 0, and the stretch of the second and third columns to 1. This means that the first column will always be as small as possible, and the second and third columns will try to be equally wide.
You can set the stretch programmatically quite easily; for example, to set the first column to stretch 0:
layout->setColumnStretch(0, 0);
In Qt Designer you can access column and row stretches as any normal properties.

Qt: How do i set the height on a Vertical layout?

I'm having trouble using the layout manager system with Qt. This is going to be a Symbian app, so it should resize to different devices.
This is done by using the Layouts.
On the image below I used the Vertical Layout, but I don't understand how I can decide how much each cell should take in width and height.
I want the blue to be a top label background, but I don't want it to be as high as it is now.
Does anyone know how I can do this? (I'm new to Qt :))
You can set the maximum size for a widget by right clicking it and selecting 'Size Constraints'. Under that menu you can find actions that allow you to set the current displayed size as the maximum / minimum size for vertical / horizontal or both directions.
You can also set the numbers by hand by selecting the widget and by setting the number in the 'Property Editor'. They should be under the QWidget properties.
You cannot set the Height of a vertical layout directly, but you can set the height of the widget in which the vertical layout is.
If you want to split your Widgets so that the top widget takes 33.33% of the space, use the Stretch values. Set the top widget to 1 and the bottom widget to 2.

qt unexpandable layout?

Ok, here is my problem:
I have a vertical layout which contains a QPlainTextEdit and a horizontal layout (containing 2 QPushButtons) below the text edit.
The vertical layout is just a part of GUI, and gets resized depending on screen resolution. Btw. it is a mobile app, so I don't have a lot of space on screen.
Push buttons have some text which is dynamically set, I don't know it from the beginning to code it manually.
My problem occurs when the text in push buttons is big, and my whole vertical layout is expanded to fit the buttons.
How can I make the vertical layout unexpandable? note, that this is different from "fixed" because of different screen resoulutions.
I'd just like the clip the buttons if they do not fit, but keep the layout width untouched.
Anyway to do this?
You'll need to set the maximum width for the buttons, not the layout, which is only widening to fit the wider buttons. Check out the docs on QPushButton and look for QWidget inherited functions called setMaximumSize or setMaximumWidth.
You can always GetWidth() on the button when it is an appropriate size, then setMaximumWidth using that value since you wouldn't ordinarily know this. Pick an appropriate default text size/val and use that to create your "dynamic" default since this is going on screens of varying size.

Resources