Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to add QGridLayout to QScrollArea?
Qt 6.12 LTS is out! See what's new in the release blog

How to add QGridLayout to QScrollArea?

Scheduled Pinned Locked Moved Solved General and Desktop
19 Posts 5 Posters 8.8k Views 2 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.
  • T Offline
    T Offline
    TomNow99
    wrote on last edited by TomNow99
    #4

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

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by SGaist
      #5

      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.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • T Offline
        T Offline
        TomNow99
        wrote on last edited by
        #6

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

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #7

          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.

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          1
          • T Offline
            T Offline
            TomNow99
            wrote on last edited by
            #8

            @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*>

            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #9

              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.

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              0
              • T Offline
                T Offline
                TomNow99
                wrote on last edited by TomNow99
                #10

                @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

                JonBJ 1 Reply Last reply
                0
                • SGaistS Offline
                  SGaistS Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on last edited by
                  #11

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

                  Interested in AI ? www.idiap.ch
                  Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                  1 Reply Last reply
                  0
                  • T Offline
                    T Offline
                    TomNow99
                    wrote on last edited by
                    #12

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

                    1 Reply Last reply
                    0
                    • T TomNow99

                      @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

                      JonBJ Offline
                      JonBJ Offline
                      JonB
                      wrote on last edited by JonB
                      #13

                      @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)?

                      1 Reply Last reply
                      0
                      • T Offline
                        T Offline
                        TomNow99
                        wrote on last edited by
                        #14

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

                        1 Reply Last reply
                        0
                        • SGaistS Offline
                          SGaistS Offline
                          SGaist
                          Lifetime Qt Champion
                          wrote on last edited by
                          #15

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

                          Interested in AI ? www.idiap.ch
                          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                          1 Reply Last reply
                          1
                          • mrjjM Offline
                            mrjjM Offline
                            mrjj
                            Lifetime Qt Champion
                            wrote on last edited by
                            #16

                            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);
                                });
                            
                            1 Reply Last reply
                            1
                            • T Offline
                              T Offline
                              TomNow99
                              wrote on last edited by
                              #17

                              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

                              1 Reply Last reply
                              0
                              • B Offline
                                B Offline
                                Bonnie
                                wrote on last edited by Bonnie
                                #18

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

                                layoutWithLabels->setSizeConstraint(QLayout::SetMinimumSize);
                                

                                QLayout::SetMinAndMaxSize also works.

                                1 Reply Last reply
                                4
                                • T Offline
                                  T Offline
                                  TomNow99
                                  wrote on last edited by
                                  #19

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

                                  Thanks for all you:@mrjj @JonB @SGaist

                                  1 Reply Last reply
                                  1

                                  • Login

                                  • Login or register to search.
                                  • First post
                                    Last post
                                  0
                                  • Categories
                                  • Recent
                                  • Tags
                                  • Popular
                                  • Users
                                  • Groups
                                  • Search
                                  • Get Qt
                                  • Unsolved