Question

I've created a custom widget, and in its "main" function (CustomWidget::CustomWidget) I've put following code:

int width = 30, height = 20; //example numbers
setMinimumWidth( ((width*8)*zoom) );
setMinimumHeight( ((height*8)*zoom) );
resize( minimumWidth(),minimumHeight() );

(Note: I've changed the Minimum size for it to be scrolled in ScrollArea.)

However, upon compiling the program the size is the one chosen with parent widget's layout. Additionally, the custom widget's size DOES change if I put the code under a button in MainWindow's code.

PS: I use Qt4, not 5.

Was it helpful?

Solution

If you want your widget to be a specific size you should setFixedWidth() and setFixedHeight() instead. These methods set both the minimum and maximum height and width. When you only set the minimum size you are still saying to the parent layout they can be as big as they want. Note that depending on your Operating System / Window manager the window may still be resized smaller or larger then your fixed size.

From my experience if you want a widget to be a fixed size and have the window calculated to the best possible value it is best to First set the fixed size, second add the widget to your layout and 3rd show the parent.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top