How exactly does addStretch work in QBoxLayout?

Question:

I’m doing a PyQt4 tutorial about box layouts. But I dont understand how addStretch works.

  • If i use vbox.addStretch(1) and hbox.addStretch(1), the two buttons appear down-right. Why?
  • if i comment vbox.addStretch(1) and hbox.addStretch(1) out, the two buttons appear in the center of my window, and they’re deformable horizontally, but not vertically. Why?
  • theres no difference if I change the value “1”… so what does the value do?

Below is the code I’m using:

import sys
from PyQt4 import QtGui

class BoxLayout(QtGui.QWidget):
    def __init__(self, parent=None):
        QtGui.QWidget.__init__(self, parent)

        self.setWindowTitle('box layout')

        ok = QtGui.QPushButton("OK")
        cancel = QtGui.QPushButton("Cancel")

        vbox = QtGui.QHBoxLayout()
        vbox.addStretch(1)
        vbox.addWidget(ok)
        vbox.addWidget(cancel)

        hbox = QtGui.QVBoxLayout()
        hbox.addStretch(1)
        hbox.addLayout(vbox)

        self.setLayout(hbox)

        self.resize(100, 100)

app = QtGui.QApplication(sys.argv)
qb = BoxLayout()
qb.show()
sys.exit(app.exec_())
Asked By: Hubschr

||

Answers:

The addStretch method adds a QSpacerItem to the end of a box layout. A QSpacerItem is an adjustable blank space.

  1. Using vbox.addStretch(1) will add a zero-width spacer-item that
    expands vertically from the top of the layout downwards.

    Using hbox.addStretch(1) will add a zero-height spacer-item that
    expands horizontally from the left of the layout rightwards.

  2. Without stretch, the layout will be determined by the
    sizePolicy
    of the widgets. For a QPushButton, this is
    QSizePolicy.Fixed
    for the vertical dimension, and
    QSizePolicy.Minimum
    for the horizontal dimension. If you wanted the buttons to expand in
    both directions, you could do something like this:

        ok.setSizePolicy(QtGui.QSizePolicy.Minimum,
                         QtGui.QSizePolicy.Minimum)
        cancel.setSizePolicy(QtGui.QSizePolicy.Minimum,
                             QtGui.QSizePolicy.Minimum)
    
  3. The argument passed to addStretch changes the stretch factor. If you
    add a second stretch after the ok button:

        vbox = QtGui.QHBoxLayout()
        vbox.addStretch(1)
        vbox.addWidget(ok)
        vbox.addStretch(2)
        vbox.addWidget(cancel)
    

    you will see that the second spacer item grows twice as fast as the
    first. And if you set the first stretch to zero, it won’t grow at
    all.

If you want more information, see the Layout Management article in the Qt docs. It’s also a good idea to use Qt Designer to experiment with stuff like this, as it gives you immediate visual feedback and shows you all the default values of the various properties involved.

Answered By: ekhumoro