Joan Venge
Joan Venge

Reputation: 331320

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 middle. Some of the widgets inside each side has expanding option but I don't want to QHBoxLayout to allow any side to cross more than half the size of the entire window.

Is this possible? How can I do this?

enter image description here

Upvotes: 1

Views: 955

Answers (1)

eyllanesc
eyllanesc

Reputation: 244202

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

Upvotes: 2

Related Questions