PyQt - QVBoxLayout



The QVBoxLayout is a class that constructs the layout container vertically. The container is sequentially arranged from top to bottom. The vertical series of container can be created using various PyQt functions- setLayout(), QPushButton(), addWidgets(), addStretch(), setStyleSheet(), etc. Therefore, all these function object create an active layout manager.

qvboxlayout Ex Intro

Syntax of QVBoxLayout

Building a vertical set of container layout uses the following syntax −

QVBoxLayout()

Usage of QVBoxLayout in PyQt Window

  • Vertical Arrangement − QVBoxLayout is a layout manager which responsible to arrange the widget in vertical order.
  • Dynamic Resizing − It is dynamically resize beacuse main window is resized.
  • Easy Widget addition − We can add widgets using addWidget() method.
  • Spacing management − The space of QVBoxLayout can be maintained through the addStretch() method. This method is used when there is a need for space.

Example 1

Here, we create a QVBoxLayout that use the method addWidget() and display the buttons in vertical direction.

from PyQt6.QtWidgets import QApplication, QWidget, QPushButton, QVBoxLayout
app = QApplication([])
window = QWidget()
layout = QVBoxLayout()
layout.addWidget(QPushButton('1'))
layout.addWidget(QPushButton('2'))
layout.addWidget(QPushButton('3'))
window.setLayout(layout)
window.show()
app.exec()

Output

On executing the code, we get the following output −

qvboxlayout Ex Basic

Example 2

Below the example illustrates the color filled vertical buttons using QVBoxLayout. The container can filled using QColor class which provide built-in function Color() to set the name of the color.

import sys
from PyQt6.QtWidgets import QApplication, QMainWindow, QVBoxLayout, QWidget
from PyQt6.QtGui import QColor
class Color(QWidget):
   def __init__(self, color):
      super().__init__()
      self.setAutoFillBackground(True)
      # default palette for current style
      palette = self.palette()
      # Modify the color
      palette.setColor(self.backgroundRole(), QColor(color))
      self.setPalette(palette)

class MainWindow(QMainWindow):
   def __init__(self):
      super(MainWindow, self).__init__()
      # Setting window title
      self.setWindowTitle("My App")

      layout = QVBoxLayout()
      layout.addWidget(Color('orange'))
      layout.addWidget(Color('blue'))
      layout.addWidget(Color('green'))

      widget = QWidget()
      widget.setLayout(layout)
      self.setCentralWidget(widget)

if __name__ == "__main__":
   app = QApplication(sys.argv)
   window = MainWindow()
   window.show()
   sys.exit(app.exec())

Output

On executing the code, we get three widgets container in different colors −

qvboxlayout Ex One

Example 3

We add the method addStretch() that align the buttons in the center. Here, we use use vertical spacer from beginning to widget(top) and then widget to end(bottom).

import sys
from PyQt6.QtWidgets import QApplication, QWidget, QPushButton, QVBoxLayout
class MainWindow(QWidget):
   def __init__(self, *args, **kwargs):
      super().__init__(*args, **kwargs)
      self.setWindowTitle('PyQt QVBoxLayout')
      # create a layout
      layout = QVBoxLayout()
      self.setLayout(layout)
      # add a spacer
      layout.addStretch()
      # create buttons and add them to the layout
      title = ['A', 'B', 'C', 'D', 'E']
      buttons = [QPushButton(t) for t in title]
      for button in buttons:
         layout.addWidget(button)
      # add a spacer
      layout.addStretch()
      # show the window
      self.show()

if __name__ == '__main__':
   app = QApplication(sys.argv)
   window = MainWindow()
   sys.exit(app.exec())

Output

On executing the code, we get the following result −

qvboxlayout Ex Two
Advertisements