QScrollBar Class

QScrollBar 提供一个垂直或水平滚动条. 更多...

头文件: #include <QScrollBar>
qmake: QT += widgets
基类: QAbstractSlider

公有函数

QScrollBar(QWidget *parent = Q_NULLPTR)
QScrollBar(Qt::Orientation orientation, QWidget *parent = Q_NULLPTR)
~QScrollBar()

重新实现的公有函数

virtual bool event(QEvent *event)
virtual QSize sizeHint() const

受保护的函数

void initStyleOption(QStyleOptionSlider *option) const

重新实现的受保护函数

virtual void contextMenuEvent(QContextMenuEvent *event)
virtual void hideEvent(QHideEvent *)
virtual void mouseMoveEvent(QMouseEvent *e)
virtual void mousePressEvent(QMouseEvent *e)
virtual void mouseReleaseEvent(QMouseEvent *e)
virtual void paintEvent(QPaintEvent *)
virtual void sliderChange(SliderChange change)
virtual void wheelEvent(QWheelEvent *event)

其他继承的成员

详细描述

QScrollBar 提供一个垂直或水平滚动条.

滚动条是一个控件, 辅助用户查阅文档显示widget中无法显示的部分. 滚动条为用户在文档的当前位置和文档的内容大小提供可视化标识.通常, 滚动条与其他控件配合实现精准导航. Qt在不同的平台显示相应样式的滚动条.

如果你需要在其他widget提供滚动视图, 那么使用 QScrollArea 可能更便捷, 因为 QScrollArea 提供一个视口widget和滚动条. 如果你需要使用QAbstractScrollArea实现类似QScrollBar功能, 直接使用 QScrollBar 可能更好; 例如, 你决定子类化 QAbstractItemView. 对于多数使用滑块控件在给定范围获取值的情况, QSlider 可能更适合.

Scroll bars typically include four separate controls: a slider, scroll arrows, and a page control.
  • a. The slider provides a way to quickly go to any part of the document, but does not support accurate navigation within large documents.
  • b. The scroll arrows are push buttons which can be used to accurately navigate to a particular place in a document. For a vertical scroll bar connected to a text editor, these typically move the current position one "line" up or down, and adjust the position of the slider by a small amount. In editors and list boxes a "line" might mean one line of text; in an image viewer it might mean 20 pixels.
  • c. The page control is the area over which the slider is dragged (the scroll bar's background). Clicking here moves the scroll bar towards the click by one "page". This value is usually the same as the length of the slider.

每个滚动条都有一个值, 指示滑块距离滚动条起点的距离; 使用 value() 获取值, 使用 setValue()设置值. 这个值始终位于滚动条定义的范围[ minimum() , maximum() ]. 这个范围可以调用 setMinimum() 和 setMaximum()设置. 最小值时, 滑块位于滚动条的顶部或左侧. 最大值时, 滑块位于滚动条的底部或右侧.

滑块长度通常与页面步长相关, 表示滚动视图显示文档在整个文档的占比. T页面步长是用户按下 Page UpPage Down 时值的变化量, 该值调用 setPageStep()设置. 调用setSingleStep() 设置方向键的变化量.

注意: 滚动条值的范围与widget的实际大小无关. 设置值范围和页面步长时不需要考虑widget大小.

滚动条范围值通常与 QSlider 范围值不同, 因此滑块的长度需要考虑. 如果我们有一个100行文档, 并使用一个仅显示20行的小widget, 那么我们希望构造一个最小值是0, 最大值是80, 页面步长是20的滚动条. QScrollBar 给我们一个具有5个 "页"的滚动条.

The relationship between a document length, the range of values used in a scroll bar, and the page step is simple in many common situations. The scroll bar's range of values is determined by subtracting a chosen page step from some value representing the length of the document. In such cases, the following equation is useful: document length = maximum() - minimum() + pageStep().

QScrollBar 仅提供整数范围. 注意: 虽然 QScrollBar 能处理非常大的数字, 但是当前屏幕上, 滚动条无法有效地表示100, 000像素以上的范围. 另外, 用户也很难使用键盘或鼠标控制滑块, 滚动箭头也将受限.

QScrollBar 继承自 QAbstractSlider一组全面的信号:

滚动条可以由键盘控制, 但它的默认 focusPolicy() 是 Qt::NoFocus. 调用 setFocusPolicy() 设置是否启用滚动条的键盘交互功能:

  • Left/Right 移动水平滚动条一步.
  • Up/Down 移动垂直滚动条一步.
  • PageUp 向上移动一页.
  • PageDown 向下移动一页.
  • Home 移动滚动条到起点(最小值).
  • End 移动滚动条到终点(最大值).

调用 triggerAction() 函数模拟用户与滚动条控件的交互. 如果你有许多使用公共值范围的不同小部件, 这将非常有用.

多数GUI样式使用 pageStep() 值来计算滑块的大小.

Screenshot of a Macintosh style scroll barA scroll bar shown in the Macintosh widget style.
Screenshot of a Windows Vista style scroll barA scroll bar shown in the Windows Vista widget style.
Screenshot of a Fusion style scroll barA scroll bar shown in the Fusion widget style.

参见 QScrollArea, QSlider, QDial, QSpinBox, GUI Design Handbook: Scroll Bar, Sliders Example.

成员函数

QScrollBar::QScrollBar(QWidget *parent = Q_NULLPTR)

Constructs a vertical scroll bar.

The parent argument is sent to the QWidget constructor.

The minimum defaults to 0, the maximum to 99, with a singleStep size of 1 and a pageStep size of 10, and an initial value of 0.

QScrollBar::QScrollBar(Qt::Orientation orientation, QWidget *parent = Q_NULLPTR)

Constructs a scroll bar with the given orientation.

The parent argument is passed to the QWidget constructor.

The minimum defaults to 0, the maximum to 99, with a singleStep size of 1 and a pageStep size of 10, and an initial value of 0.

QScrollBar::~QScrollBar()

Destroys the scroll bar.

[virtual protected] void QScrollBar::contextMenuEvent(QContextMenuEvent *event)

重新实现 QWidget::contextMenuEvent().

[virtual] bool QScrollBar::event(QEvent *event)

重新实现 QObject::event().

[virtual protected] void QScrollBar::hideEvent(QHideEvent *)

重新实现 QWidget::hideEvent().

[protected] void QScrollBar::initStyleOption(QStyleOptionSlider *option) const

Initialize option with the values from this QScrollBar. This method is useful for subclasses when they need a QStyleOptionSlider, but don't want to fill in all the information themselves.

参见 QStyleOption::initFrom().

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

重新实现 QWidget::mouseMoveEvent().

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

重新实现 QWidget::mousePressEvent().

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

重新实现 QWidget::mouseReleaseEvent().

[virtual protected] void QScrollBar::paintEvent(QPaintEvent *)

重新实现 QWidget::paintEvent().

[virtual] QSize QScrollBar::sizeHint() const

重新实现 QWidget::sizeHint().

[virtual protected] void QScrollBar::sliderChange(SliderChange change)

重新实现 QAbstractSlider::sliderChange().

[virtual protected] void QScrollBar::wheelEvent(QWheelEvent *event)

重新实现 QWidget::wheelEvent().