Qt Jambi Home

com.trolltech.qt.gui
Class QScrollArea

java.lang.Object
  extended by com.trolltech.qt.QSignalEmitter
      extended by com.trolltech.qt.QtJambiObject
          extended by com.trolltech.qt.core.QObject
              extended by com.trolltech.qt.gui.QWidget
                  extended by com.trolltech.qt.gui.QFrame
                      extended by com.trolltech.qt.gui.QAbstractScrollArea
                          extended by com.trolltech.qt.gui.QScrollArea
All Implemented Interfaces:
QPaintDeviceInterface, QtJambiInterface

public class QScrollArea
extends QAbstractScrollArea

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, Viewer Example

Nested Class Summary
 
Nested classes/interfaces inherited from class com.trolltech.qt.gui.QFrame
QFrame.Shadow, QFrame.Shape, QFrame.StyleMask
 
Nested classes/interfaces inherited from class com.trolltech.qt.gui.QWidget
QWidget.RenderFlag, QWidget.RenderFlags
 
Nested classes/interfaces inherited from class com.trolltech.qt.QSignalEmitter
QSignalEmitter.Signal0, QSignalEmitter.Signal1<A>, QSignalEmitter.Signal2<A,B>, QSignalEmitter.Signal3<A,B,C>, QSignalEmitter.Signal4<A,B,C,D>, QSignalEmitter.Signal5<A,B,C,D,E>, QSignalEmitter.Signal6<A,B,C,D,E,F>, QSignalEmitter.Signal7<A,B,C,D,E,F,G>, QSignalEmitter.Signal8<A,B,C,D,E,F,G,H>, QSignalEmitter.Signal9<A,B,C,D,E,F,G,H,I>
 
Field Summary
 
Fields inherited from class com.trolltech.qt.gui.QWidget
customContextMenuRequested
 
Constructor Summary
QScrollArea()
          Equivalent to QScrollArea(0).
QScrollArea(QWidget parent)
          Constructs an empty scroll area with the given parent.
 
Method Summary
 Qt.Alignment alignment()
          Returns the alignment of the scroll area's widget.
 void ensureVisible(int x, int y)
          Equivalent to ensureVisible(x, y, 50, 50).
 void ensureVisible(int x, int y, int xmargin)
          Equivalent to ensureVisible(x, y, xmargin, 50).
 void ensureVisible(int x, int y, int xmargin, int ymargin)
          Scrolls the contents of the scroll area so that the point (x, y) is visible inside the region of the viewport with margins specified in pixels by xmargin and ymargin.
 void ensureWidgetVisible(QWidget childWidget)
          Equivalent to ensureWidgetVisible(childWidget, 50, 50).
 void ensureWidgetVisible(QWidget childWidget, int xmargin)
          Equivalent to ensureWidgetVisible(childWidget, xmargin, 50).
 void ensureWidgetVisible(QWidget childWidget, int xmargin, int ymargin)
          Scrolls the contents of the scroll area so that the childWidget of QScrollArea::widget() is visible inside the viewport with margins specified in pixels by xmargin and ymargin.
 boolean event(QEvent arg__1)
          

This is the main event handler; it handles event arg__1. You can reimplement this function in a subclass, but we recommend using one of the specialized event handlers instead.

Key press and release events are treated differently from other events. event checks for Tab and Shift+Tab and tries to move the focus appropriately. If there is no widget to move the focus to (or the key press is not Tab or Shift+Tab), event calls keyPressEvent.

Mouse and tablet event handling is also slightly special: only when the widget is enabled, event will call the specialized handlers such as mousePressEvent; otherwise it will discard the event.

This function returns true if the event was recognized, otherwise it returns false. If the recognized event was accepted (see QEvent::accepted), any further processing such as event propagation to the parent widget stops.

 boolean eventFilter(QObject arg__1, QEvent arg__2)
          

Filters events if this object has been installed as an event filter for the arg__1 object.

protected  boolean focusNextPrevChild(boolean next)
          

Finds a new widget to give the keyboard focus to, as appropriate for Tab and Shift+Tab, and returns true if it can find a new widget, or false if it can't.

static QScrollArea fromNativePointer(QNativePointer nativePointer)
          This function returns the QScrollArea instance pointed to by nativePointer
protected  void resizeEvent(QResizeEvent arg__1)
          

This event handler can be reimplemented in a subclass to receive resize events (passed in arg__1), for the viewport widget.

protected  void scrollContentsBy(int dx, int dy)
          

This virtual handler is called when the scroll bars are moved by dx, dy, and consequently the viewport's contents should be scrolled accordingly.

 void setAlignment(Qt.Alignment arg__1)
          Sets the alignment of the scroll area's widget to arg__1.
 void setAlignment(Qt.AlignmentFlag... arg__1)
          Sets the alignment of the scroll area's widget to arg__1.
 void setWidget(QWidget widget)
          Sets the scroll area's widget.
 void setWidgetResizable(boolean resizable)
          Sets whether the scroll area should resize the view widget to resizable.
 QSize sizeHint()
          

Returns the recommended size for the widget.

If the value of this property is an invalid size, no size is recommended.

The default implementation of sizeHint returns an invalid size if there is no layout for this widget, and returns the layout's preferred size otherwise.

 QWidget takeWidget()
          Removes the scroll area's widget, and passes ownership of the widget to the caller.
 QWidget widget()
          Returns the scroll area's widget, or 0 if there is none.
 boolean widgetResizable()
          Returns whether the scroll area should resize the view widget.
 
Methods inherited from class com.trolltech.qt.gui.QAbstractScrollArea
addScrollBarWidget, addScrollBarWidget, contextMenuEvent, cornerWidget, dragEnterEvent, dragLeaveEvent, dragMoveEvent, dropEvent, horizontalScrollBar, horizontalScrollBarPolicy, keyPressEvent, maximumViewportSize, minimumSizeHint, mouseDoubleClickEvent, mouseMoveEvent, mousePressEvent, mouseReleaseEvent, paintEngine, paintEvent, scrollBarWidgets, scrollBarWidgets, setCornerWidget, setHorizontalScrollBar, setHorizontalScrollBarPolicy, setupViewport, setVerticalScrollBar, setVerticalScrollBarPolicy, setViewport, setViewportMargins, verticalScrollBar, verticalScrollBarPolicy, viewport, viewportEvent, wheelEvent
 
Methods inherited from class com.trolltech.qt.gui.QFrame
changeEvent, drawFrame, frameRect, frameShadow, frameShape, frameStyle, frameWidth, lineWidth, midLineWidth, setFrameRect, setFrameShadow, setFrameShape, setFrameStyle, setLineWidth, setMidLineWidth
 
Methods inherited from class com.trolltech.qt.gui.QWidget
acceptDrops, accessibleDescription, accessibleName, actionEvent, actions, activateWindow, addAction, addActions, adjustSize, autoFillBackground, backgroundRole, baseSize, childAt, childAt, childrenRect, childrenRegion, clearFocus, clearMask, close, closeEvent, contentsRect, contextMenuPolicy, createWinId, cursor, depth, destroy, destroy, destroy, devType, ensurePolished, enterEvent, focusInEvent, focusNextChild, focusOutEvent, focusPolicy, focusPreviousChild, focusProxy, focusWidget, font, fontInfo, fontMetrics, foregroundRole, frameGeometry, frameSize, geometry, getContentsMargins, grabKeyboard, grabMouse, grabMouse, grabShortcut, grabShortcut, hasFocus, hasMouseTracking, height, heightForWidth, heightMM, hide, hideEvent, inputContext, inputMethodEvent, inputMethodQuery, insertAction, insertActions, isActiveWindow, isAncestorOf, isEnabled, isEnabledTo, isFullScreen, isHidden, isLeftToRight, isMaximized, isMinimized, isModal, isRightToLeft, isVisible, isVisibleTo, isWindow, isWindowModified, keyboardGrabber, keyReleaseEvent, layout, layoutDirection, leaveEvent, locale, logicalDpiX, logicalDpiY, lower, mapFrom, mapFromGlobal, mapFromParent, mapTo, mapToGlobal, mapToParent, mask, maximumHeight, maximumSize, maximumWidth, metric, minimumHeight, minimumSize, minimumWidth, mouseGrabber, move, move, moveEvent, nextInFocusChain, normalGeometry, numColors, overrideWindowFlags, overrideWindowFlags, overrideWindowState, overrideWindowState, paintingActive, palette, parentWidget, physicalDpiX, physicalDpiY, pos, raise, rect, releaseKeyboard, releaseMouse, releaseShortcut, removeAction, render, render, render, render, render, repaint, repaint, repaint, repaint, resetInputContext, resize, resize, restoreGeometry, saveGeometry, scroll, scroll, setAcceptDrops, setAccessibleDescription, setAccessibleName, setAttribute, setAttribute, setAutoFillBackground, setBackgroundRole, setBaseSize, setBaseSize, setContentsMargins, setContentsMargins, setContextMenuPolicy, setCursor, setDisabled, setEnabled, setFixedHeight, setFixedSize, setFixedSize, setFixedWidth, setFocus, setFocus, setFocusPolicy, setFocusProxy, setFont, setForegroundRole, setGeometry, setGeometry, setHidden, setInputContext, setLayout, setLayoutDirection, setLocale, setMask, setMask, setMaximumHeight, setMaximumSize, setMaximumSize, setMaximumWidth, setMinimumHeight, setMinimumSize, setMinimumSize, setMinimumWidth, setMouseTracking, setPalette, setParent, setParent, setParent, setShortcutAutoRepeat, setShortcutAutoRepeat, setShortcutEnabled, setShortcutEnabled, setSizeIncrement, setSizeIncrement, setSizePolicy, setSizePolicy, setStatusTip, setStyle, setStyleSheet, setTabOrder, setToolTip, setUpdatesEnabled, setVisible, setWhatsThis, setWindowFlags, setWindowFlags, setWindowIcon, setWindowIconText, setWindowModality, setWindowModified, setWindowOpacity, setWindowRole, setWindowState, setWindowState, setWindowTitle, show, showEvent, showFullScreen, showMaximized, showMinimized, showNormal, size, sizeIncrement, sizePolicy, stackUnder, statusTip, style, styleSheet, tabletEvent, testAttribute, toolTip, underMouse, unsetCursor, unsetLayoutDirection, unsetLocale, update, update, update, update, updateGeometry, updateMicroFocus, updatesEnabled, visibleRegion, whatsThis, width, widthMM, window, windowFlags, windowIcon, windowIconText, windowModality, windowOpacity, windowRole, windowState, windowTitle, windowType, winId, x, y
 
Methods inherited from class com.trolltech.qt.core.QObject
blockSignals, childEvent, children, connectSlotsByName, customEvent, disposeLater, dumpObjectInfo, dumpObjectTree, dynamicPropertyNames, findChild, findChild, findChild, findChildren, findChildren, findChildren, findChildren, installEventFilter, isWidgetType, killTimer, moveToThread, objectName, parent, property, removeEventFilter, setObjectName, setParent, setProperty, signalsBlocked, startTimer, thread, timerEvent
 
Methods inherited from class com.trolltech.qt.QtJambiObject
dispose, disposed, finalize, reassignNativeResources, tr, tr, tr
 
Methods inherited from class com.trolltech.qt.QSignalEmitter
disconnect, disconnect, signalSender
 
Methods inherited from class java.lang.Object
clone, equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface com.trolltech.qt.QtJambiInterface
disableGarbageCollection, nativeId, nativePointer, reenableGarbageCollection, setJavaOwnership
 

Constructor Detail

QScrollArea

public QScrollArea()

Equivalent to QScrollArea(0).


QScrollArea

public QScrollArea(QWidget parent)

Constructs an empty scroll area with the given parent.

See Also:
setWidget
Method Detail

alignment

public final Qt.Alignment alignment()

Returns the alignment of the scroll area's widget.

By default, the widget stays rooted to the top-left corner of the scroll area.

See Also:
setAlignment

ensureVisible

public final void ensureVisible(int x,
                                int y,
                                int xmargin)

Equivalent to ensureVisible(x, y, xmargin, 50).


ensureVisible

public final void ensureVisible(int x,
                                int y)

Equivalent to ensureVisible(x, y, 50, 50).


ensureVisible

public final void ensureVisible(int x,
                                int y,
                                int xmargin,
                                int ymargin)

Scrolls the contents of the scroll area so that the point (x, y) is visible inside the region of the viewport with margins specified in pixels by xmargin and ymargin. If the specified point cannot be reached, the contents are scrolled to the nearest valid position. The default value for both margins is 50 pixels.


ensureWidgetVisible

public final void ensureWidgetVisible(QWidget childWidget,
                                      int xmargin)

Equivalent to ensureWidgetVisible(childWidget, xmargin, 50).


ensureWidgetVisible

public final void ensureWidgetVisible(QWidget childWidget)

Equivalent to ensureWidgetVisible(childWidget, 50, 50).


ensureWidgetVisible

public final void ensureWidgetVisible(QWidget childWidget,
                                      int xmargin,
                                      int ymargin)

Scrolls the contents of the scroll area so that the childWidget of QScrollArea::widget() is visible inside the viewport with margins specified in pixels by xmargin and ymargin. If the specified point cannot be reached, the contents are scrolled to the nearest valid position. The default value for both margins is 50 pixels.


setAlignment

public final void setAlignment(Qt.AlignmentFlag... arg__1)

Sets the alignment of the scroll area's widget to arg__1.

By default, the widget stays rooted to the top-left corner of the scroll area.

See Also:
alignment

setAlignment

public final void setAlignment(Qt.Alignment arg__1)

Sets the alignment of the scroll area's widget to arg__1.

By default, the widget stays rooted to the top-left corner of the scroll area.

See Also:
alignment

setWidget

public final void setWidget(QWidget widget)

Sets the scroll area's widget.

The widget becomes a child of the scroll area, and will be destroyed when the scroll area is deleted or when a new widget is set.

Note that if the scroll area is visible when the widget is added, you must show() it explicitly.

See Also:
widget

setWidgetResizable

public final void setWidgetResizable(boolean resizable)

Sets whether the scroll area should resize the view widget to resizable.

If this property is set to false (the default), the scroll area honors the size of its widget. Regardless of this property, you can programmatically resize the widget using widget->resize, and the scroll area will automatically adjust itself to the new size.

If this property is set to true, the scroll area will automatically resize the widget in order to avoid scroll bars where they can be avoided, or to take advantage of extra space.

See Also:
widgetResizable

takeWidget

public final QWidget takeWidget()

Removes the scroll area's widget, and passes ownership of the widget to the caller.

See Also:
widget

widget

public final QWidget widget()

Returns the scroll area's widget, or 0 if there is none.

See Also:
setWidget

widgetResizable

public final boolean widgetResizable()

Returns whether the scroll area should resize the view widget.

If this property is set to false (the default), the scroll area honors the size of its widget. Regardless of this property, you can programmatically resize the widget using widget->resize, and the scroll area will automatically adjust itself to the new size.

If this property is set to true, the scroll area will automatically resize the widget in order to avoid scroll bars where they can be avoided, or to take advantage of extra space.

See Also:
setWidgetResizable

event

public boolean event(QEvent arg__1)

This is the main event handler; it handles event arg__1. You can reimplement this function in a subclass, but we recommend using one of the specialized event handlers instead.

Key press and release events are treated differently from other events. event checks for Tab and Shift+Tab and tries to move the focus appropriately. If there is no widget to move the focus to (or the key press is not Tab or Shift+Tab), event calls keyPressEvent.

Mouse and tablet event handling is also slightly special: only when the widget is enabled, event will call the specialized handlers such as mousePressEvent; otherwise it will discard the event.

This function returns true if the event was recognized, otherwise it returns false. If the recognized event was accepted (see QEvent::accepted), any further processing such as event propagation to the parent widget stops.

Overrides:
event in class QAbstractScrollArea
See Also:
QEvent::type

eventFilter

public boolean eventFilter(QObject arg__1,
                           QEvent arg__2)

Filters events if this object has been installed as an event filter for the arg__1 object.

In your reimplementation of this function, if you want to filter the arg__2 out, i.e. stop it being handled further, return true; otherwise return false.

Example:

    class MainWindow : public QMainWindow
    {
    public:
        MainWindow();

    protected:
        bool eventFilter(QObject *obj, QEvent *ev);

    private:
        QTextEdit *textEdit;
    };

    MainWindow::MainWindow()
    {
        textEdit = new QTextEdit;
        setCentralWidget(textEdit);

        textEdit->installEventFilter(this);
    }

    bool MainWindow::eventFilter(QObject *obj, QEvent *event)
    {
        if (obj == textEdit) {
            if (event->type() == QEvent::KeyPress) {
                QKeyEvent *keyEvent = static_cast<QKeyEvent*>(event);
                qDebug() << "Ate key press" << keyEvent->key();
                return true;
            } else {
                return false;
            }
        } else {
            // pass the event on to the parent class
            return QMainWindow::eventFilter(obj, event);
        }
    }

Notice in the example above that unhandled events are passed to the base class's eventFilter function, since the base class might have reimplemented eventFilter for its own internal purposes.

Warning: If you delete the receiver object in this function, be sure to return true. Otherwise, Qt will forward the event to the deleted object and the program might crash.

Overrides:
eventFilter in class QObject
See Also:
installEventFilter

focusNextPrevChild

protected boolean focusNextPrevChild(boolean next)

Finds a new widget to give the keyboard focus to, as appropriate for Tab and Shift+Tab, and returns true if it can find a new widget, or false if it can't.

If next is true, this function searches forward, if next is false, it searches backward.

Sometimes, you will want to reimplement this function. For example, a web browser might reimplement it to move its "current active link" forward or backward, and call focusNextPrevChild only when it reaches the last or first link on the "page".

Child widgets call focusNextPrevChild on their parent widgets, but only the window that contains the child widgets decides where to redirect focus. By reimplementing this function for an object, you thus gain control of focus traversal for all child widgets.

Overrides:
focusNextPrevChild in class QWidget
See Also:
focusNextChild, focusPreviousChild

resizeEvent

protected void resizeEvent(QResizeEvent arg__1)

This event handler can be reimplemented in a subclass to receive resize events (passed in arg__1), for the viewport widget.

When resizeEvent is called, the viewport already has its new geometry: Its new size is accessible through the QResizeEvent::size() function, and the old size through QResizeEvent::oldSize().

Overrides:
resizeEvent in class QAbstractScrollArea
See Also:
QWidget::resizeEvent

scrollContentsBy

protected void scrollContentsBy(int dx,
                                int dy)

This virtual handler is called when the scroll bars are moved by dx, dy, and consequently the viewport's contents should be scrolled accordingly.

The default implementation simply calls update on the entire viewport, subclasses can reimplement this handler for optimization purposes, or - like QScrollArea - to move a contents widget. The parameters dx and dy are there for convenience, so that the class knows how much should be scrolled (useful e.g. when doing pixel-shifts). You may just as well ignore these values and scroll directly to the position the scroll bars indicate.

Calling this function in order to scroll programmatically is an error, use the scroll bars instead (e.g. by calling QScrollBar::setValue() directly).

Overrides:
scrollContentsBy in class QAbstractScrollArea

sizeHint

public QSize sizeHint()

Returns the recommended size for the widget.

If the value of this property is an invalid size, no size is recommended.

The default implementation of sizeHint returns an invalid size if there is no layout for this widget, and returns the layout's preferred size otherwise.

Overrides:
sizeHint in class QAbstractScrollArea
See Also:
QSize::isValid, minimumSizeHint, sizePolicy, setMinimumSize, updateGeometry

fromNativePointer

public static QScrollArea fromNativePointer(QNativePointer nativePointer)
This function returns the QScrollArea instance pointed to by nativePointer

Parameters:
nativePointer - the QNativePointer of which object should be returned.

Qt Jambi Home