PDA

View Full Version : Adjusting QWidget space in window once new Label is added to exsisting space.



seerofsorrow
14th November 2017, 20:45
So I currently have a window that has an introduction section on top with a center section that has two labels with plain text editor next to them. Below this I have another section of fixed plain text sections and labels.

The center section is currently sitting inside of a QWidget, the problem I'm having right now is that i have a pushbutton that when pushed is supposed to set a new label as well as plain text box next to it.

I can't use any of the layouts as they attempt to expand the plain text bars too far when being adjusted and I don't want users to have to scroll all the way to the right to see all of the text as in a line editor.

So that I can increase the size of the middle portion of my page without destroying the layout of the bottom section how should I go about this?

Put the various QObjects inside of a new QWidget? If so how do I tell the QWidget on the bottom to keep it's size while moving down correctly?

I'm not having any specific coding problems as I'm able to get the plus button to create the objects I need but I just can't get the QWidget to size correctly without everything else disappearing.

Thank you greatly for any help or examples that you can point me to.

The center section is currently sitting inside of a QWidget, the problem I'm having right now is that i have a pushbutton that when pushed is supposed to set a new label as well as plain text box next to it.------> (My apologies) inside the middle section

seerofsorrow
15th November 2017, 01:24
I guess a better to rephrase it is this way:
If I have a QGridLayout that's "on top of" a QWidget I would like to know if there's a way to have these two always have the same lower/upper border with one another so that when I resize the QGridLayout it pushes the QWidget downwards.

I apologize if the top portion was extremely confusing.

d_stranz
15th November 2017, 05:34
I can't use any of the layouts as they attempt to expand the plain text bars too far when being adjusted and I don't want users to have to scroll all the way to the right to see all of the text as in a line editor.

You can 1) restrict the maximum size using the properties of the line edit widget in Qt Designer or 2) use a horizontal spacer in the layout to compress the widget to its preferred size.

You seem to have not realized that you can use layouts within layouts within layouts to get arbitrarily complex relationships between child widgets. Your top-level widget should -always- have a layout that controls the overall size and position of other widgets and layouts within it. If you are manually adding child widget and setting their sizes and positions as your post implies, that's wrong.

If you want widgets arranged in a vertical stack, use QVBoxLayout. If you want them side-by-side, use QHBoxLayout.

My strategy for creating a complex nested layout in Qt Designer is this:

1 - First, start with a very large, empty widget. Set aside the upper left part of this widget as the place that will end up as your final, composed layout. You will use the remaining empty space as a "scratch pad" to create the other layouts that will go in this final layout.

2 - If you want a group of side-by-side widgets to sit on top of other widgets laid out in a form, first create an empty QVBoxLayout on your widget and drag this to the upper left area.

3 - In the "scratch pad" area create a QHBoxLayout. Add the side-by-side child widgets to this horizontal layout. Add a horizontal spacer to the right-most end of this layout to push everything to the left.

4 - In another part of the scratch pad, create a grid layout. Add the other child widgets to the grid, and use spacers if needed to push things into place.

5 - Drag the hbox layout created in 3) into the vertical layout.

6 - Drag the grid layout created in 4) into the vertical layout and drop it below the first layout.

7 - Now you have a vertical layout with two layouts inside it. Continue this procedure until you have the design you want.

8 - When you are done, resize the main widget until it is about the same size as the outermost layout, then right-click and select "layout vertically" or whatever is appropriate and your outermost layout will be set as the layout for the widget itself. Now when you resize the top-level widget, the layouts within it will also resize in proportion.

You can also use QSplitter to divide up a widget horizontally or vertically into subwidgets if you can't get what you want using layouts within a single widget.