QScrollArea介绍:
QScrollArea在一个部件中提供一个滚动的视图,滚动区域通常用来在一个框架(区域)显示子部件的内容。如果部件的大小超过了框架的大小,Scroll视图将会提供滚动条,以便可以浏览子部件的全部区域,子部件必须通过QScrollArea中setWidget()函数指定。
例如:
QLabel *imageLabel = new QLabel;
QImage image("happyguy.png");
imageLabel->setPixmap(QPixmap::fromImage(image));
scrollArea = new QScrollArea;
scrollArea->setBackgroundRole(QPalette::Dark);
scrollArea->setWidget(imageLabel);
setWidget函数介绍:
设置scroll area的widget,这个部件将会成为scrol area的子部件,当scroll area被释放时,将自动
或者重新设置新部件时,将会摧毁该部件。部件的autofillbackground属性将设置为true。
当部件被添加时,此时如果scroll area可见,你必须使用它的show显示调用。
注意:
在你调用该函数时,
ote 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.
翻译:
请注意:
在调用此函数之前,必须添加小部件的布局;如果以后添加,无论何时显示滚动区域,小部件都将不可见。在这种情况下,以后也不能显示小部件
the layout of widget是指那个?
scroll area?还是子部件?
使用注意事项:
1.提供一个frame(区域),让QScrollArea为子部件。
2.用布局管理器layout管理QScrollArea。(这样才能自动显示滚动条)
#include "widget.h"
#include <QApplication>
#include "QScrollArea"
#include "QVBoxLayout"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QWidget *mainwindow = new QWidget(); //frame
mainwindow->resize(330,220);
QWidget *backwidget = new QWidget(); //子部件
backwidget->setFixedSize(400,320);
backwidget->setStyleSheet("background: yellow");
QVBoxLayout * layout = new QVBoxLayout();
QScrollArea *scrollArea = new QScrollArea(mainwindow) ; //滚动部件
scrollArea->setBackgroundRole(QPalette::Dark);
scrollArea->setWidget(backwidget); //设置滚动部件
layout->addWidget(scrollArea); //添加QScrollArea布局
mainwindow->setLayout(layout);
mainwindow->show();
return a.exec();
}
效果: