PDA

View Full Version : Resizing a Qt main window when a contained widget changes its size



sizzzzlerz
30th December 2010, 02:42
In Qt4, I have a main window which sets as its central (and only) widget a custom widget I created. Through user action, the widget can change its size. When this happens, I want the main window that contains the widget to adjust its size so that it conforms to the new size of its child widget. Unfortunately, this isn't happening. The new widget is created but it is forced to adapt to the main window's previous size. I have over-ridden the sizeHint method for the widget so that it returns the desired size but this appears to be ignored. I've also added a call to updateGeometry for both the widget and the main window but this has no affect either.

I'm pretty sure I'm close to getting this to work but I'm not terribly experienced in Qt's nuances. Can someone give me some guidance here?

high_flyer
30th December 2010, 09:41
I'd say all you need is to set a layout in your parent widget, and embbed the custom widget in to that layout - the layout will take care of size adjustments - no code on your side will be necessary.

sizzzzlerz
30th December 2010, 15:23
I did try that but when I call addLayout on the main window, I get an error message about attempting to set a layout on the main window which already has a layout. I also tried adding the layout I created as the central widget but that doesn't work either.

I assume that QMainWindow creates a layout automatically when it created. Is there a way to remove this layout and install my own?

high_flyer
30th December 2010, 15:34
Why are you using a QMainWidow?
QMainWindow by design has an "inner space" which can be larger than the screen, therefore its size is not supposed to be adjusted to the inner content, and if you need that, probably QMainWindow is not what you need.
A QDialog probably would suit you better.