QAbstractScrollArea Class

QAbstractScrollArea 提供一个具有滚动条的滚动区域. 更多...

头文件: #include <QAbstractScrollArea>
qmake: QT += widgets
基类: QFrame
派生类:

QAbstractItemView, QGraphicsView, QMdiArea, QPlainTextEdit, QScrollArea, and QTextEdit

公有类型

enum SizeAdjustPolicy { AdjustIgnored, AdjustToContents, AdjustToContentsOnFirstShow }

属性

公有函数

QAbstractScrollArea(QWidget *parent = Q_NULLPTR)
~QAbstractScrollArea()
void addScrollBarWidget(QWidget *widget, Qt::Alignment alignment)
QWidget *cornerWidget() const
QScrollBar *horizontalScrollBar() const
Qt::ScrollBarPolicy horizontalScrollBarPolicy() const
QSize maximumViewportSize() const
QWidgetList scrollBarWidgets(Qt::Alignment alignment)
void setCornerWidget(QWidget *widget)
void setHorizontalScrollBar(QScrollBar *scrollBar)
void setHorizontalScrollBarPolicy(Qt::ScrollBarPolicy)
void setSizeAdjustPolicy(SizeAdjustPolicy policy)
void setVerticalScrollBar(QScrollBar *scrollBar)
void setVerticalScrollBarPolicy(Qt::ScrollBarPolicy)
void setViewport(QWidget *widget)
virtual void setupViewport(QWidget *viewport)
SizeAdjustPolicy sizeAdjustPolicy() const
QScrollBar *verticalScrollBar() const
Qt::ScrollBarPolicy verticalScrollBarPolicy() const
QWidget *viewport() const

重新实现的公有函数

virtual QSize minimumSizeHint() const
virtual QSize sizeHint() const

受保护的函数

virtual void scrollContentsBy(int dx, int dy)
void setViewportMargins(int left, int top, int right, int bottom)
void setViewportMargins(const QMargins &margins)
virtual bool viewportEvent(QEvent *event)
QMargins viewportMargins() const
virtual QSize viewportSizeHint() const

重新实现的受保护函数

virtual void contextMenuEvent(QContextMenuEvent *e)
virtual void dragEnterEvent(QDragEnterEvent *event)
virtual void dragLeaveEvent(QDragLeaveEvent *event)
virtual void dragMoveEvent(QDragMoveEvent *event)
virtual void dropEvent(QDropEvent *event)
virtual bool event(QEvent *event)
virtual void keyPressEvent(QKeyEvent *e)
virtual void mouseDoubleClickEvent(QMouseEvent *e)
virtual void mouseMoveEvent(QMouseEvent *e)
virtual void mousePressEvent(QMouseEvent *e)
virtual void mouseReleaseEvent(QMouseEvent *e)
virtual void paintEvent(QPaintEvent *event)
virtual void resizeEvent(QResizeEvent *event)
virtual void wheelEvent(QWheelEvent *e)
  • 4 个受保护的函数继承自 QFrame
  • 35 个受保护的函数继承自 QWidget
  • 9 个受保护的函数继承自 QObject
  • 1 个受保护的函数继承自 QPaintDevice

其他继承的成员

  • 19 个公有槽函数继承自 QWidget
  • 1 个公有槽函数继承自 QObject
  • 3 个信号继承自 QWidget
  • 2 个信号继承自 QObject
  • 1 个公有变量继承自 QObject
  • 5 个静态公有成员继承自 QWidget
  • 10 个静态公有成员继承自 QObject
  • 1 个受保护的槽函数继承自 QWidget
  • 2 个受保护的变量继承自 QObject
  • 1 protected type inherited from QPaintDevice

详细描述

QAbstractScrollArea 提供一个具有滚动条的滚动区域.

QAbstractScrollArea 一个滚动区域的低级抽象类. 它提供一个被称为viewport的中心widget, viewport中显示滚动区域内容 (即, viewport显示内容的可见部分).

viewport的右边是垂直滚动条, 下边是水平滚动条. 依据Qt::ScrollBarPolicy的配置, 当viewport可以显示所有内容时, 每个滚动条可以显示, 也可隐藏. 滚动条隐藏时, viewport自动扩展覆盖所有可用空间. 滚动条再次显示时, viewport自动收缩, 为滚动条留出显示空间.

你可以在viewport周围预留边距, 参见 setViewportMargins(). 这个边距是为了在viewport上方或两边放置类似 QHeaderView widget. 子类化QAbstractScrollArea 应该实现边距.

子类化 QAbstractScrollArea时, 你需要做如下内容:

  • 设置滚动条的范围, 值, 页面步长, 追踪滚动条移动.
  • 根据滚动条的值绘制viewport的显示内容.
  • viewportEvent() 函数中处理事件, 尤其是窗口大小改变事件.
  • 利用viewport->update() 更新视口内容, 而不是 update() , 所有的绘制操作都是在viewport上.

使用滚动条策略 Qt::ScrollBarAsNeeded (默认值), QAbstractScrollArea 根据滚动范围自动控制滚动条显隐.

viewport接收到窗口或内容大小改变事件时, 滚动条和viewport都应更新. 滚动条改变时, viewport也应该更新. 滚动条的初始值应该在接收到新内容时设置.

我们给出一个简单示例, 这个示例实现一个可以滚动任何QWidget的滚动区域. 我们将这个widget设置为viewport的子widget; 这样, 我们不需要计算绘制widget的哪个部分, 只需调用 QWidget::move()移动widget即可. 当区域内容或大小改变时, 我们执行如下操作:


      QSize areaSize = viewport()->size();
      QSize  widgetSize = widget->size();

      verticalScrollBar()->setPageStep(areaSize.height());
      horizontalScrollBar()->setPageStep(areaSize.width());
      verticalScrollBar()->setRange(0, widgetSize.height() - areaSize.height());
      horizontalScrollBar()->setRange(0, widgetSize.width() - areaSize.width());
      updateWidgetPosition();

滚动条值改变时, 我们需要更新widget位置, 即., 找到需在viewport中绘制的widget部分:


      int hvalue = horizontalScrollBar()->value();
      int vvalue = verticalScrollBar()->value();
      QPoint topLeft = viewport()->rect().topLeft();

      widget->move(topLeft.x() - hvalue, topLeft.y() - vvalue);

为了跟踪滚动条的移动, 重新实现虚函数 scrollContentsBy(). 为了微调滚动行为, 连接滚动条的 QAbstractSlider::actionTriggered() 信号, 并调整 QAbstractSlider::sliderPosition .

方便起见, QAbstractScrollArea 在虚函数 viewportEvent() 处理所有viewport事件. QWidget的事件被映射成viewport事件. 重新映射的事件有: paintEvent(), mousePressEvent(), mouseReleaseEvent(), mouseDoubleClickEvent(), mouseMoveEvent(), wheelEvent(), dragEnterEvent(), dragMoveEvent(), dragLeaveEvent(), dropEvent(), contextMenuEvent(), resizeEvent().

QScrollArea继承自 QAbstractScrollArea, 为任意 QWidget 提供平滑滚动 (即., 这个widget以像素为单位滚动). 如果QScrollArea无法满足你的需求, 你可以子类化 QAbstractScrollArea . 例如, 某些内容不适合在 QWidget 绘制或你不希望平滑滚动.

参见 QScrollArea.

成员类型

enum QAbstractScrollArea::SizeAdjustPolicy

This enum specifies how the size hint of the QAbstractScrollArea should adjust when the size of the viewport changes.

ConstantValueDescription
QAbstractScrollArea::AdjustIgnored0The scroll area will behave like before - and not do any adjust.
QAbstractScrollArea::AdjustToContents2The scroll area will always adjust to the viewport
QAbstractScrollArea::AdjustToContentsOnFirstShow1The scroll area will adjust to its viewport the first time it is shown.

This enum was introduced or modified in Qt 5.2.

属性

horizontalScrollBarPolicy : Qt::ScrollBarPolicy

This property holds the policy for the horizontal scroll bar

The default policy is Qt::ScrollBarAsNeeded.

访问函数:

Qt::ScrollBarPolicy horizontalScrollBarPolicy() const
void setHorizontalScrollBarPolicy(Qt::ScrollBarPolicy)

参见 verticalScrollBarPolicy.

sizeAdjustPolicy : SizeAdjustPolicy

This property holds the policy describing how the size of the scroll area changes when the size of the viewport changes.

The default policy is QAbstractScrollArea::AdjustIgnored. Changing this property might actually resize the scrollarea.

This property was introduced in Qt 5.2.

访问函数:

SizeAdjustPolicy sizeAdjustPolicy() const
void setSizeAdjustPolicy(SizeAdjustPolicy policy)

verticalScrollBarPolicy : Qt::ScrollBarPolicy

This property holds the policy for the vertical scroll bar

The default policy is Qt::ScrollBarAsNeeded.

访问函数:

Qt::ScrollBarPolicy verticalScrollBarPolicy() const
void setVerticalScrollBarPolicy(Qt::ScrollBarPolicy)

参见 horizontalScrollBarPolicy.

成员函数

QAbstractScrollArea::QAbstractScrollArea(QWidget *parent = Q_NULLPTR)

Constructs a viewport.

The parent argument is sent to the QWidget constructor.

QAbstractScrollArea::~QAbstractScrollArea()

Destroys the viewport.

void QAbstractScrollArea::addScrollBarWidget(QWidget *widget, Qt::Alignment alignment)

Adds widget as a scroll bar widget in the location specified by alignment.

Scroll bar widgets are shown next to the horizontal or vertical scroll bar, and can be placed on either side of it. If you want the scroll bar widgets to be always visible, set the scrollBarPolicy for the corresponding scroll bar to AlwaysOn.

alignment must be one of Qt::Alignleft and Qt::AlignRight, which maps to the horizontal scroll bar, or Qt::AlignTop and Qt::AlignBottom, which maps to the vertical scroll bar.

A scroll bar widget can be removed by either re-parenting the widget or deleting it. It's also possible to hide a widget with QWidget::hide()

The scroll bar widget will be resized to fit the scroll bar geometry for the current style. The following describes the case for scroll bar widgets on the horizontal scroll bar:

The height of the widget will be set to match the height of the scroll bar. To control the width of the widget, use QWidget::setMinimumWidth and QWidget::setMaximumWidth, or implement QWidget::sizeHint() and set a horizontal size policy. If you want a square widget, call QStyle::pixelMetric(QStyle::PM_ScrollBarExtent) and set the width to this value.

This function was introduced in Qt 4.2.

参见 scrollBarWidgets().

[virtual protected] void QAbstractScrollArea::contextMenuEvent(QContextMenuEvent *e)

重新实现 QWidget::contextMenuEvent().

This event handler can be reimplemented in a subclass to receive context menu events for the viewport() widget. The event is passed in e.

参见 QWidget::contextMenuEvent().

QWidget *QAbstractScrollArea::cornerWidget() const

Returns the widget in the corner between the two scroll bars.

By default, no corner widget is present.

This function was introduced in Qt 4.2.

参见 setCornerWidget().

[virtual protected] void QAbstractScrollArea::dragEnterEvent(QDragEnterEvent *event)

重新实现 QWidget::dragEnterEvent().

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

参见 QWidget::dragEnterEvent().

[virtual protected] void QAbstractScrollArea::dragLeaveEvent(QDragLeaveEvent *event)

重新实现 QWidget::dragLeaveEvent().

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

参见 QWidget::dragLeaveEvent().

[virtual protected] void QAbstractScrollArea::dragMoveEvent(QDragMoveEvent *event)

重新实现 QWidget::dragMoveEvent().

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

参见 QWidget::dragMoveEvent().

[virtual protected] void QAbstractScrollArea::dropEvent(QDropEvent *event)

重新实现 QWidget::dropEvent().

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

参见 QWidget::dropEvent().

[virtual protected] bool QAbstractScrollArea::event(QEvent *event)

重新实现 QObject::event().

This is the main event handler for the QAbstractScrollArea widget (not the scrolling area viewport()). The specified event is a general event object that may need to be cast to the appropriate class depending on its type.

参见 QEvent::type().

QScrollBar *QAbstractScrollArea::horizontalScrollBar() const

Returns the horizontal scroll bar.

参见 setHorizontalScrollBar(), horizontalScrollBarPolicy, and verticalScrollBar().

[virtual protected] void QAbstractScrollArea::keyPressEvent(QKeyEvent *e)

重新实现 QWidget::keyPressEvent().

This function is called with key event e when key presses occur. It handles PageUp, PageDown, Up, Down, Left, and Right, and ignores all other key presses.

QSize QAbstractScrollArea::maximumViewportSize() const

Returns the size of the viewport as if the scroll bars had no valid scrolling range.

[virtual] QSize QAbstractScrollArea::minimumSizeHint() const

重新实现 QWidget::minimumSizeHint().

[virtual protected] void QAbstractScrollArea::mouseDoubleClickEvent(QMouseEvent *e)

重新实现 QWidget::mouseDoubleClickEvent().

This event handler can be reimplemented in a subclass to receive mouse double click events for the viewport() widget. The event is passed in e.

参见 QWidget::mouseDoubleClickEvent().

[virtual protected] void QAbstractScrollArea::mouseMoveEvent(QMouseEvent *e)

重新实现 QWidget::mouseMoveEvent().

This event handler can be reimplemented in a subclass to receive mouse move events for the viewport() widget. The event is passed in e.

参见 QWidget::mouseMoveEvent().

[virtual protected] void QAbstractScrollArea::mousePressEvent(QMouseEvent *e)

重新实现 QWidget::mousePressEvent().

This event handler can be reimplemented in a subclass to receive mouse press events for the viewport() widget. The event is passed in e.

参见 QWidget::mousePressEvent().

[virtual protected] void QAbstractScrollArea::mouseReleaseEvent(QMouseEvent *e)

重新实现 QWidget::mouseReleaseEvent().

This event handler can be reimplemented in a subclass to receive mouse release events for the viewport() widget. The event is passed in e.

参见 QWidget::mouseReleaseEvent().

[virtual protected] void QAbstractScrollArea::paintEvent(QPaintEvent *event)

重新实现 QWidget::paintEvent().

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

Note: If you open a painter, make sure to open it on the viewport().

参见 QWidget::paintEvent().

[virtual protected] void QAbstractScrollArea::resizeEvent(QResizeEvent *event)

重新实现 QWidget::resizeEvent().

This event handler can be reimplemented in a subclass to receive resize events (passed in event), 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().

参见 QWidget::resizeEvent().

QWidgetList QAbstractScrollArea::scrollBarWidgets(Qt::Alignment alignment)

Returns a list of the currently set scroll bar widgets. alignment can be any combination of the four location flags.

This function was introduced in Qt 4.2.

参见 addScrollBarWidget().

[virtual protected] void QAbstractScrollArea::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).

void QAbstractScrollArea::setCornerWidget(QWidget *widget)

Sets the widget in the corner between the two scroll bars to be widget.

You will probably also want to set at least one of the scroll bar 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 scroll area when it is destroyed 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.

This function was introduced in Qt 4.2.

参见 cornerWidget(), horizontalScrollBarPolicy, and horizontalScrollBarPolicy.

void QAbstractScrollArea::setHorizontalScrollBar(QScrollBar *scrollBar)

Replaces the existing horizontal scroll bar with scrollBar, and sets all the former scroll bar's slider properties on the new scroll bar. The former scroll bar is then deleted.

QAbstractScrollArea already provides horizontal and vertical scroll bars by default. You can call this function to replace the default horizontal scroll bar with your own custom scroll bar.

This function was introduced in Qt 4.2.

参见 horizontalScrollBar() and setVerticalScrollBar().

void QAbstractScrollArea::setVerticalScrollBar(QScrollBar *scrollBar)

Replaces the existing vertical scroll bar with scrollBar, and sets all the former scroll bar's slider properties on the new scroll bar. The former scroll bar is then deleted.

QAbstractScrollArea already provides vertical and horizontal scroll bars by default. You can call this function to replace the default vertical scroll bar with your own custom scroll bar.

This function was introduced in Qt 4.2.

参见 verticalScrollBar() and setHorizontalScrollBar().

void QAbstractScrollArea::setViewport(QWidget *widget)

Sets the viewport to be the given widget. The QAbstractScrollArea will take ownership of the given widget.

If widget is 0, QAbstractScrollArea will assign a new QWidget instance for the viewport.

This function was introduced in Qt 4.2.

参见 viewport().

[protected] void QAbstractScrollArea::setViewportMargins(int left, int top, int right, int bottom)

Sets the margins around the scrolling area to left, top, right and bottom. This is useful for applications such as spreadsheets with "locked" rows and columns. The marginal space is is left blank; put widgets in the unused area.

Note that this function is frequently called by QTreeView and QTableView, so margins must be implemented by QAbstractScrollArea subclasses. Also, if the subclasses are to be used in item views, they should not call this function.

By default all margins are zero.

参见 viewportMargins().

[protected] void QAbstractScrollArea::setViewportMargins(const QMargins &margins)

Sets margins around the scrolling area. This is useful for applications such as spreadsheets with "locked" rows and columns. The marginal space is is left blank; put widgets in the unused area.

By default all margins are zero.

This function was introduced in Qt 4.6.

参见 viewportMargins().

[virtual] void QAbstractScrollArea::setupViewport(QWidget *viewport)

This slot is called by QAbstractScrollArea after setViewport(viewport) has been called. Reimplement this function in a subclass of QAbstractScrollArea to initialize the new viewport before it is used.

参见 setViewport().

[virtual] QSize QAbstractScrollArea::sizeHint() const

重新实现 QWidget::sizeHint().

Returns the sizeHint property of the scroll area. The size is determined by using viewportSizeHint() plus some extra space for scroll bars, if needed.

QScrollBar *QAbstractScrollArea::verticalScrollBar() const

Returns the vertical scroll bar.

参见 setVerticalScrollBar(), verticalScrollBarPolicy, and horizontalScrollBar().

QWidget *QAbstractScrollArea::viewport() const

Returns the viewport widget.

Use the QScrollArea::widget() function to retrieve the contents of the viewport widget.

参见 setViewport() and QScrollArea::widget().

[virtual protected] bool QAbstractScrollArea::viewportEvent(QEvent *event)

The main event handler for the scrolling area (the viewport() widget). It handles the event specified, and can be called by subclasses to provide reasonable default behavior.

Returns true to indicate to the event system that the event has been handled, and needs no further processing; otherwise returns false to indicate that the event should be propagated further.

You can reimplement this function in a subclass, but we recommend using one of the specialized event handlers instead.

Specialized handlers for viewport events are: paintEvent(), mousePressEvent(), mouseReleaseEvent(), mouseDoubleClickEvent(), mouseMoveEvent(), wheelEvent(), dragEnterEvent(), dragMoveEvent(), dragLeaveEvent(), dropEvent(), contextMenuEvent(), and resizeEvent().

[protected] QMargins QAbstractScrollArea::viewportMargins() const

Returns the margins around the scrolling area. By default all the margins are zero.

This function was introduced in Qt 5.5.

参见 setViewportMargins().

[virtual protected] QSize QAbstractScrollArea::viewportSizeHint() const

Returns the recommended size for the viewport. The default implementation returns viewport()->sizeHint(). Note that the size is just the viewport's size, without any scroll bars visible.

This function was introduced in Qt 5.2.

[virtual protected] void QAbstractScrollArea::wheelEvent(QWheelEvent *e)

重新实现 QWidget::wheelEvent().

This event handler can be reimplemented in a subclass to receive wheel events for the viewport() widget. The event is passed in e.

参见 QWidget::wheelEvent().