View Full Version : How to resize QDialog when one of the widgets got (in)visible?

30th July 2012, 11:18

I have an object of QDialog. The dialog contains some widgets. Also there is an additional widget on the dialog (showing more details) that is visible/invisible with respect to the button "Show/hide more details" click. The widget locates in the bottom of the dialog. I want my dialog to resize itself when the widget "More details" have changed its visibility. I mean when I want to see more details my dialog gets longer (and vice-versa) but all widgets that are upper than the widget "More details" do not move at all. Now I use this code:

// This is a slot for the button "Show/hide more details"
void MyDialog::onMoreDetails(void)
bool visible = ui.widgetMoreInfo->isVisible();
QSize dialogSize = this->size();
int dialogHeight = dialogSize.height(),
widgetHeight = ui.widgetMoreInfo->height();


if (visible)
dialogSize.setHeight(dialogHeight - widgetHeight - 6);
dialogSize.setHeight(dialogHeight + widgetHeight + 6);


But it is not a universal method. Sometimes the dialog is resized okay but if one shrinks the dialog and clicks the button "More details" all upper widgets move. Maybe I should use layouts to resize?
P.S.: the number 6 is a kind of margin or padding.

30th July 2012, 11:41
Hy tehman,

i think you are searching for a "resize to content" function. Sometimes ago i found a possible solution about your problem but i never test it myself.
Take a look at the first answer ("It seems that calling....") of this thread (http://stackoverflow.com/questions/3611832/qt-how-to-resize-a-window-to-its-new-content) - i think it could be a possible solution for your problem.


30th July 2012, 11:48
You might get some ideas from the Extension Example

30th July 2012, 11:54
Thank you guys, Michael and Chris! I will try to use your advises.