So I have this QFrame
which is the parent widget (represented by this
in the code). In this widget, I want to place a QWidget
at 10 px
You messed up the stack of items. The idea of having scrollable area is like this:
QDialog
)QScrollArea
) of fixed sizeQWidget
) of some size, where usually only part of it is visible (it's supposed to be bigger than scrollarea)QPushButton
here)Try this code:
int
main( int _argc, char** _argv )
{
QApplication app( _argc, _argv );
QDialog * dlg = new QDialog();
dlg->setGeometry( 100, 100, 260, 260);
QScrollArea *scrollArea = new QScrollArea( dlg );
scrollArea->setVerticalScrollBarPolicy( Qt::ScrollBarAlwaysOn );
scrollArea->setWidgetResizable( true );
scrollArea->setGeometry( 10, 10, 200, 200 );
QWidget *widget = new QWidget();
scrollArea->setWidget( widget );
QVBoxLayout *layout = new QVBoxLayout();
widget->setLayout( layout );
for (int i = 0; i < 10; i++)
{
QPushButton *button = new QPushButton( QString( "%1" ).arg( i ) );
layout->addWidget( button );
}
dlg->show();
return app.exec();
}
Worth to mention about QScrollArea::setWidgetResizable
, which adjusts the child widget size dynamically according to its content.
And the result looks like this: