2

In my project, I have a Group box with a few elements that may need to be hidden, with other elements above and below. If I hide those elements with setHidden(true), the elements are hidden but that leaves a large space in between the other elements.

How would I be able to compact the group box after hiding those elements, so there isn't such a big space?

Sam
  • 23
  • 4

2 Answers2

0

Without code, it's a little bit hard to guess, what is missing.

Have you tried QLayout::invalidate() on the layout that is holding your group box after hiding the box?

Jens
  • 6,173
  • 2
  • 24
  • 43
0

The reason is not in invalidate(). It's because you have an improper layout on you GroupBox content. if you hide() or show() any QWidget QT automatically invalidates everything up to top-most parent which can be affected by visibility change. So in you case QGroupBox is missing layout, so then you hide element there is nothing to change in regards of QGroupBox geometry, that's why you don't see expected stretching.

evilruff
  • 3,947
  • 1
  • 15
  • 27
  • I am not sure I know what you mean by 'improper layout'. I used the 'Layout in Grid' button in QT Designer. And I am sorry, as this is a project for work I am not able to post any code. – Sam Apr 18 '13 at 18:32
  • 1
    Well in case of grid layout it's a bit tricky.. let's say you have grid 2x2 with labels in column 1 and line edits in column 2.. so to collapse a row you must hide both label and line edit, same to hide column you must hide controls in every row which is inside the grid. In the same way group box should be within a layout itself and if for example you want to collapse it vertically should have stretch element under it. So as soon as elements disappear from group box, stretch will expand more and make group box smaller. – evilruff Apr 18 '13 at 20:30
  • You are correct sir. I had it set up like you described, the group box was within a layout with a spacer under it. I believe it was not being collapsed after hiding elements because I also had a spacer to the left of those elements to offset them slightly from the others. That spacer was not being hidden as I don't think there's a setHidden function on spacers. To resolve this, I placed everything that might need to be hidden (including the spacer) inside their own group box and then I can hide that group box if I need to. Thanks for your help! – Sam Apr 18 '13 at 22:43