Important: Please read the Qt Code of Conduct -

What's relationship between viewport(), widget() and QScrollArea

  • I thought they were same, eventually I was wrong.
    Now I'm totally confused with their relationship.

    Here's my understanding,

    1. QScrollArea is the outmost container widget, it contains scrollbars and viewport widget.
    2. I've no idea about what purpose of viewport widget is.
    3. widget() represents real contents, holding other subwidgets, for example.
      Did I get right?

  • Hi fifth,

    if you read the docs, it is stated:

    bq. QAbstractScrollArea is a low-level abstraction of a scrolling area. The area provides a central widget called the viewport, in which the contents of the area is to be scrolled (i.e, the visible parts of the contents are rendered in the viewport).
    Next to the viewport is a vertical scroll bar, and below is a horizontal scroll bar. When all of the area contents fits in the viewport, each scroll bar can be either visible or hidden depending on the scroll bar's Qt::ScrollBarPolicy. When a scroll bar is hidden, the viewport expands in order to cover all available space. When a scroll bar becomes visible again, the viewport shrinks in order to make room for the scroll bar.

    "see QAbstractScrollArea Description":

    so in short:

    QScrollArea is the outmost container widget, it contains scrollbars and viewport widget. --> correct

    viewport is the area, the visible stuff is rendered to

    widget() represents the content, you added. It might be bigger than the QScrollArea Widget.

  • Think of the viewport() as a rectangle through which you are looking at the widget(). The scrollbars determine above which part of the widget() the viewport() is floating. It is even possible to use a custom viewport. This is mostly used in the graphics scene/view framework, where it is used to set the viewport to an QGLWidget. However, you can also make a custom widget that allows you to zoom in or out on a widget using a transformation matrix. There are a lot of different possibilities by using the viewport.

Log in to reply