[Solved] Scroll Area Size



  • I am attempting to get a scroll area to work and have spent way more time then I have. It is probably easy, I am fairly new here. I made a simple example that I can't make work so I removed the complicated code:

    @
    MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    {
    // Create QScrollArea and make it the CentralWidget
    QScrollArea *scrollArea = new QScrollArea(this);
    setCentralWidget(scrollArea);

    // Set scroll bar policy
    scrollArea->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    scrollArea->setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded);
    //scrollArea->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
    
    // create a horizontal layout for the scroll area
    QHBoxLayout *channelLayout = new QHBoxLayout;
    scrollArea->setLayout(channelLayout);
    
    // Add 4 colums to the scrollArea
    for (int i = 0; i < 4; i++){
        QGroupBox *channelGroup = new QGroupBox();
        QVBoxLayout *groupLayout = new QVBoxLayout(channelGroup);
        channelLayout->addWidget(channelGroup);
        groupLayout->setSizeConstraint( QLayout::SetFixedSize );
        for (int j = 0; j < 20; j++){
            QLabel *waveLabel = new QLabel("Wave: ");
            groupLayout->addWidget(waveLabel);
        }
    }
    

    }
    @


  • Moderators

    Hi,

    Two things that i would like to mention is you setWidgetResizable to true and setMinimumSize for the content widget of ScrollArea greater than the frame of ScrollArea then only the ScrollBars get enabled.
    I modified you example which will explain it
    @
    QScrollArea *scrollArea = new QScrollArea(this);
    setCentralWidget(scrollArea);

    scrollArea->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    scrollArea->setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded);
    scrollArea->setWidgetResizable(true);

    QWidget m_view = new QWidget();
    m_view->setContentsMargins(0,0,0,0);
    m_view->setMinimumSize(0,100
    50);

    QVBoxLayout *vb = new QVBoxLayout;
    m_view->setLayout(vb);

    scrollArea->setWidget(m_view);

    for(int i=0;i<100;i++) {
    QLabel *waveLabel = new QLabel("Wave: "+QString::number(i),m_view);
    vb->addWidget(waveLabel);
    }
    @



  • Thank you for your help. I took what you posted and put it back into my application and came up with this:

    @MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    {
    // Create QScrollArea and make it the CentralWidget
    QScrollArea *scrollArea = new QScrollArea(this);
    setCentralWidget(scrollArea);

    // Set scroll bar policy
    scrollArea->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    scrollArea->setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded);
    //scrollArea->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
    
    QWidget *clientArea = new QWidget();
    clientArea->setContentsMargins(0,0,0,0);
    

    //clientArea->setMinimumSize(0,100*50);

    scrollArea->setWidget(clientArea);
    
    // create a horizontal layout for the scroll area
    QHBoxLayout *channelLayout = new QHBoxLayout;
    channelLayout->setSizeConstraint( QLayout::SetFixedSize );
    clientArea->setLayout(channelLayout);
    
    // Add 4 colums to the scrollArea
    for (int i = 0; i < 4; i++){
        QGroupBox *channelGroup = new QGroupBox();
        QVBoxLayout *groupLayout = new QVBoxLayout(channelGroup);
        channelLayout->addWidget(channelGroup);
        for (int j = 0; j < 20; j++){
            QLabel *waveLabel = new QLabel("Wave: ");
            groupLayout->addWidget(waveLabel);
        }
    }
    

    }
    @

    I found that the call to setMinimumSize() on the widget for the ScrollArea was not necessary. Is this going to be a problem later on?

    I had to call channelLayout->setSizeConstraint( QLayout::SetFixedSize ); to keep the label added to groupLayout from being scalled to fit without scrollbars.

    I have learned that the scrollArea needs a widget and not a layout full of widgets to operate correctly. Thank you


Log in to reply
 

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