Skip to content

[PyQt5] How to use the QScrollArea component to make a scrolling page

Last Updated on 2021-05-19 by Clay

In the process of designing the interface, we may need to set up a "scrollable" area in the interface so that we can maximize the display of the information we want to display in the limited layout.

I learned this dragging method on the following website: https://www.riverbankcomputing.com/static/Docs/PyQt5/index.html?highlight=qicon

If you are interested in learning PyQt5, maybe you can refer to: PyQt5 Tutorial.


How to use QScrollArea

Let's look at the code:

# -*- coding: utf-8 -*-
import sys
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *


class MainWindow(QWidget):
    def __init__(self):
        super().__init__()
        self.setWindowTitle('QScrollArea Test')
        self.setGeometry(400, 400, 400, 800)

        formLayout = QFormLayout()
        groupBox = QGroupBox()

        for n in range(100):
            label1 = QLabel('Slime_%2d' % n)
            label2 = QLabel()
            label2.setPixmap(QPixmap('s1.png'))
            formLayout.addRow(label1, label2)

        groupBox.setLayout(formLayout)

        scroll = QScrollArea()
        scroll.setWidget(groupBox)
        scroll.setWidgetResizable(True)

        layout = QVBoxLayout(self)
        layout.addWidget(scroll)


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



Output:



This is really scrollable.

1 thought on “[PyQt5] How to use the QScrollArea component to make a scrolling page”

Leave a Reply