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] FlowLayout in a QScrollArea

[Solved] FlowLayout in a QScrollArea

Scheduled Pinned Locked Moved General and Desktop
3 Posts 2 Posters 9.5k Views
  • 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.
  • C Offline
    C Offline
    Chainsawkitten
    wrote on last edited by
    #1

    I am trying to put a FlowLayout (as per the "Flow Layout example":http://doc.qt.nokia.com/4.7/layouts-flowlayout.html) populated with QLabels inside a QScrollArea to be able to scroll it vertically. However, no scrollbar appears even though the content can't fit inside the window. Scrolling with the mouse wheel does nothing and when I force the scrollbar to show (by setting the scrollbar policy to Qt::ScrollBarAlwaysOn), it's greyed out and can't be used. Additionally, when I force the scrollbar visible, the QLabels can get covered by the scrollbar.

    Here's a screenshot outlining the problem(s):
    !http://i.imgur.com/x6BQD.png(screenshot)!

    The code for the widget outlined above:
    @// Create FlowLayout
    FlowLayout *flowLayout = new FlowLayout;

    // Populate FlowLayout with QLabels
    for (int i=0; i<gamelist->size(); i++) {
        QLabel *label = new QLabel(gamelist->getGame(i)->getTitle());
        flowLayout->addWidget(label);
    }
    
    // Create QScrollArea
    QScrollArea *scrollArea = new QScrollArea();
    scrollArea->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
    scrollArea->setLayout(flowLayout);
    
    // Add additional widgets to this widget's layout
    ViewModeWidget *viewModeWidget = new ViewModeWidget;
    GameInfoWidget *gameInfoWidget = new GameInfoWidget;
    
    QVBoxLayout *layout = new QVBoxLayout;
    layout->addWidget(viewModeWidget);
    layout->addWidget(scrollArea); // QScrollArea added here
    layout->addWidget(gameInfoWidget);
    
    setLayout(layout);@
    

    Notes:

    • The FlowLayout-class is identical to the one in "the example":http://doc.qt.nokia.com/4.7/layouts-flowlayout.html, no changes have been made.
    • If you think the problem lies elsewhere, just ask and I'll provide the full source. It's a non-commercial project so no secrets here.
    1 Reply Last reply
    0
    • A Offline
      A Offline
      andre
      wrote on last edited by
      #2

      I think you set the layout on the wrong widget. Instead of setting the flowlayout on the scrollArea (as you do in line 13), you should set it on the widget inside the scrollarea:

      @
      scrollArea->widget()->setLayout(flowLayout);
      @

      1 Reply Last reply
      0
      • C Offline
        C Offline
        Chainsawkitten
        wrote on last edited by
        #3

        Thanks for the help! The code you posted crashes the program during runtime since I never set a widget to the scrollArea (hence scrollArea->widget() returns 0). However, the solution was similar.

        The solution was to create a custom widget that holds the flowLayout and set that as the QScrollArea's widget.

        Code for future reference (hi there, people of the future!):
        @ QScrollArea *scrollArea = new QScrollArea();
        scrollArea->setWidgetResizable(true); // Important or else the widget won't expand to the size of the QScrollArea, resulting in the FlowLayout showing up as a vertical list of items rather than a flow layout
        scrollArea->setWidget(<name of custom widget>);@

        Custom widget constructor:
        @ // Create FlowLayout
        FlowLayout *flowLayout = new FlowLayout;

        // Populate FlowLayout with QLabels
        for (int i=0; i<gamelist->size(); i++) {
            QLabel *label = new QLabel(gamelist->getGame(i)->getTitle());
            flowLayout->addWidget(label);
        }
        
        setLayout(flowLayout);@
        
        1 Reply Last reply
        3

        • Login

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