Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

resize QMainWindow based on a child widget?

Tags:

c++

qt

The child-parent hierarchy is the following: mainWindow -> centralWidget -> frame -> widget.

Widget is being resized during the application lifetime, however it is always set to a fixed size. I want the QMainWindow to resize based on that - to have a minimum size that is needed to display all the widgets.

To do that I currently have to do this.

widget->setFixedSize(x, y);
frame->setFixedSize(frame->sizeHint());
centralWidget->setFixedSize(centralWidget->sizeHint());
mainWindow->setFixedSize(mainWindow->sizeHint());

It doesn't work properly if I only resize the main window. All parents of widget need to be resized in order for this to work. Is there a more elegant way? Is it possible to make the main window call resize on all of it's children?

NOTE: All widgets except 'widget' have automatic layout management. So I find it strange that they don't resize themselves based on 'widget'.

like image 202
NFRCR Avatar asked Mar 30 '12 13:03

NFRCR


2 Answers

It is the job of the layout to determine the size and position of the widgets. So if you explicitly resize a child widget which is inside a layout, you need to ensure that the new size is passed correctly through the layout system. This can be done by ensuring that the widget’s sizeHint() returns the new size. The value returned by the sizeHint() will be used when the layout calculates the new size. So after the sizeHint() has changed, simply call updateGeometry() [qt.nokia.com] that will notify the layout system that the widget has changed and may need to change geometry.

An alternative way to ensure that the parent widget is resized in response to its child being resized, is to call setFixedSize() on the child.

For more details check this...

http://qt-project.org/faq/answer/how_can_i_trigger_the_parent_widget_to_resize_when_its_child_widget_is_resi

like image 58
shofee Avatar answered Oct 03 '22 22:10

shofee


You should let layout manage widgets size. As far as i am concerned there is 2 situations:

  1. With layout when you are resizing a parent all children are resizing too regarding the size policy you provided to them. If you choose a fixed policy children will not be resized. So you have to resize them by hand... but it's quit weird.

  2. When you are resizing a child like you are doing, parent is not automatically resized. You need to do it by hand. Fortunately there's a "magic function for that: AdjustSize. If you call it from QMainWindow all widget will be resized to their optimize size. It is possible (I can't test it here) that this does not work at runtime if size policy is set to fixed.

Hope that helps

like image 32
Patrice Bernassola Avatar answered Oct 03 '22 21:10

Patrice Bernassola