Understand PyQT QHBoxLayout addStretch() with Examples for Beginners – PyQT Tutorial

By | November 25, 2019

PyQT QHBoxLayout class is used to place pyqt widgets horizontally, its addStretch() function is a good way to fill blank space. In this tutorial, we will introduce how to use addStretch() and display the effect of ui after using it.

Create a basic window frame without addStretch()

In this example, we will add two buttions horizontally.

import sys
from PyQt5.QtWidgets import QApplication, QWidget,QPushButton, QHBoxLayout, QVBoxLayout

class YuTextFrame(QWidget):

    def __init__(self):
        super().__init__()
        self.initUI()

    def initUI(self):
        self.setWindowTitle('YuText')  
        
        #
        btn_search = QPushButton('Find', self)
        btn_select = QPushButton('Select', self)
        #
        hbox = QHBoxLayout()
        hbox.addWidget(btn_select)
        hbox.addWidget(btn_search)
        
        self.setLayout(hbox)
        self.setGeometry(300,300,400,300)
        self.show()


if __name__ == '__main__':

    app = QApplication(sys.argv)
    frame = YuTextFrame()
    sys.exit(app.exec_())

Here we create two buttons with QPushButton class, then add these two buttons into QHBoxLayout using hbox.addWidget(), finally, we use self.setLayout(hbox) to set layout of our window frame is QHBoxLayout.

The effect of window frame is:

a basic pyqt window frame

From the effect, we can find:

1. If we do not use addStretch(), the widgets will average the whole width of QHBoxLayout.

For example, if the width of QHBoxLayout  is w, the widget number is n, the width of each widget is about w/n.

2.All widgets in QHBoxLayout are centered horizontally and vertically.

How about addStretch()?

QHBoxLayout.addStretch(size)

Create an empty, stretchable box, where size can be any integer number, -1, 0 ,1,…..

Two effects of using QHBoxLayout.addStretch()

If you have used QHBoxLayout.addStretch(size), it will affect two features of widgets in QHBoxLayout.

1.The width of all widgets in QHBoxLayout will be original width.  They will not average the whole width of QHBoxLayout.

2.It will add an empty and stretchable box in QHBoxLayout, this box will stretch as long as it until all widgets fill up the whole QHBoxLayout.

We will use some examples to discuss these effects.

Add an empty box before the first button

        hbox = QHBoxLayout()
        hbox.addStretch()
        hbox.addWidget(btn_select)
        hbox.addWidget(btn_search)

Here we use hbox.addStretch() create an empty and stretchable box before select button. The effect is:

an empty and stretchable box in pyqt

From the effect, we find width of these two button is original, the remaining space is filled by empty box.

However, if you use hbox.addStretch(1) or hbox.addStretch(2), the effect is the same.

Add widgets like empty box + 2 buttons + empty box 

        hbox = QHBoxLayout()
        hbox.addStretch()
        hbox.addWidget(btn_select)
        hbox.addWidget(btn_search)
        hbox.addStretch()

The effect likes:

two empty boxes in pyqt

Then we will find the width of these two buttons is also original, and the remaining space of QHBoxLayout is averaged by these two empty boxes.

Leave a Reply

Your email address will not be published. Required fields are marked *