QScrollArea Class
QScrollArea 为其他widget提供滚动视图. 更多...
头文件: | #include <QScrollArea> |
qmake: | QT += widgets |
基类: | QAbstractScrollArea |
属性
- alignment : Qt::Alignment
- widgetResizable : bool
- 3 个属性继承自 QAbstractScrollArea
- 6 个属性继承自 QFrame
- 59 个属性继承自 QWidget
- 1 个属性继承自 QObject
公有函数
QScrollArea(QWidget *parent = Q_NULLPTR) | |
~QScrollArea() | |
Qt::Alignment | alignment() const |
void | ensureVisible(int x, int y, int xmargin = 50, int ymargin = 50) |
void | ensureWidgetVisible(QWidget *childWidget, int xmargin = 50, int ymargin = 50) |
void | setAlignment(Qt::Alignment) |
void | setWidget(QWidget *widget) |
void | setWidgetResizable(bool resizable) |
QWidget * | takeWidget() |
QWidget * | widget() const |
bool | widgetResizable() const |
重新实现的公有函数
virtual bool | focusNextPrevChild(bool next) |
virtual QSize | sizeHint() const |
- 20 个公有函数继承自 QAbstractScrollArea
- 14 个公有函数继承自 QFrame
- 214 个公有函数继承自 QWidget
- 32 个公有函数继承自 QObject
- 14 个公有函数继承自 QPaintDevice
重新实现的受保护函数
virtual bool | event(QEvent *e) |
virtual bool | eventFilter(QObject *o, QEvent *e) |
virtual void | resizeEvent(QResizeEvent *) |
virtual void | scrollContentsBy(int dx, int dy) |
virtual QSize | viewportSizeHint() const |
- 20 个受保护的函数继承自 QAbstractScrollArea
- 4 个受保护的函数继承自 QFrame
- 35 个受保护的函数继承自 QWidget
- 9 个受保护的函数继承自 QObject
- 1 个受保护的函数继承自 QPaintDevice
其他继承的成员
- 19 个公有槽函数继承自 QWidget
- 1 个公有槽函数继承自 QObject
- 3 个信号继承自 QWidget
- 2 个信号继承自 QObject
- 1 个公有变量继承自 QObject
- 5 个静态公有成员继承自 QWidget
- 10 个静态公有成员继承自 QObject
- 20 个受保护的函数继承自 QAbstractScrollArea
- 4 个受保护的函数继承自 QFrame
- 35 个受保护的函数继承自 QWidget
- 9 个受保护的函数继承自 QObject
- 1 个受保护的函数继承自 QPaintDevice
- 1 个受保护的槽函数继承自 QWidget
- 2 个受保护的变量继承自 QObject
- 1 protected type inherited from QPaintDevice
详细描述
QScrollArea 为其他widget提供滚动视图.
滚动区域用于显示子widget的内容. 若widget超过边界, 视图可以提供滚动条辅助用户查看所有区域. 调用 setWidget()设置子widget. 例如:
QLabel *imageLabel = new QLabel; QImage image("happyguy.png"); imageLabel->setPixmap(QPixmap::fromImage(image)); scrollArea = new QScrollArea; scrollArea->setBackgroundRole(QPalette::Dark); scrollArea->setWidget(imageLabel);
上述代码创建一个图像标签的滚动区域 (如下图所示). 缩放图像时, 滚动区域自动提供滚动条:
![]() | ![]() | ![]() |
滚动条外观取决于当前设置的 滚动条策略. 你可以使用从 QAbstractScrollArea继承的函数控制滚动条外观.
例如, 你可以设置 QAbstractScrollArea::horizontalScrollBarPolicy 和 QAbstractScrollArea::verticalScrollBarPolicy 属性. 如果你想在滚动区域内容变动时, 动态调整滚动条, 你可以使用 horizontalScrollBar() 和 verticalScrollBar() 函数 (这两个函数可以访问滚动条) . 调用 QScrollBar::setValue() 函数设置滚动条的值.
调用 widget() 获取滚动条的子widget. 调用 setWidgetResizable() 设置视图大小. 调用 setAlignment() 设置子widget的对齐方式.
2个便捷函数 ensureVisible() 和 ensureWidgetVisible() 自动滚动内容以确保设定区域的内容可见.
大小提示和布局
当使用滚动区域显示自定义widget的内容时, 你必须确保子widget的 大小提示 是一个合适的值. 如果标准 QWidget 作为QScrollArea的子widget, 你可能需要调用 QWidget::setMinimumSize() , 用以确保widget内容正确显示在滚动区域.
如果滚动区域显示的widget内容包含布局及widget的子widget, 布局的大小策略也将决定widget的大小, 尤其是你需要动态更改布局的内容. 在此种情况下, 设置布局的 大小约束属性 (如., QLayout::SetMinAndMaxSize)将使滚动条的大小在布局内容改变时更新.
有关QScrollArea 的完整示例参见 Image Viewer . 这个示例演示如何组合 QLabel 和 QScrollArea 显示图像.
参见 QAbstractScrollArea, QScrollBar, Image Viewer Example.
属性
alignment : Qt::Alignment
This property holds the alignment of the scroll area's widget
A valid alignment is a combination of the following flags:
Qt::AlignLeft
Qt::AlignHCenter
Qt::AlignRight
Qt::AlignTop
Qt::AlignVCenter
Qt::AlignBottom
By default, the widget stays rooted to the top-left corner of the scroll area.
This property was introduced in Qt 4.2.
访问函数:
Qt::Alignment | alignment() const |
void | setAlignment(Qt::Alignment) |
widgetResizable : bool
This property holds 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.
访问函数:
bool | widgetResizable() const |
void | setWidgetResizable(bool resizable) |
成员函数
QScrollArea::QScrollArea(QWidget *parent = Q_NULLPTR)
Constructs an empty scroll area with the given parent.
参见 setWidget().
QScrollArea::~QScrollArea()
Destroys the scroll area and its child widget.
参见 setWidget().
void QScrollArea::ensureVisible(int x, int y, int xmargin = 50, int ymargin = 50)
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.
void QScrollArea::ensureWidgetVisible(QWidget *childWidget, int xmargin = 50, int ymargin = 50)
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.
This function was introduced in Qt 4.2.
[virtual protected]
bool QScrollArea::event(QEvent *e)
重新实现 QObject::event().
[virtual protected]
bool QScrollArea::eventFilter(QObject *o, QEvent *e)
重新实现 QObject::eventFilter().
[virtual]
bool QScrollArea::focusNextPrevChild(bool next)
重新实现 QWidget::focusNextPrevChild().
[virtual protected]
void QScrollArea::resizeEvent(QResizeEvent *)
重新实现 QWidget::resizeEvent().
[virtual protected]
void QScrollArea::scrollContentsBy(int dx, int dy)
重新实现 QAbstractScrollArea::scrollContentsBy().
void QScrollArea::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.
The widget's autoFillBackground property will be set to true
.
If the scroll area is visible when the widget is added, you must show() it explicitly.
Note that You must add the layout of widget before you call this function; if you add it later, the widget will not be visible - regardless of when you show() the scroll area. In this case, you can also not show() the widget later.
参见 widget().
[virtual]
QSize QScrollArea::sizeHint() const
重新实现 QWidget::sizeHint().
QWidget *QScrollArea::takeWidget()
Removes the scroll area's widget, and passes ownership of the widget to the caller.
参见 widget().
[virtual protected]
QSize QScrollArea::viewportSizeHint() const
重新实现 QAbstractScrollArea::viewportSizeHint().
QWidget *QScrollArea::widget() const
Returns the scroll area's widget, or 0 if there is none.
参见 setWidget().