Important: Please read the Qt Code of Conduct -

Making a Qwidget, in no layout but with a layout in it, stay at its minimum size

  • I have a window level widget that has no layout. Items are added to it when and where the user click on it.

    The item placed on the main window is derived from a Qwidget and has a QHboxLayout in it. I will call it “the item”. “The item” has three things in its QHboxLayout: A label and two custom widgets that stay small.

    The question is how do I make “the item” stay as small as it can given its content. When I click on the window widget it places “the item” there but it is many times larger than it needs to be to hold the tree items in it.

    I tried to make a custom QHBoxLayout:
    QSize maximumSize() const {
    return minimumSize();

    QSize sizeHint() const {
        return minimumSize();

    I also tried to set the layout to minimum size..


    I tried a few other thing but nothing seems to be working. Any ideas what I should try. Thanks

  • Sounds like you need to add a spacer item to soak up the additional space horizontally and/or vertically.

  • [quote author="ZapB" date="1356128437"]Sounds like you need to add a spacer item to soak up the additional space horizontally and/or vertically.[/quote]

    I do not think that will help since I do not want any additional space in "the item" around its content. I want "the item" to be as small as its contents allows, so the user can place as many of them as possible on the main window. There is no layout on the main window or spacers around items on it.

  • I think I figured out something that will work. In “the item's” constructor I set the layouts spacing and contents margins. Then in its paintEvent I call adjustSize(). It seems to be working so far. Thanks

  • Can you post a screenshot of what you are trying to achieve please?

Log in to reply