Chapter 21 scroll area QScrollArea and scroll bar QScrollBar

Imagine if you used 100 buttons in your program, would you choose to display all 100 buttons on the interface at the same time? Of course not, otherwise the interface will be crowded. We can put these 100 buttons in a scroll area QScrollArea. Users can only see a few buttons at first, but the scroll bar allows them to operate the rest of the buttons. Through QScrollArea, we can make the interface more neat and friendly.

QScrollArea has its own scroll bar, but many times, developers may want to add more functions to the scroll bar to control the interface, or sometimes they just need to use the scroll bar. The QScrollBar class allows us to design the scroll bar we want.

21.1 example

Let's explain the basic usage of QScrollArea and QScrollBar through the following example:

  1. Scroll area QScrollArea

  2. Scroll bar QScrollBar

  3. The Zoom in button and Zoom out button are used to Zoom in and out the picture respectively

The code is as follows:

import sys
from PyQt5.QtCore import Qt
from PyQt5.QtGui import QPixmap
from PyQt5.QtWidgets import QApplication, QWidget, QLabel, QPushButton, QScrollArea, QScrollBar, \
                            QHBoxLayout, QVBoxLayout

class Demo(QWidget):
    def __init__(self):
        super(Demo, self).__init__()
        self.label = QLabel(self)                              # 1

        self.scroll_area = QScrollArea(self)                   # 2

        self.scrollbar = QScrollBar(Qt.Horizontal, self)       # 3

        self.bigger_btn = QPushButton('Zoom in', self)         # 4
        self.smaller_btn = QPushButton('Zoom out', self)

        self.bigger_btn.clicked.connect(self.bigger_func)      # 5

        self.h_layout = QHBoxLayout()                        

        self.v_layout = QVBoxLayout()

    def bigger_func(self):
        self.label.resize(self.label.width()*1.2, self.label.height()*1.2)

    def smaller_func(self):
        self.label.resize(self.label.width() * 0.8, self.label.height() * 0.8)

    def sync_func(self):

if __name__ == '__main__':
    app = QApplication(sys.argv)
    demo = Demo()
  1. Instantiate a QLabel control to display a large picture. The setScaledContents(True) method allows the picture to change with the size of the QLabel control, that is, adaptive;

  2. Instantiate a QScrollArea control and call the setWidget() method to scroll the controls in the QLabel area. The meaning of the following line of code is to hide the horizontal scroll bar in the scroll area, because we want to use the scroll bar sent to ourselves:


Obviously, if you want to hide the vertical scroll bar, use:

  1. Instantiate a horizontal scroll bar and call the setMaximum() method to set the maximum value. Its maximum value should be the same as the maximum value of the horizontal scroll bar hidden in QScrollArea;

  2. Instantiate two buttons to zoom in and out of the QLabel control (the picture will also zoom in and out accordingly);

  3. Signal and slot function connection. In bigger_func() slot function, we enlarge the QLabel control by 20%, and set the maximum value of QScrollBar to the maximum value of QScrollArea horizontal scroll bar; In smaller_ In func() slot function, we will reduce the QLabel control by 20%, and also update the maximum value of QScrollBar; In sync_ In the func() slot function, we synchronize the current value of the QScrollArea horizontal scroll bar with the value of the QScrollBar. In this way, we are using our own instantiated QScrollBar to control the pictures in the scrolling area (I believe some readers will have such a demand).

Picture download address:

The operation screenshot is as follows:

Operate the scroll bar to display the rest of the picture content:

Click the zoom in / out button (the maximum value of the scroll bar will change accordingly):

21.2 summary

  1. Readers can replace QLabel with a QWidget, which contains many child controls, which can save a lot of interface space;

  2. Of course, QScrollBar does not have to be used with QScrollArea. We can also regard it as a QSlider;

  3. The above examples are only a small part of the usage of QScrollArea and QScrollBar. You can learn more about the usage through the documentation (be sure to check the documentation more).

Keywords: Qt PyQt5

Added by thekidscareya on Thu, 06 Jan 2022 03:32:30 +0200