Scrolling in QScrollArea pyqt
Asked Answered
I

2

5

How can I auto scroll for the scroll area? For example, when there is a new update instead of the view of the scroll area staying the same, I want to go down with the new text. Think of it as in a CMD console, when you type a command it autoscroll with the output.

Iinden answered 9/8, 2012 at 15:37 Comment(0)
R
4

Use QTextEdit.moveCursor to set the location you want to scroll to, and then use QTextEdit.ensureCursorVisible to scroll to it:

textedit.moveCursor(QtGui.QTextCursor.End)
textedit.ensureCursorVisible()
Rectifier answered 9/8, 2012 at 17:4 Comment(2)
I'm confused...was this question for a QScrollArea or a QTextEdit - cause if its for the QSCrollArea, then this won't work. You'd have to do: area.verticalScrollBar().setValue(area.verticalScrollBar().maximum()) If its for a QTextEdit, then the question is misleading...Knisley
@EricHulser. The question clearly mentions text, so I just made the assumption that it was related to a QTextEdit - after all, a QScrollArea isn't much use on its own.Rectifier
K
5

I was just going to respond to the other answer, but I just didn't know the best way to phrase it in the space allotted.

QScrollArea's are very useful widgets to use when designing custom PyQt widgets - I use them often. Things like rollout widgets, card widgets, anything where you could be displaying multiple sub-widgets with the need for scrolling can be a very useful utility. I don't agree with the idea that a QScrollArea isn't much use on its own.

The QTextEdit answer solves the problem the developer was facing - but only because it so happens the question is really about that. If you're trying to scroll a text edit, go with that answer.

However, if you are searching for an answer to the actual question and come across this thread, then the way to scroll down a QScrollArea is by actually modifying the scrollbar's value.

area = QScrollArea(parent)
vbar = area.verticalScrollBar()
vbar.setValue(vbar.maximum())

If you want to scroll to particular areas or anything (like implementing the ensureCursorVisible) then you want to take the location on the area's widget that you want to scroll to, figure out the percentage of the height of it, and apply that value to the vertical scrollbar. (Pseudocode)

Knisley answered 10/8, 2012 at 19:55 Comment(1)
QScrollBar already has the functions ensureVisible and ensureWidgetVisible that will scroll both vertically and horizontally to either a specific point or child widget, so there's no need to reimplement them.Rectifier
R
4

Use QTextEdit.moveCursor to set the location you want to scroll to, and then use QTextEdit.ensureCursorVisible to scroll to it:

textedit.moveCursor(QtGui.QTextCursor.End)
textedit.ensureCursorVisible()
Rectifier answered 9/8, 2012 at 17:4 Comment(2)
I'm confused...was this question for a QScrollArea or a QTextEdit - cause if its for the QSCrollArea, then this won't work. You'd have to do: area.verticalScrollBar().setValue(area.verticalScrollBar().maximum()) If its for a QTextEdit, then the question is misleading...Knisley
@EricHulser. The question clearly mentions text, so I just made the assumption that it was related to a QTextEdit - after all, a QScrollArea isn't much use on its own.Rectifier

© 2022 - 2024 — McMap. All rights reserved.