滚动视图QScrollArea

来源:互联网 发布:怎么样做淘宝生意 编辑:程序博客网 时间:2024/06/15 16:07

1.代码片段

void DiagramWid::initScrollArea(){    QDesktopWidget* desktopWidget = QApplication::desktop();    this->resize(desktopWidget->width()-200,desktopWidget->height()-240);    QScrollArea *s = new QScrollArea(this);    s->setAutoFillBackground(true);    s->setGeometry(0, 0, this->width()-10,this->height()-15);//也可用resize    qDebug() << "this" <<this->width() << "heigth:" << this->height();    //垂直滚动条不可见,只能通过鼠标滑动    s->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);    mWid = new QWidget(s);    QHBoxLayout *layout = new QHBoxLayout(mWid);    layout->addWidget(mView);    mWid->setGeometry(0, 0,this->width()+200 ,this->height());    //设置滚动区域的窗体    s->setWidget(mWid);}

2.注意细节

这个滚动视图虽然看起来简单,但是在创建时,如果稍不注意,就会进入各种坑,现在总结一下
1.QScrollArea 在创建时,可设置其父控件,滚动区域尺寸一定不能比其父控件还大,否则可能无法看到水平或者垂直滚动条
2. s->setWidget(mWid),滚动区域窗体需要比滚动窗体大,才能看到水平或垂直滚动条