Qt Creator Resizing Widgets in Design Tab - c++

I am having some difficulty resizing widgets inside a layout within the Qt Creator Design tab.
I want to have a vertical layout with a Grid Layout on the top part of the vertical layout and some buttons on the bottom of the vertical layout. When I set this up, the Grid Layout section and the Buttons section each take up exactly half of the screen as shown below.
Current Layout Screenshot
The design creator does not allow me to adjust the size of either section for some reason. I want to have the buttons only take up a small portion of the screen vertically (say 10%) and the Grid Layout take up the remainder of the screen (90%). How can I do that using the Qt Creator Design tab?

Just populate the top layout with a place holder widget that you can use as a container, and set its vertical size policy to MinimumExpanding. You should get something like this:
You can set size policies only on widgets, not on layouts. Therefore you can not achieve what you want without populating the top layout. Finally you can give the grid layout to the contents of the container widget.
Adjust the minimum size of your buttons if you want them to be higher.

Related

How to make the sizes of groupboxes and the contents of a layout uniform

I have a few groupboxes inside of a gridlayout and also two Vboxlayouts that will contain widgets.
However I'm having trouble with the sizings of the groupboxes and the layouts as the layouts have a smaller horizontal size, what do I need to do to make them uniform?
Without much detail to go on in the question, my best guess is that for each QGroupBox in Qt Designer you want to set its sizePolicy -> Horizontal Policy to Expanding. This will make each group box take up the maximum amount of available horizontal space within its parent layout/widget. Which in turn should make them all the same overall width.

How to force layout recalculation after one of its children is resized using CSS?

I have a dialog box with two widgets (which are actually vertical layouts themselves), and a spacer in the middle, arranged as a horizontal layout, like so (layout "lines" are red):
I also have a global stylesheet that's loaded and parsed at runtime. The trouble is, this stylesheet changes the height of some widgets in the left layout, but the main layout doesn't seem to take that into consideration, and so when the application is running it looks like this:
My understanding is that the main layout is resized to compensate for the extra space needed to accommodate the widgets in the left layout, but the right layout is then not resized to fit that extra space in its parent, which looks ugly. I'm using the default size constraint in all layouts.
What can I do to force the right layout to resize itself?

layout resizing in QStackedWidget Page

I'm trying to get a widget placed on a page in a QStackedWidget to resize automatically when the parent QStackedWidget resizes. This simple task seems impossible to do with Qt Creator. Adding a layout to the page does not seem to do anything, the layout just stays at whatever size it's designed as, and will not resize. This is (I think?) because the layout is being added into the page widget as a child and not directly attached to the page widget. There seems to be no way to actually add a layout to page.
How do a get a widget to resize in a QStackedWidget with Qt Creator???
As a side comment, I really wish Qt would abandon it's layout system, it is far far harder to use then an anchoring based system.
Qt Creator treats widgets as if they already have a layout on them which you can access from the context menu (right-click) of the widget in the designer or from the widget list at the right. This means that you only have to drop layouts (from the palette) on a widget if you want to divide that top level layout into further sub layouts. The image below shows this context menu.
Notice the widget icons on the right that show a crossed-out red circle. This indicates that no top-level layout has been defined for those widgets. Once a layout type has been chosen, the red circle will disappear and the icon will reflect your layout choice.
In your case, after you drop a QStackedWidget on your main widget, you can right-click the main widget, select "Lay out" and choose "Lay Out Horizontally". This will make the stacked widget fill the entire main widget and resize with it.
Likewise, if you drop any widgets on one of the "page" widgets, you can then right-click the page widget and assign a top level layout for that which will cause those widgets to resize accordingly with the page widget.

Qt layout and splitter difference

I want to ask that in Qt what is the difference between:
layout horizontally
and:
lay out horizontally in splitter
Similarly:
layout vertically
and:
lay out vertically in splitter
When should either be used?
Qt "Splitters" are layouts that give you a movable handle between the embedded frames, so that the user can adjust the relative size of each by dragging that.
You'd use a splitter instead of an ordinary layout if the ability to adjust the relative sizes of each is a reasonable user expectation; for example, the typical scenario would be a tree view to the left of your main window separated from the main viewport on the right, with the splitter handle allowing you do adjust how much you see of each.
An ordinary simple layout, on the other hand, would be used to implement something like a toolbar; adjusting the relative sizes of toolbar buttons and/or having splitter handles in between toolbar buttons isn't "normal", therefore an ordinary layout is perfectly acceptable.

Change resize behavior in Qt layouts

I want my custom widgets to gain extra space when the dialog is resized. This was working when I only had a handful of widgets, but after adding several more columns of these same widgets and putting them in a QGridLayout, the extra space merely goes in as padding between the widgets.
I've had trouble with this in the past and here are some of the things I've found:
First make sure all the widgets you want to expand have sizePolicy set to "Expanding".
Make sure the widgets that make up your custom widgets are in a layout that allows for expanding. You can check this by just adding one of your custom widgets to the window and seeing that it expands as expected.
Make sure any widgets on the form that you do not want to expand have a fixed (minimum=maximum) size in the dimension you want them to stay static.
Sometimes the grid layout causes some weird spacing issues because rows are resized based on the largest widget in the entire row and similarly for columns. For some layouts, it is better to use a vertical layout that contains horizontal layouts or vica versa to create a grid-like effect. Only this way, each sub-layout is spaced independently of the other rows or columns.
Controlling grid expansion programatically
I've found that you can easily control which columns/rows expand and which columns/rows stay fixed in width by using QGridLayout::setColumnStretch() and QGridLayout::setRowStretch(). You'll need to provide weights to the specific columns (0 for no stretch).
For example, if you want column 0 to not take up any room and column 1 to take the rest of the window's room, do this:
QGridLayout* layout ;
// Set up the layout
layout->setColumnStretch( 0, 0 ) ; // Give column 0 no stretch ability
layout->setColumnStretch( 1, 1 ) ; // Give column 1 stretch ability of ratio 1
Controlling grid expansion using Qt Designer
You can do what I described above if you're using Designer. Just look for the widget properties layoutRowStretch and layoutColumnStretch. It'll contain a comma-separated list of integers.
Another option is inside of QT Creator, to specify in the top level layout widget of the section you want fixed size a layoutSizeConstraint of "SetFixedSize". You must also remove all spacers from beneath that widget. In my case, I had a dialog with a TreeWidget, a Table, and some color selection stuff. I wanted the color selection controls to remain the same size horizontally, so they were in a VerticalLayout. I imagine you can do the same with a HorizontalLayout too if you want things to stay the same height. IF you really need spacers inside the layout, you can probably use blank labels with fixed size.