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] Scroll Area Size

[Solved] Scroll Area Size

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

    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);
        }
    }
    

    }
    @

    1 Reply Last reply
    0
    • p3c0P Offline
      p3c0P Offline
      p3c0
      Moderators
      wrote on last edited by
      #2

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

      157

      1 Reply Last reply
      0
      • S Offline
        S Offline
        sting
        wrote on last edited by
        #3

        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

        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