Seungho Lee
Seungho Lee

Reputation: 1155

How do I set layout's fixed height in PyQt5?

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()

Upvotes: 3

Views: 14177

Answers (1)

eyllanesc
eyllanesc

Reputation: 243907

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_())

Upvotes: 6

Related Questions