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'.
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
You should let layout manage widgets size. As far as i am concerned there is 2 situations:
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With