Solved How to remove item from Layout neatly
-
I have built this input window (see attached picture 1) with a bunch of QlineEdits and Qlabels inside nested layouts inside a QscrollArea in Qt Designer (notice that there are more widgets below the "Second Shaft Segment" label).
When I use hide() to hide the "Second Shaft Segment" label and the widgets below it, attached picture 2 occurs (Notice the gap in the top portion which is also matched at the bottom of the scroll area).
How to get the QscrollArea to shrink back to its original size, maintaning the original vertical spacing between my currently-shown widgets?What I tried: I tried to programmatically set the layout spacings back to the original status right after the hide() command, but that didn't work.
-
Why not use a single QGridLayout ?
-
Hi,
How are you building that widget ?
-
Hello, thank you for your reply.
I am sorry I am not sure I understand your question. Can you rephrase please? -
How are you nesting these widgets ?
From the looks of it you seem to have two layouts on top of each other but not inside a QVBoxLayout which could explain why you get the result you have.
-
@SGaist
I basically have 2 gridLayouts (one for each column) both inside a horizontal layout.
Adding them inside a Qframe seemed to solve the top and bottom gap. However, now my columns look like the attached picture (not of equal length kind of) although they contain the same number of widgets with the same fixed min and max height (attached picture).
Sorry if my problem seemed too vague or unreproducible. -
Why not use a single QGridLayout ?
-
@SGaist
It was because my columns are not equally distanced, but I took your recommendation and built my widgets in one QgridLayout and it worked perfectly! (After morphing it into Qframe to solve the top and bottom gap issue).
(I used empty labels as spacers between my two columns)Two more questions please, my last row is initially hidden, when I show it by clicking a button, my scroll bar stays in previous place, can I have it go to the new bottom automatically instead?
Also, when I re-hide that last row also using a button, my scroll bar jumps to the top instead of going to the new bottom. Can I get it to go to new bottom instead? -
Not knowing how you setup your QScrollArea, I can't answer.
-
@Ahmed-Zeid Try using TabWidget for showing the same repetitive entry of values related to "Second Shaft Segment" .
Two more questions please, my last row is initially hidden, when I show it by clicking a button, my scroll bar stays in previous place, can I have it go to the new bottom automatically instead? Also, when I re-hide that last row also using a button, my scroll bar jumps to the top instead of going to the new bottom. Can I get it to go to new bottom instead?
Scroll Area has alignment property can be set as setAlignment(Qt::Alignment) for aligning Qt::AlignTop or Qt::AlignBottom