简体   繁体   English

QScrollArea-具有许多子项的小部件的滚动条

[英]QScrollArea - scrollbar for a widget with many childs

So, I got some problems with QScrollArea . 因此,我在QScrollArea遇到了一些问题。

I want to put a widget with many children in a QScrollArea , but I just dont get any scrollbars. 我想在QScrollArea放置一个包含许多子项的小部件,但我只是没有任何滚动条。

Heres the code: 这是代码:

QDialog *dialog = new QDialog();
QVBoxLayout *dialoglayout = new QVBoxLayout( dialog );

QScrollArea *area = new QScrollArea();

dialoglayout->setMargin( 0 );
dialoglayout->addWidget( area );

area->setAlignment( Qt::AlignCenter );
area->setAlignment( Qt::AlignTop );
area->setWidgetResizable( true );

// mainwidget has a lot of children
QWidget *mainwidget = randomclass.getWidget();
QVBoxLayout *mainwidgetlayout = new QVBoxLayout( mainwidget );

dialog->setWindowFlags( Qt::Window );

area->setWidget( mainwidget );

dialog->showMaximized();

if the mainwidget is bigger than the scrollarea, the content just overflows. 如果mainwidget大于滚动区域,则内容只会溢出。

Can anyone help me? 谁能帮我? Thanks in advance. 提前致谢。

Some widgets don't report the area to scroll and that causes the confusion with scroll area. 有些小部件不报告要滚动的区域,这会导致滚动区域混乱。 For QScrollarea object to adjust to the content: 为QScrollarea对象调整内容:

myWidget->setMinimumSize(myWidget->sizeHint());  // assume the min size 
scrollArea->setWidget( myWidget );               // use that widget in scroll area

Specific scrollers also might need to be enabled depending on the content: 根据内容,还可能需要启用特定的滚动条:

scrollArea->setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded);
scrollArea->setHorizontalScrollBarPolicy(Qt::ScrollBarAsNeeded);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM