Qt World Summit: Register Today!

[SOLVED] Inserting a list of QWidgets into a QScrollArea

  • I'm currently trying to insert a list of custom QWidgets into a QSCrollArea, a list which if all thing go to plan, will be much, much larger than the size of the ScrollArea's viewport. When this happens, I would like to be able to scroll down through the widgets until I reach the bottom, but instead all my widgets are being crushed up together so they will all fit onscreen at once. This screenshot illustrates my current situation:


    The scroll area is being populated by setting a QVBoxLayout on its widget and adding the widgets to that. The code I am using for both the widget and scroll area can be seen below. Can anyone please tell me what I need to do to achieve when I'm after?

    @ArticleListWidget::ArticleListWidget( QWidget *parent ) : QScrollArea( parent )
      centralWidget = new QWidget( this );
      setWidget( centralWidget );
      layout = new QVBoxLayout( this );
      setWidgetResizable( true );
    void ArticleListWidget::addWidget( QWidget *widget )
      if( widget != NULL ){
        layout->addWidget( widget );
    ArticleWidget::ArticleWidget( QWidget *parent ) : QFrame( parent )
      labelTitle = new QLabel( this );
      labelTitle->setText( "Title" );
      labelAuthor = new QLabel( this );
      labelAuthor->setText( "Author" );
      layout = new QVBoxLayout( this );
      layout->addWidget( labelTitle );
      layout->addWidget( labelAuthor );
      setFrameStyle( QFrame::StyledPanel | QFrame::Plain );
      setMinimumSize( sizeHint() );

  • Set the layout on the centralWidget, and make your ArticleWidgets children of that central widget. You should re-read the documentation on the different widgets involved in a QScrollArea...

    Then again: It seems you really need a QListView or something like that instead. Using widgets in a QScrollArea in these quantities is not going to perform well. QScrollArea with a list of widgets is fine for a limited amount of entries, but if that grows to any significant numbers, the widgets will be too heavy to work well.

  • Ah, that was it. Thanks a lot :)

    In what situations would a QListView be more appropriate than simply using a QScrollArea? The example I've given above seems rather simple just now, but the ArticleWidgets are going to be rather more complicated in the future, possessing heavily customised styles and additional widgets such as buttons.

    Would a QListView or QListWidget be more suitable for handling something like that or am I better off doing it the way I currently am?

  • It depends...

    Qt item views and widgets don't mix all that well. That makes making interactive items in views (with buttons or more complicated embedded widgets) hard to do in a way that still performs well, whatever approach you choose.

    You might considder if embedding a QML view and creating your list view as a QML item would work well for you. QML makes it easy using delegates to apply styles and put interactive elements on each item. You can use a [[doc:QDeclarativeView]] to embed a piece of QML in a QWidget based application.

    There are trade-offs with each of the approaches though.

  • Thanks a lot for your advice :)

Log in to reply