3

I am trying to set a fixed height for QHBoxLayout. To elaborate more on that, I need to set a specific height for my Horizontal layout. However, I cannot find the proper way to do so. What should I do to make this happen?

hbox1 = QHBoxLayout()
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
Seungho Lee
  • 1,068
  • 4
  • 16
  • 42

1 Answers1

6

As noted by @ekhumuro in QHBoxLayout you can not set the fixed height, you must do that to the widget where it will be contained as I show below:

import random
from PyQt5 import QtCore, QtGui, QtWidgets


class Widget(QtWidgets.QWidget):
    def __init__(self, parent=None):
        super(Widget, self).__init__(parent)
        self.setFixedHeight(300)

        lay = QtWidgets.QHBoxLayout(self)

        for letter in "ABCDEFG":
            label = QtWidgets.QLabel(letter, alignment=QtCore.Qt.AlignCenter)
            color = QtGui.QColor(*[random.randint(0, 255) for _ in range(3)])
            label.setStyleSheet("background-color: {}".format(color.name()))
            lay.addWidget(label)


if __name__ == '__main__':
    import sys

    app = QtWidgets.QApplication(sys.argv)
    w = Widget()
    w.show()
    sys.exit(app.exec_())
eyllanesc
  • 235,170
  • 19
  • 170
  • 241