Important: Please read the Qt Code of Conduct -

[SOLVED] Custom QWidget has wrong height in QGridLayout

  • Hi everyone,
    I made a widget derived from QWidget that is used inside a QGridLayout. The widget's size hint depends on some string it displays, which is usually a couple of short words. To handle line breaking, the widget also defines "heightForWidth": Its size policy is "QSizePolicy::Preferred": in both directions.

    When I just show the widget on its own as a top-level window, everything seems alright. However, when I insert it into a QGridLayout, the widget is rendered much too high. Outputting the height of the widget's rect() during the paint event reveals that the height is fixed at 480 px, regardless of how high the layout is. The size hint at that point recommends a height of only 17 px, same for heightForWidth.

    Any idea where those 480 px could come from?

  • From what I know, you should implement sizeHint and minimumSizeHint. But to give a more detailed answer, I would need to know more about your code.

  • Ouch. Note to self: always check for calls to setMinimumSize, especially if the size you set comes from another widget that's not part of a layout yet.

Log in to reply