Skip to content Skip to sidebar Skip to footer

How To Have Qhboxlayout To Have Fixed Ratio?

Just like in the image below, I have a QHBoxLayout. Inside this 2 QVBoxLayout where I add a series of widgets to both. But I want the split of QHBoxLayout to be perfectly in the mi

Solution 1:

A possible solution is to set the same stretch factor when adding the QVBoxLayout:

from PyQt5 import QtCore, QtGui, QtWidgets


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

        hlay = QtWidgets.QHBoxLayout(self)

        left_vlay = QtWidgets.QVBoxLayout()
        right_vlay = QtWidgets.QVBoxLayout()

        hlay.addLayout(left_vlay, stretch=1)
        hlay.addLayout(right_vlay, stretch=1)

        left_vlay.addWidget(QtWidgets.QTextEdit())
        right_vlay.addWidget(QtWidgets.QLineEdit())

if __name__ == "__main__":
    import sys

    app = QtWidgets.QApplication(sys.argv)
    w = Widget()
    w.show()
    sys.exit(app.exec_())

Post a Comment for "How To Have Qhboxlayout To Have Fixed Ratio?"