Important: Please read the Qt Code of Conduct -

[Solved] Restrain Height of QHBoxLayout

  • Hello,

    I've looked into the doc and played around with QHBoxLayout, but i can't seem to be able to restrain its size (especially it height).
    I have a layout with labels, and icons, i want it to be 25px high (not less, not more). I've tried to use 'setGeometry()' passing in a QRect and 'setHeight(25)', and then using setSizeConstraint(3). But no matter what i choose, the layout seems to take on a height of about 40 to 50. The layout's geometry().height() however shows '0' no matter what i do...
    I'm a bit confused now, could you help me?


  • Try this:

    Set its layoutsizeconstraint to fixedsize.

    Also the labels, icons you use in the layout should have size policy fixed.(at least one of them)

  • Thanks. It's exactly what i did:

    @self.label1 = QtGui.QLabel("hello", self)

    self.hbox = QtGui.QHBoxLayout()


    And if i query the geometry:
    label heightis: 24
    hbox sizehint PyQt4.QtCore.QSize(72, 24)
    hbox margins:
    top: 0;
    bottom: 0
    hbox size constraint: 3
    hbox geometry:
    height: 0;
    width: 0;
    x pos: 0;
    y pos: 0;
    top: 0;
    bottom: -1

    Despite this, the height is not 24, but rather app. 40 to 50 px high.

  • it think you forgot to set
    label->setMinimumSize(QSize(50, 24));
    or something like that.

  • Apparently the problem was due to the margins of the QHBoxLayout which i had to set with setContentsMargins(0, 0, 0, 0).


Log in to reply