Important: Please read the Qt Code of Conduct -

How to keep the currently visible content section of a scroll area in Qt?

  • First of all: I know this is not the PySide2 sub forum, however, my question is not Python but Qt-related, so I guess I have more chances here in the general Qt forum... Now on to my question:

    In my Qt 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?

  • Lifetime Qt Champion

    Hi and welcome to devnet,

    Your calculation does not take into account the space between the images due to the layout.

Log in to reply