January 26, 2011

fifth fifth
Lab Rat
37 posts

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?

2 replies

January 26, 2011

Gerolf Gerolf
Hobby Entomologist
3251 posts

Hi fifth,

if you read the docs, it is stated:

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 [doc.qt.nokia.com]

so in short:

  1. QScrollArea is the outmost container widget, it contains scrollbars and viewport widget. —> correct
  2. viewport is the area, the visible stuff is rendered to
  3. widget() represents the content, you added. It might be bigger than the QScrollArea Widget.


Nokia Certified Qt Specialist.
Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

January 26, 2011

Franzk Franzk
Lab Rat
833 posts

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.


“Horse sense is the thing a horse has which keeps it from betting on people.”—W.C. Fields


  ‹‹ Phonon::MediaObject does not implements DVD in Windows      Hidden information in a QTextDocument ››

You must log in to post a reply. Not a member yet? Register here!