PyQt - QScrollArea



The QScrollArea class builds a scrolling view onto another widget. This class is used to display the child widget content within the frame.

In PyQt, we have classes like QScrollArea and QScrollBar that are closely related components and work together to provide scrolling functionality within the application. Both these classes with their methods are useful when we dynamically change the content of the layout.

Methods used in QScrollArea

Building a scroll area require various methods of PyQt −

  • QScrollArea() − This method enables the area of content specified.
  • QScrollBar() − This method enable the users to scroll the content of the window from left to right or top to bottom.
  • setMaximum() − This method is used to set the maximum value of a widgets such as QScrollBar.
  • addWidget() − This add widget to grid layout.
  • setGeometry() − This method is used to set the geometry for PyQt application such as left, top, width, and height.
  • addrow() − This adds the number of widgets in row wise.
  • setWidget() − Set the widget as the content of the scroll area using setWidget().

Advantages of QScrollArea

  • This is esssential for navigating the lengthy content such as webpages, documents, and lists.
  • Using ScrollArea you can get the portion of length as per any given topic.
  • ScrollArea uses the various properties such as label, bars, grids, and general attributes.
  • It controls all the scrollable data.

Example 1

Following example demonstrate the vertical scrollbar in PyQt window application.

import sys
from PyQt6.QtWidgets import QApplication, QWidget, QVBoxLayout, QListWidget, QScrollBar
class Window(QWidget):
   def __init__(self):
      super().__init__()
      self.MyUI()
   def MyUI(self):
      # Create a QListWidget to display the items
      self.list_widget = QListWidget(self)

      # Add some items to the list
      for i in range(20):
         self.list_widget.addItem("Item {}".format(i))
      # Create a scrollbar and connect it to the list
      scrollbar = QScrollBar(self)
      scrollbar.setMaximum(self.list_widget.count())
      scrollbar.sliderMoved.connect(self.list_widget.setCurrentRow)

      # Create a layout for the widget and add the list and scrollbar to it
      vbox = QVBoxLayout(self)
      vbox.addWidget(self.list_widget)
      vbox.addWidget(scrollbar)

      self.setLayout(vbox)
      self.setGeometry(300, 300, 250, 150)
      self.setWindowTitle('PyQt(Scrollbar)')
      self.show()

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

Output

The above code produces the following output−

qscrollbar_ex_one

Example 2

Following example illustrate the multiple rows of push button using for-loop and display the area of scollbar using QScrollArea and it's relevant method.

import sys
from PyQt6 import QtGui
from PyQt6.QtWidgets import QApplication, QWidget, QScrollArea, QVBoxLayout, QGroupBox, QLabel, QPushButton, QFormLayout
class Window(QWidget):
   def __init__(self, val):
      super().__init__()
      self.title = "Scroll Bar"
      self.top = 200
      self.left = 500
      self.width = 400
      self.height = 300
      self.setWindowIcon(QtGui.QIcon("icon.png"))
      self.setWindowTitle(self.title)
      self.setGeometry(self.left, self.top, self.width, self.height)
      formLayout =QFormLayout()
      groupBox = QGroupBox("This Is Group Box")
      labelLis = []
      comboList = []
      for i in  range(val):
         labelLis.append(QLabel("Label"))
         comboList.append(QPushButton("Click Me"))
         formLayout.addRow(labelLis[i], comboList[i])
      groupBox.setLayout(formLayout)
      scroll = QScrollArea()
      scroll.setWidget(groupBox)
      scroll.setWidgetResizable(True)
      scroll.setFixedHeight(400)
      layout = QVBoxLayout(self)
      layout.addWidget(scroll)
      self.show()
App = QApplication(sys.argv)
window = Window(30)
sys.exit(App.exec())

Output

The above code produces the following output−

qscrollbar_ex_two
Advertisements