Skip to content Skip to sidebar Skip to footer

Set Scroll Area Initially Moved

I need to create a big widget inside a scroll area and initially set both sliders in the middle of the bar. The scroll bar does not work, the widgets are not well connected I think

Solution 1:

It seems that the problem comes from scrol.setWidgetResizable(True), which seems to resize the content... setting this to False worked for me.

Also, to center the scrollbar, there are a few options, like setting the value of the verticalScrollBar or using ensureVisible(x, y).

A working solution:

from PyQt5 import QtCore, QtWidgets
import sys

classUiVentana(QtWidgets.QMainWindow):
    def__init__(self, parent=None):
        super(UiVentana, self).__init__(parent)
        self.setupUi()

        self.label.setGeometry(QtCore.QRect(10, 0, 282, 331))
        self.label.setText("this is a long text\n" * 100)

        self.scrollArea.verticalScrollBar().setValue(300)

    defsetupUi(self):
        self.resize(800, 600)
        self.centralwidget = QtWidgets.QWidget(self)

        self.scrollArea = QtWidgets.QScrollArea(self.centralwidget)
        self.scrollArea.setGeometry(QtCore.QRect(470, 330, 301, 211))
        self.scrollArea.setVerticalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOn)
        self.scrollArea.setWidgetResizable(False)

        self.scrollAreaWidgetContents = QtWidgets.QWidget()
        self.scrollAreaWidgetContents.setGeometry(QtCore.QRect(0, 0, 282, 10000))

        self.label = QtWidgets.QLabel(self.scrollAreaWidgetContents)

        self.scrollArea.setWidget(self.scrollAreaWidgetContents)

        self.setCentralWidget(self.centralwidget)

if __name__ == "__main__":
    app = QtWidgets.QApplication(sys.argv)
    ui = UiVentana()
    ui.show()
    sys.exit(app.exec_())

Post a Comment for "Set Scroll Area Initially Moved"