[Solved] FlowLayout in a QScrollArea



  • 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.


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



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

Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.