Resize QDialog at a runtime
Asked Answered
C

1

12

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?

Cadmus answered 11/7, 2014 at 8:21 Comment(12)
Do you hide/show any widgets inside the groupbox when you check/uncheck it?Badr
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.Chloroplast
The adjustSize() should do the job. How do you hide those additional widgets and how do you restore them?Badr
@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.Cadmus
I know that it should...The docs say so, but it don'tCadmus
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.Badr
@Badr I'll try to do it, but I don't think so - GroupBox is shinking after checking offCadmus
Did you try restoring size policy to Preferred? I think it should fit better for this case.Badr
@Badr I have tried to remove widgets from layout and to set size policy to Preferred, but unfortunately it doesn't helpCadmus
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.Badr
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.Predicable
@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!Cadmus
P
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.

Predicable answered 11/7, 2014 at 11:48 Comment(1)
This saves a ton of headache trying to manage ui sizing, Thanks.Chuffy

© 2022 - 2024 — McMap. All rights reserved.