The QScrollView widget provides a scrolling area with on-demand scrollbars. More...
#include <qscrollview.h>
Inherits QFrame.
Inherited by QCanvasView, QIconView, QListBox, QListView, QTable and QTextView.
Type | Name | READ | WRITE | Options |
---|---|---|---|---|
ResizePolicy | resizePolicy | resizePolicy | setResizePolicy | |
ScrollBarMode | vScrollBarMode | vScrollBarMode | setVScrollBarMode | |
ScrollBarMode | hScrollBarMode | hScrollBarMode | setHScrollBarMode | |
int | visibleWidth | visibleWidth | ||
int | visibleHeight | visibleHeight | ||
int | contentsWidth | contentsWidth | ||
int | contentsHeight | contentsHeight | ||
int | contentsX | contentsX | ||
int | contentsY | contentsY | ||
bool | dragAutoScroll | dragAutoScroll | setDragAutoScroll |
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 reimplement drawContents(), and use resizeContents() to set the size of the viewed area. Use addChild() / moveChild() to position widgets on the view.
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.
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.
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 reimplement 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.
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 reimplement 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 viewportToContents() for this.
To handle mouse events on the scrolling area, subclass scrollview as you would subclass other widgets, but rather than overriding mousePressEvent(), reimplement viewportMousePressEvent() instead (if you reimplement 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.
When you construct a QScrollView, some of the widget flags apply to the
viewport(), instead of being sent to the QWidget constructor for the
QScrollView. This applies to WResizeNoErase, WNorthWestGravity, WRepaintNoErase
and WPaintClever.
See Qt::WidgetFlags for
documentation about these flags. Here are some examples:
WResizeNoErase|WNorthWestGravity,
because the widget draws all pixels
itself and when the size increases, it only needs a paint event for
the new part, since the old part remains unchanged.
WResizeNoErase
and repaint
itself line by line to get a less flickery resizing. If the widget is
in a mode where no text justification can take place, it might use WNorthWestGravity
too, so that it would only get a repaint for the
newly visible parts.
WRepaintNoErase
(in addition to WNorthWestGravity
and WResizeNoErase)
so that the window system
background does not flash in and out during scrolling.
Warning: WResizeNoErase is currently set by default, i.e. you always have to clear the background manually in scrollview subclasses. This will change in a future version of Qt, and we recommend specifying the flag explicitly.
Examples: scrollview/scrollview.cpp
Default
- QScrollView selects one of the other settings
automatically when it has to. At the time of writing, QScrollView
changes to Manual
if you resize the contents with
resizeContents(), and to AutoOne
if a child is added.
Manual
- the view stays the size set by resizeContents().
AutoOne
- if there is only child widget, the view stays
the size of that widget. Otherwise, the behaviour is undefined.
Auto
- QScrollView shows a scrollbar when the content is
too tall to fit and not else. This is the default.
AlwaysOff
- QScrollView never shows a scrollbar.
AlwaysOn
- QScrollView always shows a scrollbar.
(The modes for the horizontal and vertical scroll bars are independent.).
Examples: scrollview/scrollview.cpp
Constructs a QScrollView with a parent, a name and widget flags f.
The widget flags WNorthWestGravity, WRepaintNoErase
and WPaintClever
are propagated to the viewport() widget. The other
widget flags are propagated to the parent constructor as usual.
Destructs the QScrollView. Any children added with addChild() will be destructed.
[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 enableClipper(TRUE) if you add a large number of widgets.
Examples: scrollview/scrollview.cpp
[protected]
Returns the current bottom margin.
See also setMargins().
[slot]
Scrolls the content so that the point (x,y) is in the center of visible area.
[slot]
Scrolls the content so that the point (x,y) is visible, with the given margins (as fractions of visible area).
eg.
This function is obsolete. It is provided to keep old source working, and will probably be removed in a future version of Qt. We strongly advise against using it in new code.
Returns TRUE if child is visible. This is equivalent to child->isVisible().
Returns the X position of the given child widget. Use this rather than QWidget::x() for widgets added to the view.
Returns the Y position of the given child widget. Use this rather than QWidget::y() for widgets added to the view.
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().
[virtual protected]
This event handler is called whenever the QScrollView receives a dragEnterEvent() - the drag position is translated to be a point on the contents.
[virtual protected]
This event handler is called whenever the QScrollView receives a dragLeaveEvent() - the drag position is translated to be a point on the contents.
[virtual protected]
This event handler is called whenever the QScrollView receives a dragMoveEvent() - the drag position is translated to be a point on the contents.
[virtual protected]
This event handler is called whenever the QScrollView receives a dropEvent() - the drop position is translated to be a point on the contents.
Returns the height of the contents area.
[virtual protected]
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 QTable and QListView.
[virtual protected]
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 and QTable.
[virtual protected]
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 and QTable.
[virtual protected]
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 and QTable.
[signal]
This signal is emitted just before the contents is moved to the given position.
See also contentsX() and contentsY().
Returns the point p translated to a point on the viewport() widget.
Translates a point (x, y) in the contents to a point (vx, vy) on the viewport() widget.
[virtual protected]
This event handler is called whenever the QScrollView receives a wheelEvent() - the mouse position is translated to be a point on the contents.
Returns the width of the contents area.
Returns the X coordinate of the contents which is at the left edge of the viewport.
Returns the Y coordinate of the contents which is at the top edge of the viewport.
Returns the widget in the corner between the two scrollbars.
By default, no corner widget is present.
Examples: scrollview/scrollview.cpp
Returns TRUE if autoscrolling in drag move events is enabled, else FALSE.
See also setDragAutoScroll().
[virtual protected]
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... 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.
Reimplemented in QCanvasView and QTable.
[virtual protected]
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.
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
[slot]
Scrolls the content so that the point (x, y) is visible with at least 50-pixel margins (if possible, otherwise centered).
[slot]
Scrolls the content so that the point (x, y) is visible with at least the given pixel margins (if possible, otherwise centered).
[virtual protected]
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.
[virtual protected]
Reimplemented for internal reasons; the API is not affected.
Reimplemented from QWidget.
[virtual protected]
Reimplemented for internal reasons; the API is not affected.
Reimplemented from QFrame.
Returns the currently set mode for the horizontal scrollbar.
See also setHScrollBarMode().
Examples: scrollview/scrollview.cpp
Returns wether QScrollView uses a static background.
See also setStaticBackground().
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.
[protected]
Returns the current left margin.
See also setMargins().
[virtual]
Reimplemented for internal reasons; the API is not affected.
Reimplemented from QWidget.
[virtual]
Repositions child to (x, y). This functions the same as addChild().
[virtual]
Reimplemented for internal reasons; the API is not affected.
Reimplemented from QObject.
Removes a child from the scrolled area. Note that this happens automatically if the child is deleted.
Calls repaint() on rectangle defined by x, y, w, h, translated appropriately. If the rectangle in not visible, nothing is repainted.
See also updateContents().
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
Reimplemented for internal reasons; the API is not affected.
Examples: iconview/main.cpp dirview/main.cpp
Reimplemented for internal reasons; the API is not affected.
[virtual slot]
Set the size of the contents area to w pixels wide and h pixels high, and updates the viewport accordingly.
[virtual protected]
Reimplemented for internal reasons; the API is not affected.
Reimplemented from QWidget.
Returns the currently set ResizePolicy.
See also setResizePolicy() and ResizePolicy.
[protected]
Returns the current right margin.
See also setMargins().
[slot]
Scrolls the content by x to the left and y upwards.
[virtual slot]
Scrolls the content so that the point (x, y) is in the top-left corner.
Reimplemented in QListView.
[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
If b is set to TRUE, the QScrollView automatically scrolls the contents in drag move events if the user moves the cursor close to a border of the view. This of course only works id the viewport accepts drops! Specifying FALSE here disables this autoscroll feature.
[virtual slot]
Reimplemented for internal reasons; the API is not affected.
Reimplemented from QWidget.
[virtual protected]
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().
Sets the mode for the horizontal scrollbar.
Auto
(the default) shows a scrollbar when the content is too wide to fit.
AlwaysOff
never shows a scrollbar.
AlwaysOn
always shows a scrollbar.
See also hScrollBarMode() and setVScrollBarMode().
Examples: scrollview/scrollview.cpp
[virtual protected]
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 - reimplement drawContents() or put widgets in the unused area.
By default all margins are zero.
See also frameChanged().
Sets the resize policy to r.
See also resizePolicy() and ResizePolicy.
Sets the scrollview to have a static background if y is TRUE, or a scrolling background otherwise. By default, the background is scrolling.
Beware that this mode is quite slow, as a full repaint of the visible area has to be triggered on every contents move.
See also hasStaticBackground().
[virtual protected]
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().
Sets the mode for the vertical scrollbar.
See also vScrollBarMode() and setHScrollBarMode().
Examples: scrollview/scrollview.cpp
[virtual]
Reimplemented for internal reasons; the API is not affected.
Examples: iconview/main.cpp
Reimplemented from QWidget.
This function is obsolete. It is provided to keep old source working, and will probably be removed in a future version of Qt. We strongly advise against using it in new code.
Sets the visibility of child. Equivalent to QWidget::show() or QWidget::hide().
[virtual]
Reimplemented for internal reasons; the API is not affected.
Reimplemented from QWidget.
[virtual]
Reimplemented for internal reasons; the API is not affected.
Reimplemented from QWidget.
[virtual]
Reimplemented for internal reasons; the API is not affected.
Reimplemented from QWidget.
[protected]
Returns the current top margin.
See also setMargins().
Calls update() on rectangle defined by x, y, w, h, translated appropriately. If the rectangle in not visible, nothing is repainted.
See also repaintContents().
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
[slot]
Updates scrollbars - all possibilities considered. You should never need to call this in your code.
Returns the currently set mode for the vertical scrollbar.
See also setVScrollBarMode().
Examples: scrollview/scrollview.cpp
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.
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
[virtual protected]
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().
[virtual protected]
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().
[virtual protected]
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().
[virtual protected]
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().
[virtual protected]
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.
[virtual protected]
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 QTextView, QTextBrowser and QListBox.
[virtual protected]
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.
[virtual protected]
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 QListBox, QTextView and QTextBrowser.
[virtual protected]
This is a low-level painting routine that draws the viewport contents. Reimplement this if drawContents() is too high-level. (for example, if you don't want to open a QPainter on the viewport).
Reimplemented in QListBox.
[virtual protected]
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.
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.
Returns the point on the viewport vp translated to a point in the contents.
Translates a point (vx, vy) on the viewport() widget to a point (x, y) in the contents.
[virtual protected]
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().
Returns the vertical amount of the content that is visible.
Returns the horizontal amount of the content that is visible.
[virtual protected]
Reimplemented for internal reasons; the API is not affected.
Reimplemented from QWidget.
Search the documentation, FAQ, qt-interest archive and more (uses
www.trolltech.com):
This file is part of the Qt toolkit, copyright © 1995-2000 Trolltech, all rights reserved.
Copyright © 2000 Trolltech | Trademarks | Qt version 2.2.1
|