I have a main layout in PyQt6 with a left and right side (QHBoxLayout containing two elements). I want both sides to always be 50% of the main window width. However, when a widget on the left side grows wider, the right side is squished smaller.
How can I keep both sides equal without setting explicit (maximum) pixel widths.
Here is a minimum example:
from PyQt6 import QtWidgets
import sys
class MainWindow(QtWidgets.QMainWindow):
def __init__(self):
super().__init__()
layout = QtWidgets.QHBoxLayout()
widget_left = QtWidgets.QLabel()
widget_left.setText("Left: long text without linebreaks")
widget_left.setLineWidth(1)
widget_left.setFrameStyle(QtWidgets.QFrame.Shape.Box)
layout.addWidget(widget_left)
widget_right = QtWidgets.QLabel()
widget_right.setText("Right")
widget_right.setLineWidth(1)
widget_right.setFrameStyle(QtWidgets.QFrame.Shape.Box)
layout.addWidget(widget_right)
widget = QtWidgets.QWidget()
widget.setLayout(layout)
self.setCentralWidget(widget)
self.setFixedSize(300,100)
app = QtWidgets.QApplication(sys.argv)
window = MainWindow()
window.show()
app.exec()
With the result looking like this:

As you can see, left and right don't have equal width. I also tried QGridLayout with the same result. I tried different SizePolicies, but couldn't get the result I want.


Ignoredhorizontal rule for the growing widget should work as expected. Please edit your post and show us your attempts.QLabel.setWordWrap(True)AND setting the stretch factor for both sides to the same value. Setting horizontal size policy toIgnoredalso works though!Ignoredhorizontal policy may work, it may not always acceptable for all widgets (especially those with advanced behavior such as QLabel) or peculiar layouts (eg, when using nested layouts and expanding widgets).