1

I am using QHBoxLayout to show 5 widgets horizontally aligned.

If I hide and show one widget, it gets resized. How do I prevent this from happening?

BЈовић
  • 62,405
  • 41
  • 173
  • 273

1 Answers1

2

You can't. If you hide a widget, it's gone from screen and everything else will be resized/reordered to fit the available space as best as possible.

However, you can replace your widget with spacing, cf. QBoxLayout::addSpacing or QBoxLayout::addSpacerItem which you set to the width of the widget you hide.

arne
  • 4,514
  • 1
  • 28
  • 47
  • I remember that I read something like that somewhere, but can't find it now. Do you know where it says that the widgets shouldn't be hidden when in a layout? – BЈовић Mar 14 '12 at 13:11
  • @VJovic: Sorry, I don't have a quote handy. This is just the way I do it. Also, you could probably harness a `QGridLayout`, which has a `setColumnMinimumWidth()` function, but I'm not to sure about that. – arne Mar 14 '12 at 13:21
  • 2
    See my answer where I show how to hide a widget while retaining empty space in the layout. It doesn't require inserting any temporary layout items. http://stackoverflow.com/questions/10794532/how-to-make-a-qt-widget-invisible-without-changing-the-position-of-the-other-qt/10840906#10840906 – Kuba hasn't forgotten Monica Jun 01 '12 at 19:06