Qt logo

QScrollView Class Reference


The QScrollView widget provides a scrolling area with on-demand scrollbars. More...

#include <qscrollview.h>

Inherits QFrame.

Inherited by QListBox, QListView and QTextView.

List of all member functions.

Public Members

Public Slots

Signals

Protected Members


Detailed Description

The QScrollView widget provides a scrolling area with on-demand scrollbars.

The QScrollView is a large canvas - potentially larger than the coordinate system normally supported by the underlying window system. This is important, as is is quite easy to go beyond such limitations (eg. many web pages are more than 32000 pixels high). Additionally, the QScrollView can have QWidgets positioned on it that scroll around with the drawn content. These subwidgets can also have positions outside the normal coordinate range (but they are still limited in size).

To provide content for the widget, inherit from QScrollView and override drawContentsOffset(), and use resizeContents() to set the size of the viewed area. Use addChild() / moveChild() to position widgets on the view. For large numbers of such child widgets, consider using packChildWidgets() to improve performance.

To use QScrollView effectively, it is important to understand its widget structure in the three styles of usage: a single large child widget, a large panning area with some widgets, a large panning area with many widgets.

One Big Widget

The first, simplest usage of QScrollView depicated above is appropriate for scrolling areas which are never more than about 4000 pixels in either dimension (this is about the maximum reliable size on X11 servers). In this usage, you just make one large child in the QScrollView. The child should be a child of the viewport() of the scrollview, and be added with addChild():

    QScrollView* sv = new QScrollView(...);
    QVBox* big_box = new QVBox(sv->viewport());
    sv->addChild(big_box);

You may go on to add arbitrary child widgets to the single child in the scrollview, as you would with any widget:

    QLabel* child1 = new QLabel("CHILD", big_box);
    QLabel* child2 = new QLabel("CHILD", big_box);
    QLabel* child3 = new QLabel("CHILD", big_box);
    ...

Here, the QScrollView has 4 children - the viewport(), the verticalScrollBar(), the horizontalScrollBar(), and a small cornerWidget(). The viewport() has 1 child, the big QVBox. The QVBox has the three labels as child widgets. When the view is scrolled, the QVBox is moved, and its children move with it as child widgets normally do.

Very Big View, some Widgets

The second usage of QScrollView depicated above is appropriate when few, if any, widgets are on a very large scrolling area that is potentially larger than 4000 pixels in either dimension. In this usage, you call resizeContents() to set the size of the area, and override drawContents() to paint the contents. You may also add some widgets, by making them children of the viewport() and adding them with addChild() (this is the same as the process for the single large widget in the previous example):

    QScrollView* sv = new QScrollView(...);
    QLabel* child1 = new QLabel("CHILD", sv->viewport());
    sv->addChild(child1);
    QLabel* child2 = new QLabel("CHILD", sv->viewport());
    sv->addChild(child2);
    QLabel* child3 = new QLabel("CHILD", sv->viewport());
    sv->addChild(child3);

Here, the QScrollView has the same 4 children - the viewport(), the verticalScrollBar(), the horizontalScrollBar(), and a small cornerWidget(). The viewport() has the three labels as child widgets. When the view is scrolled, the scrollview moves the child widgets individually.

Very Big View, many Widgets

The final usage of QScrollView depicated above is appropriate when many widgets are on a very large scrolling area that is potentially larger than 4000 pixels in either dimension. In this usage, you call resizeContents() to set the size of the area, and override drawContents() to paint the contents. You then call enableClipper(TRUE) and add widgets, again by making them children of the viewport() and adding them with addChild():

    QScrollView* sv = new QScrollView(...);
    sv->enableClipper(TRUE);
    QLabel* child1 = new QLabel("CHILD", sv->viewport());
    sv->addChild(child1);
    QLabel* child2 = new QLabel("CHILD", sv->viewport());
    sv->addChild(child2);
    QLabel* child3 = new QLabel("CHILD", sv->viewport());
    sv->addChild(child3);

Here, the QScrollView has 4 children - the clipper() (not the viewport() this time), the verticalScrollBar(), the horizontalScrollBar(), and a small cornerWidget(). The clipper() has 1 child - the viewport(). The viewport() has the three labels as child widgets. When the view is scrolled, the viewport() is moved, and its children move with it as child widgets normally do.

Normally you will use the first or third method if you want any child widgets in the view.

Note that the widget you see in the scrolled area is the viewport() widget, not the QScrollView itself. So, to turn mouse tracking on for example, use viewport()->setMouseTracking(TRUE).

To enable drag-and-drop, you would setAcceptDrops(TRUE) on the QScrollView (since drag-and-drop events propagate to the parent), but to work out what logical position in the view, you would need to map the drop co-ordinate from being relative to the QScrollView to being relative to the contents - use the function mapToContents() for this.

To handle mouse events on the scrolling area, subclass scrollview as you would subclass other widgets, but rather than overriding mousePressEvent(), override viewportMousePressEvent() instead (if you override mousePressEvent() you'll only get called when part of the QScrollView is clicked - and the only such part is the "corner" (if you don't set a cornerWidget()) and the frame, everything else being covered up by the viewport, clipper, or scrollbars.

Examples: scrollview/scrollview.cpp


Member Type Documentation

QScrollView::ResizePolicy

This enum type is used to control QScrollView's reaction to resize events. There are three possible settings:

QScrollView::ScrollBarMode

This enum type describes the various modes of QScrollView's scroll bars. The defined modes are:

(The modes for the horizontal and vertical scroll bars are independent.).

Examples: scrollview/scrollview.cpp


Member Function Documentation

QScrollView::QScrollView(QWidget*parent=0, constchar*name=0, WFlagsf=0)

Constructs a QScrollView.

If you intend to add child widgets, you may see improved refresh if you include WPaintClever in the widgets flags, f. WPaintClever as well as WNorthWestGravity is propagated to the viewport() widget.

QScrollView::~QScrollView()

Destructs the QScrollView. Any children added with addChild() will be destructed.

voidQScrollView::addChild(QWidget*child, intx=0, inty=0) [virtual]

Inserts child into the scrolled area positioned at (x, y). The position defaults to (0,0). If the child is already in the view, it is just moved.

You may want to call packChildWidgets() after a large number of such additions is complete.

Examples: scrollview/scrollview.cpp

intQScrollView::bottomMargin()const [protected]

Returns the current bottom margin.

See also: setMargins().

voidQScrollView::center(intx, inty) [slot]

Scrolls the content so that the point (x,y) is in the center of visible area.

voidQScrollView::center(intx, inty, floatxmargin, floatymargin) [slot]

Scrolls the content so that the point (x,y) is visible, with the given margins (as fractions of visible area).

eg.

boolQScrollView::childIsVisible(QWidget*child)

This function is obsolete. It is provided to keep old programs working. We strongly advise against using it in new code.

Returns TRUE if child is visible. This is equivalent to child->isVisible().

intQScrollView::childX(QWidget*child)

Returns the X position of the given child widget. Use this rather than QWidget::x() for widgets added to the view.

intQScrollView::childY(QWidget*child)

Returns the Y position of the given child widget. Use this rather than QWidget::y() for widgets added to the view.

QWidget*QScrollView::clipper()const

Returns the clipper widget. Contents in the scrollview is ultimately clipped to be inside the clipper widget.

You should not need to access this.

See also: visibleWidth() and visibleHeight().

voidQScrollView::contentsDragEnterEvent(QDragEnterEvent*) [virtualprotected]

This event handler is called whenever the QScrollView receives a dragEnterEvent() - the drag position is translated to be a point on the contents.

voidQScrollView::contentsDragLeaveEvent(QDragLeaveEvent*) [virtualprotected]

This event handler is called whenever the QScrollView receives a dragLeaveEvent() - the drag position is translated to be a point on the contents.

voidQScrollView::contentsDragMoveEvent(QDragMoveEvent*) [virtualprotected]

This event handler is called whenever the QScrollView receives a dragMoveEvent() - the drag position is translated to be a point on the contents.

voidQScrollView::contentsDropEvent(QDropEvent*) [virtualprotected]

This event handler is called whenever the QScrollView receives a dropEvent() - the drop position is translated to be a point on the contents.

intQScrollView::contentsHeight()const

Returns the height of the contents area.

voidQScrollView::contentsMouseDoubleClickEvent(QMouseEvent*) [virtualprotected]

This event handler is called whenever the QScrollView receives a mouseDoubleClickEvent() - the click position is translated to be a point on the contents.

Reimplemented in QListView.

voidQScrollView::contentsMouseMoveEvent(QMouseEvent*) [virtualprotected]

This event handler is called whenever the QScrollView receives a mouseMoveEvent() - the mouse position is translated to be a point on the contents.

Reimplemented in QListView.

voidQScrollView::contentsMousePressEvent(QMouseEvent*) [virtualprotected]

This event handler is called whenever the QScrollView receives a mousePressEvent() - the press position is translated to be a point on the contents.

Reimplemented in QListView.

voidQScrollView::contentsMouseReleaseEvent(QMouseEvent*) [virtualprotected]

This event handler is called whenever the QScrollView receives a mouseReleaseEvent() - the release position is translated to be a point on the contents.

Reimplemented in QListView.

voidQScrollView::contentsMoving(intx, inty) [signal]

This signal is emitted just before the contents is moved to the given position.

See also: contentsX() and contentsY().

QPointQScrollView::contentsToViewport(constQPoint&p)

Returns the point p translated to a point on the viewport() widget.

voidQScrollView::contentsToViewport(intx, inty, int&vx, int&vy)

Translates a point (x, y) in the contents to a point (vx, vy) on the viewport() widget.

voidQScrollView::contentsWheelEvent(QWheelEvent*) [virtualprotected]

This event handler is called whenever the QScrollView receives a wheelEvent() - the mouse position is translated to be a point on the contents.

intQScrollView::contentsWidth()const

Returns the width of the contents area.

intQScrollView::contentsX()const

Returns the X coordinate of the contents which is at the left edge of the viewport.

intQScrollView::contentsY()const

Returns the Y coordinate of the contents which is at the top edge of the viewport.

QWidget*QScrollView::cornerWidget()const

Returns the widget in the corner between the two scrollbars.

By default, no corner widget is present.

Examples: scrollview/scrollview.cpp

voidQScrollView::drawContents(QPainter*p, intclipx, intclipy, intclipw, intcliph) [virtualprotected]

Reimplement this method if you are viewing a drawing area rather than a widget.

The function should draw the rectangle (clipx, clipy, clipw, cliph ) of the contents, using painter p. The clip rectangle is in the scroll views's coordinates.

For example:

  {
    // Fill a 40000 by 50000 rectangle at (100000,150000)

    // Calculate the coordinates... (don't use QPoint, QRect, etc!)
    int x1 = 100000, y1 = 150000;
    int x2 = x1+40000-1, y2 = y1+50000-1;

    // Clip the coordinates so X/Windows will not have problems...
    if (x1 < clipx) x1=clipx;
    if (y1 < clipy) y1=clipy;
    if (x2 > clipx+clipw-1) x2=clipx+clipw-1;
    if (y2 > clipy+cliph-1) y2=clipy+cliph-1;

    // Paint using the small coordinates...
    if ( x2 >= x1 && y2 >= y1 )
        p->fillRect(x1, y1, x2-x1+1, y2-y1+1, red);
  }

The clip rectangle and translation of the painter p is already set appropriately.

voidQScrollView::drawContentsOffset(QPainter*p, intoffsetx, intoffsety, intclipx, intclipy, intclipw, intcliph) [virtualprotected]

For backward compatibility only. It is easier to use drawContents(QPainter*,int,int,int,int).

The default implementation translates the painter appropriately and calls drawContents(QPainter*,int,int,int,int).

Reimplemented in QListView and QTextView.

voidQScrollView::enableClipper(booly)

When large numbers of child widgets are in a scrollview, especially if they are close together, the scrolling performance can suffer greatly. If you call enableClipper(TRUE), the scrollview will use an extra widget to group child widgets.

Note that you may only call enableClipper() prior to adding widgets.

For a full discussion, see the overview documentation of this class.

Examples: scrollview/scrollview.cpp

voidQScrollView::ensureVisible(intx, inty) [slot]

Scrolls the content so that the point (x, y) is visible with at least 50-pixel margins (if possible, otherwise centered).

voidQScrollView::ensureVisible(intx, inty, intxmargin, intymargin) [slot]

Scrolls the content so that the point (x, y) is visible with at least the given pixel margins (if possible, otherwise centered).

boolQScrollView::eventFilter(QObject*obj, QEvent*e) [virtualprotected]

This event filter ensures the scrollbars are updated when a single contents widget is resized, shown, hidden, or destroyed, and passes mouse events to the QScrollView.

Reimplemented from QObject.

boolQScrollView::focusNextPrevChild(boolnext) [virtualprotected]

Override so that traversal moves among child widgets, even if they are not visible, scrolling to make them so.

Reimplemented from QWidget.

voidQScrollView::frameChanged() [virtualprotected]

An override - ensures scrollbars are correct size when frame style changes.

Reimplemented from QFrame.

QScrollView::ScrollBarModeQScrollView::hScrollBarMode()const

Returns the currently set mode for the horizontal scrollbar.

See also: setHScrollBarMode().

Examples: scrollview/scrollview.cpp

QScrollBar*QScrollView::horizontalScrollBar()const

Returns the component horizontal scrollbar. It is made available to allow accelerators, autoscrolling, etc., and to allow changing of arrow scroll rates: bar->setSteps( rate, bar->pageStep() ).

It should not be otherwise manipulated.

This function never returns 0.

intQScrollView::leftMargin()const [protected]

Returns the current left margin.

See also: setMargins().

QSizeQScrollView::minimumSizeHint()const [virtual]

Reimplemented for internal reasons; the API is not affected.

Reimplemented from QWidget.

voidQScrollView::moveChild(QWidget*child, intx, inty) [virtual]

Repositions child to (x, y). This functions the same as addChild().

voidQScrollView::removeChild(QObject*child) [virtual]

Just an override to keep QObject::removeChild() accessible.

Reimplemented from QObject.

voidQScrollView::removeChild(QWidget*child)

Removes a child from the scrolled area. Note that this happens automatically if the child is deleted.

voidQScrollView::repaintContents(intx, inty, intw, inth, boolerase=TRUE)

Calls repaint() on rectangle defined by x, y, w, h, translated appropriately. If the rectangle in not visible, nothing is repainted.

See also: updateContents().

voidQScrollView::resize(constQSize&s)

An override - ensures scrollbars are correct size upon resize.

Examples: qiconview/main.cpp dirview/main.cpp

voidQScrollView::resize(intw, inth)

An override - ensures scrollbars are correct size upon resize.

voidQScrollView::resizeContents(intw, inth) [virtualslot]

Set the size of the contents area to w pixels wide and h pixels high, and updates the viewport accordingly.

voidQScrollView::resizeEvent(QResizeEvent*event) [virtualprotected]

An override - ensures scrollbars are correct size upon resize.

Reimplemented from QWidget.

QScrollView::ResizePolicyQScrollView::resizePolicy()const

Returns the currently set ResizePolicy.

See also: setResizePolicy() and ResizePolicy.

intQScrollView::rightMargin()const [protected]

Returns the current right margin.

See also: setMargins().

voidQScrollView::scrollBy(intdx, intdy) [slot]

Scrolls the content by x to the left and y upwards.

voidQScrollView::setContentsPos(intx, inty) [virtualslot]

Scrolls the content so that the point (x, y) is in the top-left corner.

voidQScrollView::setCornerWidget(QWidget*corner) [virtual]

Sets the widget in the corner between the two scrollbars.

You will probably also want to set at least one of the scrollbar modes to AlwaysOn.

Passing 0 shows no widget in the corner.

Any previous corner widget is hidden.

You may call setCornerWidget() with the same widget at different times.

All widgets set here will be deleted by the QScrollView when it destructs unless you separately reparent the widget after setting some other corner widget (or 0).

Any newly set widget should have no current parent.

By default, no corner widget is present.

See also: setVScrollBarMode() and setHScrollBarMode().

Examples: scrollview/scrollview.cpp

voidQScrollView::setEnabled(boolenable) [virtualslot]

Reimplemented for internal reasons; the API is not affected.

Reimplemented from QWidget.

voidQScrollView::setHBarGeometry(QScrollBar&hbar, intx, inty, intw, inth) [virtualprotected]

Called when the horizontal scrollbar geometry changes. This is provided as a protected function so that subclasses can do interesting things like providing extra buttons in some of the space normally used by the scrollbars.

The default implementation simply gives all the space to hbar.

See also: setVBarGeometry().

voidQScrollView::setHScrollBarMode(ScrollBarModemode) [virtual]

Sets the mode for the horizontal scrollbar.

See also: hScrollBarMode() and setVScrollBarMode().

Examples: scrollview/scrollview.cpp

voidQScrollView::setMargins(intleft, inttop, intright, intbottom) [virtualprotected]

Sets the margins around the scrolling area. This is useful for applications such as spreadsheets with `locked' rows and columns. The marginal space is inside the frameRect() and is left blank - override drawContents() or put widgets in the unused area.

By default all margins are zero.

See also: frameChanged().

voidQScrollView::setResizePolicy(ResizePolicyr) [virtual]

Sets the resize policy to r.

See also: resizePolicy() and ResizePolicy.

voidQScrollView::setVBarGeometry(QScrollBar&vbar, intx, inty, intw, inth) [virtualprotected]

Called when the vertical scrollbar geometry changes. This is provided as a protected function so that subclasses can do interesting things like providing extra buttons in some of the space normally used by the scrollbars.

The default implementation simply gives all the space to vbar.

See also: setHBarGeometry().

voidQScrollView::setVScrollBarMode(ScrollBarModemode) [virtual]

Sets the mode for the vertical scrollbar.

See also: vScrollBarMode() and setHScrollBarMode().

Examples: scrollview/scrollview.cpp

voidQScrollView::show() [virtual]

An override - ensures scrollbars are correct size upon showing.

Examples: qiconview/main.cpp

Reimplemented from QWidget.

voidQScrollView::showChild(QWidget*child, booly=TRUE)

This function is obsolete. It is provided to keep old programs working. We strongly advise against using it in new code.

Sets the visibility of child. Equivalent to QWidget::show() or QWidget::hide().

QSizePolicyQScrollView::sizePolicy()const [virtual]

Specifies that this widget can use additional space, and that it can survive on less than sizeHint().

Reimplemented from QWidget.

intQScrollView::topMargin()const [protected]

Returns the current top margin.

See also: setMargins().

voidQScrollView::updateContents(intx, inty, intw, inth)

Calls update() on rectangle defined by x, y, w, h, translated appropriately. If the rectangle in not visible, nothing is repainted.

See also: repaintContents().

voidQScrollView::updateScrollBars() [slot]

Updates scrollbars - all possibilities considered. You should never need to call this in your code.

QScrollView::ScrollBarModeQScrollView::vScrollBarMode()const

Returns the currently set mode for the vertical scrollbar.

See also: setVScrollBarMode().

Examples: scrollview/scrollview.cpp

QScrollBar*QScrollView::verticalScrollBar()const

Returns the component vertical scrollbar. It is made available to allow accelerators, autoscrolling, etc., and to allow changing of arrow scroll rates: bar->setSteps( rate, bar->pageStep() ).

It should not be otherwise manipulated.

This function never returns 0.

QWidget*QScrollView::viewport()const

Returns the viewport widget of the scrollview. This is the widget containing the contents widget or which is the drawing area.

Examples: scrollview/scrollview.cpp

voidQScrollView::viewportDragEnterEvent(QDragEnterEvent*e) [virtualprotected]

To provide simple processing of events on the contents, this method receives all drag enter events sent to the viewport.

The default implementation translates the event and calls contentsDragEnterEvent().

See also: QWidget::dragEnterEvent().

voidQScrollView::viewportDragLeaveEvent(QDragLeaveEvent*e) [virtualprotected]

To provide simple processing of events on the contents, this method receives all drag leave events sent to the viewport.

The default implementation calls contentsDragLeaveEvent().

See also: QWidget::dragLeaveEvent().

voidQScrollView::viewportDragMoveEvent(QDragMoveEvent*e) [virtualprotected]

To provide simple processing of events on the contents, this method receives all drag move events sent to the viewport.

The default implementation translates the event and calls contentsDragMoveEvent().

See also: QWidget::dragMoveEvent().

voidQScrollView::viewportDropEvent(QDropEvent*e) [virtualprotected]

To provide simple processing of events on the contents, this method receives all drop events sent to the viewport.

The default implementation translates the event and calls contentsDropEvent().

See also: QWidget::dropEvent().

voidQScrollView::viewportMouseDoubleClickEvent(QMouseEvent*e) [virtualprotected]

To provide simple processing of events on the contents, this method receives all mouse double click events sent to the viewport.

The default implementation translates the event and calls contentsMouseDoubleClickEvent().

See also: QWidget::mouseDoubleClickEvent().

Reimplemented in QListBox.

voidQScrollView::viewportMouseMoveEvent(QMouseEvent*e) [virtualprotected]

To provide simple processing of events on the contents, this method receives all mouse move events sent to the viewport.

The default implementation translates the event and calls contentsMouseMoveEvent().

See also: QWidget::mouseMoveEvent().

Reimplemented in QTextBrowser, QListBox and QTextView.

voidQScrollView::viewportMousePressEvent(QMouseEvent*e) [virtualprotected]

To provide simple processing of events on the contents, this method receives all mouse press events sent to the viewport.

The default implementation translates the event and calls contentsMousePressEvent().

See also: contentsMousePressEvent() and QWidget::mousePressEvent().

Reimplemented in QTextView, QListBox and QTextBrowser.

voidQScrollView::viewportMouseReleaseEvent(QMouseEvent*e) [virtualprotected]

To provide simple processing of events on the contents, this method receives all mouse release events sent to the viewport.

The default implementation translates the event and calls contentsMouseReleaseEvent().

See also: QWidget::mouseReleaseEvent().

Reimplemented in QTextView, QListBox and QTextBrowser.

voidQScrollView::viewportPaintEvent(QPaintEvent*pe) [virtualprotected]

This is a low-level painting routine that draws the viewport contents. Override this if drawContentsOffset() is too high-level. (for example, if you don't want to open a QPainter on the viewport).

Reimplemented in QListBox.

voidQScrollView::viewportResizeEvent(QResizeEvent*) [virtualprotected]

To provide simple processing of events on the contents, this method receives all resize events sent to the viewport.

See also: QWidget::resizeEvent().

Reimplemented in QTextView.

QSizeQScrollView::viewportSize(intx, inty)const

Returns the viewport size for size (x, y).

The viewport size depends on x,y (the size of the contents), the size of this widget, the modes of the horizontal and vertical scroll bars.

This function permits widgets that can trade vertical and horizontal space for each other to control scroll bar appearance better. For example, a word processor or web browser can control the width of the right margin accurately, whether there needs to be a vertical scroll bar or not.

QPointQScrollView::viewportToContents(constQPoint&vp)

Returns the point on the viewport vp translated to a point in the contents.

voidQScrollView::viewportToContents(intvx, intvy, int&x, int&y)

Translates a point (vx, vy) on the viewport() widget to a point (x, y) in the contents.

voidQScrollView::viewportWheelEvent(QWheelEvent*e) [virtualprotected]

To provide simple processing of events on the contents, this method receives all wheel events sent to the viewport.

The default implementation translates the event and calls contentsWheelEvent().

See also: QWidget::wheelEvent().

intQScrollView::visibleHeight()const

Returns the vertical amount of the content that is visible.

intQScrollView::visibleWidth()const

Returns the horizontal amount of the content that is visible.

voidQScrollView::wheelEvent(QWheelEvent*e) [virtualprotected]

An override - pass wheel events to the vertical scrollbar.

Reimplemented from QWidget.


Search the documentation, FAQ, qt-interest archive and more (uses www.troll.no):


This file is part of the Qt toolkit, copyright © 1995-99 Troll Tech, all rights reserved.


Copyright 1999 Troll TechTrademarks
Qt version 2.0.2