How do I bind a ScrollBar to a label?

There is a program with label and scrollbar. How do I link scrollbar to label?

from PyQt5 import QtCore, QtGui, QtWidgets
import sys


class Ui_Form(object):
    def setupUi(self, Form):
        Form.setObjectName("Form")
        Form.resize(980, 829)
        self.scrollArea = QtWidgets.QScrollArea(Form)
        self.scrollArea.setGeometry(QtCore.QRect(10, 10, 911, 811))
        self.scrollArea.setWidgetResizable(True)
        self.scrollArea.setObjectName("scrollArea")
        self.scrollAreaWidgetContents = QtWidgets.QWidget()
        self.scrollAreaWidgetContents.setGeometry(QtCore.QRect(0, 0, 909, 809))
        self.scrollAreaWidgetContents.setObjectName("scrollAreaWidgetContents")
        self.label = QtWidgets.QLabel(self.scrollAreaWidgetContents)
        self.label.setGeometry(QtCore.QRect(30, 130, 841, 581))
        self.label.setObjectName("label")
        self.scrollArea.setWidget(self.scrollAreaWidgetContents)
        self.verticalScrollBar = QtWidgets.QScrollBar(Form)
        self.verticalScrollBar.setGeometry(QtCore.QRect(940, 20, 16, 771))
        self.verticalScrollBar.setMaximum(10)
        self.verticalScrollBar.setOrientation(QtCore.Qt.Vertical)
        self.verticalScrollBar.setObjectName("verticalScrollBar")

        self.retranslateUi(Form)
        QtCore.QMetaObject.connectSlotsByName(Form)

    def retranslateUi(self, Form):
        _translate = QtCore.QCoreApplication.translate
        Form.setWindowTitle(_translate("Form", "Form"))
        self.label.setText(_translate("Form", "TextLabel"))


class Main(QtWidgets.QWidget, Ui_Form): 
    def __init__(self, parent=None):
        super(Main, self).__init__(parent)
        self.setupUi(self)
        x = "1n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n3n".replace("n", "\n")
        self.label.setText(x)


if __name__ == "__main__":
    app = QtWidgets.QApplication(sys.argv) 
    w = Main()
    w.show()
    sys.exit(app.exec_())
Author: S. Nick, 2020-01-07

1 answers

  1. Create a control QLabel

  2. Create an instance of the QScrollArea control and call the setWidget() method to control the scroll area QLabel.

    The meaning of the following line of code

    Self.scroll_area.setHorizontalScrollBarPolicy(Qt.ScrollBarAlwaysOff)

    The idea is to hide the horizontal scrollbar that goes with the scroll area, because I wanted to use a custom scrollbar scrolls:

  3. Create a horizontal scrollbar and call the setMaximum() method to set the maximum value. And its maximum value should be the same as the maximum value of the hidden horizontal scroll barQScrollArea;

  4. Signal and connection function slot:

    In the slot function sync_func() , we synchronize the current value of the horizontal scrollbar QScrollArea with the value QScrollBar.


import sys
from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import QApplication, QWidget, QLabel, QScrollArea, QScrollBar, QVBoxLayout


class Demo(QWidget):
    def __init__(self):
        super(Demo, self).__init__()

        self.label = QLabel(self)                                                   # 1
        x = ' '.join([ str(i) for i in range(100)])
        y = '\n'.join([ str(i) for i in range(100)])
        self.label.setText(f'{x}\n{y}\n{x}')

        self.scroll_area = QScrollArea(self)                                        # 2
        self.scroll_area.setWidget(self.label)
        self.scroll_area.setHorizontalScrollBarPolicy(Qt.ScrollBarAlwaysOff)

        self.scrollbar = QScrollBar(Qt.Horizontal, self)                            # 3
        self.scrollbar.setMaximum(self.scroll_area.horizontalScrollBar().maximum())

        self.scrollbar.valueChanged.connect(self.sync_func)                         # 4

        self.v_layout = QVBoxLayout(self)
        self.v_layout.addWidget(self.scroll_area)
        self.v_layout.addWidget(self.scrollbar)

    def sync_func(self):
        self.scroll_area.horizontalScrollBar().setValue(self.scrollbar.value())


if __name__ == '__main__':
    app = QApplication(sys.argv)
    demo = Demo()
    demo.show()
    sys.exit(app.exec_())

enter a description of the image here

 1
Author: S. Nick, 2020-01-07 19:12:48