11

I have a QDialog subclass containing some options of my application. Some of the options are core, the other are advanced, so I decided to put them into separeted checkable QGroupBox. enter image description here
I want my dialog to shrink verticaly when the user checked off advanced options box, but I can't find the way to do it properly - the dialog size stays exactle the same enter image description here
I set dialog's size policy to Expanding, tried to call adjustSize() and tried to call resize() method - nothing helps. I can't resize programmaticaly dialog to be smaller then it's current size (it only can become larger). At the same time, it is possible to resize it manualy. enter image description here
Can anybody help me?

Mikhail Zimka
  • 694
  • 1
  • 9
  • 20
  • Do you hide/show any widgets inside the groupbox when you check/uncheck it? – Googie Jul 11 '14 at 08:24
  • Maybe you need to hide widgets that aren't necessary at the moment and show them on expanding the dialog? You can take a look at "Dynamic Layouts Example" among Qt examples. – vahancho Jul 11 '14 at 08:25
  • The `adjustSize()` should do the job. How do you hide those additional widgets and how do you restore them? – Googie Jul 11 '14 at 08:27
  • @Googie, yes, exactly, I have connected GroupBox *clicked()* signal with my own slot and hide/show widgets inside it according to GroupBox's *isChecked()* method. – Mikhail Zimka Jul 11 '14 at 08:27
  • I know that it should...The docs say so, but it don't – Mikhail Zimka Jul 11 '14 at 08:36
  • Try removing/adding them from/to the group's layout. I'm pretty sure that the layout gives the wrong `sizeHint()`, because it still thinks it has widgets to be contained and displayed. After removing them, call `adjustSize()` on dialog. – Googie Jul 11 '14 at 08:37
  • @Googie I'll try to do it, but I don't think so - GroupBox is shinking after checking off – Mikhail Zimka Jul 11 '14 at 08:40
  • Did you try restoring size policy to `Preferred`? I think it should fit better for this case. – Googie Jul 11 '14 at 08:58
  • @Googie I have tried to remove widgets from layout and to set size policy to `Preferred`, but unfortunately it doesn't help – Mikhail Zimka Jul 11 '14 at 11:10
  • IS there a chance you paste a piece of code (by editing your original question) where you do the hiding/showing widgets? The entire slot for checking/unchecking group would be nice. – Googie Jul 11 '14 at 11:13
  • 1
    Do you need manual resize? If not, you can add _layout()->setSizeConstraint(QLayout::SetFixedSize);_ to the dialog constructor, then the layout takes over the responsibility to automatically resize when widgets are shown or hidden. – Zlatomir Jul 11 '14 at 11:33
  • @Zlatomir, thank you, that is it! Can you please write an answer for my question - I'd like to accept it, maybe it will be usefull for somebody else. Thanks for everyone! – Mikhail Zimka Jul 11 '14 at 11:45

1 Answers1

11

If you don't need manual resize, you can add

layout()->setSizeConstraint(QLayout::SetFixedSize);

to the dialog constructor, then the layout takes over the responsibility to automatically resize when widgets are shown or hidden.

Zlatomir
  • 6,964
  • 3
  • 26
  • 32