I have a QScrollArea with a QGridLayout within. This said QGridLayout is filled with buttons on a 100x100 grid. I want the vertical scrollbar to start on the bottom instead of the top. I've searched online how to do it, but nothing worked so far.
import sys
from PyQt5 import QtWidgets
class IndicSelectWindow(QtWidgets.QDialog):
def __init__(self, parent=None):
super(IndicSelectWindow, self).__init__(parent=parent)
self.resize(500, 400)
self.layout = QtWidgets.QHBoxLayout(self)
self.scrollArea = QtWidgets.QScrollArea(self)
self.scrollArea.setWidgetResizable(True)
self.scrollAreaWidgetContents = QtWidgets.QWidget()
self.gridLayout = QtWidgets.QGridLayout(self.scrollAreaWidgetContents)
self.scrollArea.setWidget(self.scrollAreaWidgetContents)
self.layout.addWidget(self.scrollArea)
for i in range(100):
for j in range(100):
self.gridLayout.addWidget(QtWidgets.QPushButton(), i, j)
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
w = IndicSelectWindow()
w.show()
sys.exit(app.exec_())
Edit: Trying to explain better - I need to scroll to the bottom so when I open the window, the scrollbar is at the bottom and not at the top.
What I have tried:
x = self.scrollArea.verticalScrollBar().maximum()
self.scrollArea.verticalScrollBar().setValue(x)
and
x = self.scrollArea.verticalScrollBar().maximum()
self.scrollArea.verticalScrollBar().setSliderPosition(x)
Although this question has already got an answer, but, I had somewhat similar problem, which was unsolved even after trying the @eyllanesc 's solution.
So, I'm sharing what worked for me.
I got the solution from another post, quick-link https://stackoverflow.com/a/47879340, though his solution is for somewhat different topic.
Solution:
# inside __init__ method
self.scrollArea.verticalScrollBar().rangeChanged.connect(
self.scrollToBottom,
)
# new function in same class
def scrollToBottom (self, minVal=None, maxVal=None):
# Additional params 'minVal' and 'maxVal' are declared because
# rangeChanged signal sends them, but we set it to optional
# because we may need to call it separately (if you need).
self.scrollArea.verticalScrollBar().setValue(
self.scrollArea.verticalScrollBar().maximum()
)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With