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

How to add QGridLayout to QScrollArea?



  • Hi,

    I have QGridLayout with 10 QLabels. After 5 seconds I will add to QGridLayout next QLabel, so after 5 minutes I will have many QLabels in QGridLayout. Each QLabel has FixedSize. I know that is impossible to have them in the screen at the same moment, so I would like to have Scroll Bar. I know that is QScrollArea class. I try something like that:

    centralWidget()->setLayout(layout);
    layout->addWidget(scrollArea);
    scrollArea->setLayout(layoutWithQLabels);
    

    But I don't get scroll bar and QLabels are overlap.

    I would like something like that:

    from.png



  • @TomNow99

    QWidget *scrollWidget = new QWidget;
    scrollArea->setWidget(scrollWidget);
    scrollWidget->setLayout(layoutWithQLabels);
    

  • Lifetime Qt Champion

    Hi
    Would it not be easier just to use QListWidget?
    Its a list of text lines that can scroll already.



  • @mrjj I need QGridLayout

    @JonB Thank you, but what with scrollArea?
    Now I have:

        scrollWidget = new QWidget;
        area->setWidget(scrollWidget);
        scrollWidget->setLayout(layoutWithLabels);
    
        layout->addWidget(area);
        centralWidget()->setLayout(layout);
    

    And I don't see any QLabels.

    EDIT
    I try setCentralWidget(area); but this doesn't work too...


  • Lifetime Qt Champion

    Hi,

    Don't set a layout on a QScrollArea. Set it on the widget contained in the scroll area.

    Then set the scroll area as central widget.



  • @SGaist Hi,
    My code:

        layoutWithLabels = new QGridLayout;
        scrollWidget = new QWidget;
        area= new QScrollArea;
        area->setWidget(scrollWidget);
        scrollWidget->setLayout(layoutWithLabels);
        setCentralWidget(area);
    

    and I add Widgets to layoutWithLabels. But I doesn't see any QLabels.


  • Lifetime Qt Champion

    Well, the code you show here does not contain the part related to the labels. The issue might be in what you did not post.



  • @SGaist This is my whole code:

     layoutWithLabels = new QGridLayout;
        scrollWidget = new QWidget;
        area= new QScrollArea;
        area->setWidget(scrollWidget);
        scrollWidget->setLayout(layoutWithLabels);
        setCentralWidget(area);
    
    
        for(int i=0;i<10;i++)
        {
            wektor.append(new QLabel("randomText"));
            wektor[i]->setStyleSheet("QLabel {background-color:red}");
    
            layoutWithLabels->addWidget(wektor[i],i,0, Qt::AlignRight);
    
            wektor[i]->setFixedSize(50,20);
        }
    

    wektor is QVector<QLabel*>


  • Lifetime Qt Champion

    Just a silly idea: build the complete widget first so it has all its content in place and as last step create the scroll area and put the widget in.



  • @SGaist hmmm, it works, but there is a new problem.

    Now my code looks like:

        layoutWithLabels = new QGridLayout;
    
        for(int i=0;i<30;i++)
        {
            wektor.append(new QLabel(QString::number(i)));
            wektor[i]->setStyleSheet("QLabel {background-color:red}");
    
            layoutWithLabels->addWidget(wektor[i],i,0);
    
            wektor[i]->setFixedSize(50,20);
        }
        scrollWidget = new QWidget;
        scrollWidget->setLayout(layoutWithLabels);
        area= new QScrollArea;
        area->setWidget(scrollWidget);
    
        setCentralWidget(area);
    

    But if you read my first post I would like to add a new QLabel to QGridLayout after 5 sec. I have QTimer and slot:

    void MainWindow::timeoutSlot()
    {
        static int i=30;
        wektor.append(new QLabel(QString::number(i)));
        wektor[i]->setStyleSheet("QLabel {background-color:red}");
    
        layoutWithLabels->addWidget(wektor[i],i,0);
    
        wektor[i]->setFixedSize(50,20);
        i++;
    }
    

    and it add new QLabel, but look:
    This is my app after start:
    good.png

    and this is my app after add new QLabels:
    bad.png

    QLabels are overlap


  • Lifetime Qt Champion

    There's no need for that static i variable. You can use the vector size for your computation.



  • @SGaist Yeah, you are right with static :) But what with the main problem?



  • @TomNow99
    I think QGridLayout's job is to fit everything. You have to give something a size/spacing to make it work. setRowMinimumHeight(int row, int minSize)? setSpacing(int spacing)?



  • @JonB I try setSpacing and setRow but not work :(


  • Lifetime Qt Champion

    Since you have your labels in on column, why not use a QVBoxLayout ?


  • Lifetime Qt Champion

    Hi
    Tried your code and it worked?
    (not including the static int )

    alt text

      QTimer::singleShot(5000, [this, layoutWithLabels]() {
            wektor.append(new QLabel(QString::number( wektor.size()))  );
            wektor.last()->setStyleSheet("QLabel {background-color:blue}");
    
            layoutWithLabels->addWidget(wektor.last(), wektor.size(), 0);
    
            wektor.last()->setFixedSize(50, 20);
        });
    


  • Hello,
    @SGaist You are right. I started from QVBoxLayout, but I had problem with Alingment QLabels in layout, so I use QGridLayout.

    @mrjj Please repeat add QLabel many Times. Of course you can change 5 sec to 0.5 sec. When I use your code I get:

    bad2.png



  • [EDITED]
    Removed my previous answer.
    I've found the solution:

    layoutWithLabels->setSizeConstraint(QLayout::SetMinimumSize);
    

    QLayout::SetMinAndMaxSize also works.



  • @Bonnie P E R F E C T! Thank you!

    Thanks for all you:@mrjj @JonB @SGaist


Log in to reply