简体   繁体   中英

QScrollArea - scrollbar for a widget with many childs

So, I got some problems with QScrollArea .

I want to put a widget with many children in a QScrollArea , but I just dont get any scrollbars.

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.

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:

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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