Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [SOLVED] Inserting a list of QWidgets into a QScrollArea
Forum Updated to NodeBB v4.3 + New Features

[SOLVED] Inserting a list of QWidgets into a QScrollArea

Scheduled Pinned Locked Moved General and Desktop
5 Posts 2 Posters 6.6k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • N Offline
    N Offline
    notgary
    wrote on last edited by
    #1

    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:

    !http://i.stack.imgur.com/UdJhe.png!

    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() );
    }@
    
    1 Reply Last reply
    0
    • A Offline
      A Offline
      andre
      wrote on last edited by
      #2

      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.

      1 Reply Last reply
      0
      • N Offline
        N Offline
        notgary
        wrote on last edited by
        #3

        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?

        1 Reply Last reply
        0
        • A Offline
          A Offline
          andre
          wrote on last edited by
          #4

          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.

          1 Reply Last reply
          0
          • N Offline
            N Offline
            notgary
            wrote on last edited by
            #5

            Thanks a lot for your advice :)

            1 Reply Last reply
            0

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved