Home · Overviews · Examples 

QScrollArea Class Reference
[com.trolltech.qt.gui module]

The QScrollArea class provides a scrolling view onto another widget. More...

Inherits QAbstractScrollArea.


Detailed Description

The QScrollArea class provides a scrolling view onto another widget.

A scroll area is used to display the contents of a child widget within a frame. If the widget exceeds the size of the frame, the view can provide scroll bars so that the entire area of the child widget can be viewed. The child widget must be specified with setWidget. For example:

    QLabel *imageLabel = new QLabel;
    QImage image("happyguy.png");
    imageLabel->setPixmap(QPixmap::fromImage(image));

    scrollArea = new QScrollArea;
    scrollArea->setBackgroundRole(QPalette::Dark);
    scrollArea->setWidget(imageLabel);

The code above creates a scroll area (shown in the images below) containing an image label. When scaling the image, the scroll area can provide the necessary scroll bars:

The scroll bars appearance depends on the currently set scroll bar policies. You can control the appearance of the scroll bars using the inherited functionality from QAbstractScrollArea.

For example, you can set the QAbstractScrollArea::horizontalScrollBarPolicy and QAbstractScrollArea::verticalScrollBarPolicy properties. Or if you want the scroll bars to adjust dynamically when the contents of the scroll area changes, you can use the horizontalScrollBar() and verticalScrollBar() functions (which enable you to access the scroll bars) and set the scroll bars' values whenever the scroll area's contents change, using the QScrollBar::setValue() function.

You can retrieve the child widget using the widget function. The view can be made to be resizable with the setWidgetResizable function. The alignment of the widget can be specified with setAlignment.

When using a scroll area to display the contents of a custom widget, it is important to ensure that the size hint of the child widget is set to a suitable value. If a standard QWidget is used for the child widget, it may be necessary to call QWidget::setMinimumSize() to ensure that the contents of the widget are shown correctly within the scroll area.

Two convenience functions ensureVisible and ensureWidgetVisible ensure a certain region of the contents is visible inside the viewport, by scrolling the contents if necessary.

For a complete example using the QScrollArea class, see the Image Viewer example. The example shows how to combine QLabel and QScrollArea to display an image.

See also QAbstractScrollArea, QScrollBar, and Image Viewer Example.


Copyright © 2008 Trolltech Trademarks
Qt Jambi 4.3.5_01