Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

QScrollArea : doesn't expand to parent size



  • I've a big issue with scroll area. I've already been able to make it work in a different class of the application I'm developing, after many tries. This time I can't find a way to make the scroll area extend to its parent height. It always keeps a minimum height , just enough to show the 2 buttons to scroll up and down and being able to see the content is correctly created. The content widget extends to contain the labels as it should.
    Main container is derives from QMdiSubWindow.
    The following code is extracted from constructor. Label creation is not: labels are actually added at runtime.

    QScrollArea * labelScrollArea;
    QWidget * spaceDivider;
    QWidget * labelContainer;
    QVBoxLayout * labelContainerLayout;
    QHBoxLayout * dividerLayout;
    QVBoxLayout * rightPanelLayout;
    QWidget * rightPanel;
    QWidget * leftPanel;
    QVBoxLayout * leftPanelLayout;
    
    
    spaceDivider = new QWidget();
    spaceDivider->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
    dividerLayout = new QHBoxLayout();
    spaceDivider->setLayout(dividerLayout);
    
    // plot on the left
    
    leftPanel = new QWidget();
    leftPanelLayout = new QVBoxLayout();
    leftPanel->setLayout(leftPanelLayout);
    
    //...
    
    // right panel
    rightPanel = new QWidget();
    rightPanel->setFixedWidth(labelFontSize * 20);
    rightPanel->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Expanding);
    rightPanel->setStyleSheet("background-color:black;");
    
    rightPanelLayout = new QVBoxLayout();
    rightPanel->setLayout(rightPanelLayout);
    rightPanel->layout()->setAlignment(Qt::AlignCenter);
    
    labelScrollArea = new QScrollArea(rightPanel);
    labelScrollArea->setStyleSheet("background-color:grey;");
    labelScrollArea->setFixedWidth(labelFontSize * 20);
    labelScrollArea->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Expanding);
    labelScrollArea->setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded);
    labelScrollArea->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    labelScrollArea->setWidgetResizable(true);
    
    labelContainer = new QWidget();
    labelContainer->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
    labelContainerLayout = new QVBoxLayout();
    labelContainer->setLayout(labelContainerLayout);
    labelContainer->layout()->setAlignment( Qt::AlignTop );
    labelContainer->setContentsMargins(0, 0, 0, 0);
    
    labelScrollArea->setWidget(labelContainer);
    labelContainer->setMinimumHeight(0);
    
    // add to support widget
    //dividerLayout->addWidget(plot);
    dividerLayout->addWidget(leftPanel);
    dividerLayout->addWidget(rightPanel);
    
    // add support widget to subwindow
    setWidget(spaceDivider);
    
    for (int index = 0; index < 100; index++ )
    {
           QLabel * label = new QLabel(NULL);
            label->setFixedHeight(40);
            label->setFixedWidth(labelFont.pointSize() * 18);
            label->setFont(labelFont);
            QPalette palette = label->palette();
            palette.setColor(label->backgroundRole(), Qt::black);
            palette.setColor(label->foregroundRole(), Qt:white);
            label->setPalette(palette);
            label->setText("Test");
            labelContainerLayout->addWidget(label);  
    }
    

    Any idea to make it work? Thank you


  • Lifetime Qt Champion

    Hi and welcome to devnet,

    You are not putting labelScrollArea in any layout hence it won't follow anything except it's initial size.



  • Thankyou very much! it worked as you suggested, by adding

    rightPanelLayout->addWidget(labelScrollArea);

  • Lifetime Qt Champion

    Great !

    Since you have it working now, please mark the thread as solved using the "Topic Tools" button so that other forum users may know a solution has been found :)


Log in to reply