@Christian-Ehrlicher Ok. I understand, I will add my check boxes in a different function and not during showEvent().
I tried setting layoutSizeConstraint to different values for the vertical layout, the QScrollArea as well as for the widget itself, because of QScrollArea my widget is always resized to a small size when shown for the first time, this is why I tried to add and resize the widget during showEvent() myself. According to Qt's documentation:
If a scroll area is used to display the contents of a widget that contains child widgets arranged in a layout, it is important to realize that the size policy of the layout will also determine the size of the widget. This is especially useful to know if you intend to dynamically change the contents of the layout. In such cases, setting the layout's size constraint property to one which provides constraints on the minimum and/or maximum size of the layout (e.g., QLayout::SetMinAndMaxSize) will cause the size of the scroll area to be updated whenever the contents of the layout changes.
But this is not working for me, I am setting the size constraint to QLayout::SetMinAndMaxSize but the QScrollArea remains at the minimum size when shown for the first time.