0

In my Qt (PySide2) application I have a QScrollArea with a widget that uses a QVBoxLayout. The application continuously adds new images to the top of the scroll area. Now let's assume the user scrolls half way down the list of images, so that the vertical scroll bar is somewhere in the middle of the scroll area.

Now a new image is being added to the scroll area. This does not alter the scroll bar. However, since the new image has been added to the top of the scroll area, this means that what the user sees in the scroll area changes, because all images are pushed down to make room for the new image.

But this is not what I want. Instead the scroll area should stick to the same area of the layout, so that the user always sees the same secton of the layout, even when new images are added to the top of scroll area.

I tried the following code, which does jump a bit into the right direction, but not exactly to the point where it was before the new image has been added:

scroll_bar = self.scroll_area.verticalScrollBar()
scroll_bar.setValue(scroll_bar.value() + last_image_label.height())

Any ideas how to achieve this in Qt/PySide2?

Matthias
  • 8,321
  • 8
  • 45
  • 97
  • I once did something similar. Maybe, it provides some inspiration: [SO: Stop QTableView from scrolling as data is added above current position](https://stackoverflow.com/a/42460216/7478597). – Scheff's Cat May 11 '20 at 14:21
  • @Scheff Thanks, but frankly that seems to be quite complex - I was hoping for an easier solution. I am thinking of `QScrollArea.ensureWidgetVisible()` (or `ensureVisible()`), but how can I determine which widget in the scroll area's layout is currently visible? – Matthias May 11 '20 at 14:34

0 Answers0