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. QGroupBox, QScrollArea, QFormLayout
Forum Updated to NodeBB v4.3 + New Features

QGroupBox, QScrollArea, QFormLayout

Scheduled Pinned Locked Moved Solved General and Desktop
41 Posts 7 Posters 7.3k Views 4 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
    SGaist
    Lifetime Qt Champion
    wrote on 28 Oct 2021, 18:40 last edited by
    #15

    Hi,

    As @JonB already wrote, QScrollArea is a container widget so 99% of the time you do not set a layout on it. You are in these 99%.

    As already suggested, use a QWidget on which you apply the QFormLayout and set that widget on the QScrollArea.

    You will then have the result you expect.

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

    S 1 Reply Last reply 29 Oct 2021, 06:30
    1
    • S SGaist
      28 Oct 2021, 18:40

      Hi,

      As @JonB already wrote, QScrollArea is a container widget so 99% of the time you do not set a layout on it. You are in these 99%.

      As already suggested, use a QWidget on which you apply the QFormLayout and set that widget on the QScrollArea.

      You will then have the result you expect.

      S Offline
      S Offline
      SPlatten
      wrote on 29 Oct 2021, 06:30 last edited by SPlatten
      #16

      @SGaist , latest code:

              mpobjErrors = new QWidget(this);      //Wrapper/container for everything
              mpvbloErrors = new QVBoxLayout;       //Vertical layout for error messages
              mpsaErrors = new QScrollArea;         //Vertical layout to be scrollable
              mpsaErrors->setWidget(mpobjErrors);   //Add scroll area to container
              mpobjErrors->setLayout(mpvbloErrors); //Add vertical layout to container
              int intFixedHeight(fontMetrics().height() * MainWindow::mscuintErrorRows);
              mpsaErrors->setFixedHeight(intFixedHeight);  //Large enough for 3 rows of errors
              mpvbloErrors->addStretch(1);          
              QPushButton* pbtnAck(new QPushButton(tr("&Clear Errors")));
              QObject::connect(pbtnAck, &QPushButton::clicked,
                  [this, pbtnAck]
                  {
                      mpobjErrors->setVisible(false);
                  }
              );
              mpfrmloErrors = new QFormLayout;       //Layout for errors, time and cause
              mpvbloErrors->addLayout(mpfrmloErrors, 1); //Add form to vertical layout
              mpvbloErrors->addWidget(pbtnAck, 1, Qt::AlignHCenter); //Add button to container
      

      The result from is:
      4c81d34f-b6ab-455f-aa1e-60122d1de022-image.png

      I only want 3 lines to be displayed and the rest to be accessed using the scroll bar, but there is no scroll bar.

      Its the QFormLayout that is used to display the error lines, the QVBoxLayout is used to contain the QFormLayout and the Clear Errors button at the bottom. I want the QFormLayout to be scrollable.

      [edit]...:

          //Create container for errors panel
              mpobjErrors = new QWidget(this);
              mpvbloErrors = new QVBoxLayout;
              mpobjErrors->setLayout(mpvbloErrors);
          //Create a widget that contains the scrollable errors form
              QWidget* pobjFormErrorsContr(new QWidget(mpobjErrors));
              mpfrmloErrors = new QFormLayout;
              pobjFormErrorsContr->setLayout(mpfrmloErrors);
              int intFixedHeight(fontMetrics().height() * MainWindow::mscuintErrorRows);
              mpsaErrors = new QScrollArea;
              mpsaErrors->setWidget(pobjFormErrorsContr);
              mpsaErrors->setFixedHeight(intFixedHeight);
              mpvbloErrors->addWidget(pobjFormErrorsContr, 1, Qt::AlignHCenter);
          //Create a push button for acknowledging and clearing errors panel
              QPushButton* pbtnAck(new QPushButton(tr("&Clear Errors")));
              mpvbloErrors->addWidget(pbtnAck, 1, Qt::AlignHCenter);
              QObject::connect(pbtnAck, &QPushButton::clicked,
                  [this, pbtnAck]
                  {
                      mpobjErrors->setVisible(false);
                  }
              );
          //Default to hidden until errors are available to display
              mpobjErrors->setVisible(false);
      

      I thought perhaps an additional widget container would be required for the QFormLayout, didn't help.

      Kind Regards,
      Sy

      1 Reply Last reply
      0
      • S Offline
        S Offline
        SGaist
        Lifetime Qt Champion
        wrote on 29 Oct 2021, 06:47 last edited by
        #17

        Why do you have a QVBoxLayou and a QFormLayout in mpobjErrors on the widget in your QScrollArea ?

        The logic would rather be

        -- QWidget ("this" in your sample code)
        --- QVBoxLayout
        ---- QScrollArea (fixed height)
        ----- QWidget
        ------ QFormLayout
        ------- Dynamic number of widgets
        ---- QPushButton -> clear button

        So you don't have to hunt for the clear button if you have lots of errors.

        Note that depending on how many errors there might be limiting to three lines may not be the best user experience.

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

        S 1 Reply Last reply 29 Oct 2021, 06:52
        2
        • S SGaist
          29 Oct 2021, 06:47

          Why do you have a QVBoxLayou and a QFormLayout in mpobjErrors on the widget in your QScrollArea ?

          The logic would rather be

          -- QWidget ("this" in your sample code)
          --- QVBoxLayout
          ---- QScrollArea (fixed height)
          ----- QWidget
          ------ QFormLayout
          ------- Dynamic number of widgets
          ---- QPushButton -> clear button

          So you don't have to hunt for the clear button if you have lots of errors.

          Note that depending on how many errors there might be limiting to three lines may not be the best user experience.

          S Offline
          S Offline
          SPlatten
          wrote on 29 Oct 2021, 06:52 last edited by
          #18

          @SGaist , its supposed to be like this:

          QWidget( mpobjErrors )
          -- QVBoxLayout( mpvbloErrors )
          ----- QWidget( pobjFormErrorsContr )
          -------- QScrollArea( fixed height )
          -------- QFormLayout( mpfrmloErrors  )
          ----- QPushButton( pbtnAck )
          

          Where only the QFormLayout is scrollable, but it isn't.

          Kind Regards,
          Sy

          J 1 Reply Last reply 29 Oct 2021, 07:10
          0
          • S SPlatten
            29 Oct 2021, 06:52

            @SGaist , its supposed to be like this:

            QWidget( mpobjErrors )
            -- QVBoxLayout( mpvbloErrors )
            ----- QWidget( pobjFormErrorsContr )
            -------- QScrollArea( fixed height )
            -------- QFormLayout( mpfrmloErrors  )
            ----- QPushButton( pbtnAck )
            

            Where only the QFormLayout is scrollable, but it isn't.

            J Online
            J Online
            JonB
            wrote on 29 Oct 2021, 07:10 last edited by JonB
            #19

            @SPlatten

            ----- QWidget( pobjFormErrorsContr )
            -------- QScrollArea( fixed height )
            -------- QFormLayout( mpfrmloErrors  )
            

            But I/we already said that QScrollArea takes a setWidget(someWidget) for the thing it wants to scroll, which has to be a QWidget not a QLayout.

            Also your QFormLayout is not anywhere descended from QScrollArea, it's a sibling, so it's not inside it, so it won't be subject to scrollability.

            S 1 Reply Last reply 29 Oct 2021, 07:12
            0
            • J JonB
              29 Oct 2021, 07:10

              @SPlatten

              ----- QWidget( pobjFormErrorsContr )
              -------- QScrollArea( fixed height )
              -------- QFormLayout( mpfrmloErrors  )
              

              But I/we already said that QScrollArea takes a setWidget(someWidget) for the thing it wants to scroll, which has to be a QWidget not a QLayout.

              Also your QFormLayout is not anywhere descended from QScrollArea, it's a sibling, so it's not inside it, so it won't be subject to scrollability.

              S Offline
              S Offline
              SPlatten
              wrote on 29 Oct 2021, 07:12 last edited by
              #20

              @JonB , see code above:

              mpsaErrors = new QScrollArea;
              mpsaErrors->setWidget(pobjFormErrorsContr);
              

              Kind Regards,
              Sy

              J 1 Reply Last reply 29 Oct 2021, 07:13
              0
              • S SPlatten
                29 Oct 2021, 07:12

                @JonB , see code above:

                mpsaErrors = new QScrollArea;
                mpsaErrors->setWidget(pobjFormErrorsContr);
                
                J Online
                J Online
                JonB
                wrote on 29 Oct 2021, 07:13 last edited by JonB
                #21

                @SPlatten
                I see your code, my two comments apply to your "diagram", and comparing it to @SGaist's "diagram".

                S 1 Reply Last reply 29 Oct 2021, 07:14
                0
                • J JonB
                  29 Oct 2021, 07:13

                  @SPlatten
                  I see your code, my two comments apply to your "diagram", and comparing it to @SGaist's "diagram".

                  S Offline
                  S Offline
                  SPlatten
                  wrote on 29 Oct 2021, 07:14 last edited by SPlatten
                  #22

                  @JonB , what diagram are you referring to? the fixed height is just a setting, the parent of the QScrollArea is the widget its under.

                  Kind Regards,
                  Sy

                  J 1 Reply Last reply 29 Oct 2021, 07:15
                  0
                  • S SPlatten
                    29 Oct 2021, 07:14

                    @JonB , what diagram are you referring to? the fixed height is just a setting, the parent of the QScrollArea is the widget its under.

                    J Online
                    J Online
                    JonB
                    wrote on 29 Oct 2021, 07:15 last edited by
                    #23

                    @SPlatten said in QGroupBox, QScrollArea, QFormLayout:

                    what diagram are you referring to?

                    The 3 lines I quoted verbatim from your previous post. Whatever.

                    S 1 Reply Last reply 29 Oct 2021, 07:16
                    0
                    • J JonB
                      29 Oct 2021, 07:15

                      @SPlatten said in QGroupBox, QScrollArea, QFormLayout:

                      what diagram are you referring to?

                      The 3 lines I quoted verbatim from your previous post. Whatever.

                      S Offline
                      S Offline
                      SPlatten
                      wrote on 29 Oct 2021, 07:16 last edited by
                      #24

                      @JonB , yes and...haven't I explained what it means ?

                      Kind Regards,
                      Sy

                      J 1 Reply Last reply 29 Oct 2021, 07:19
                      0
                      • S SPlatten
                        29 Oct 2021, 07:16

                        @JonB , yes and...haven't I explained what it means ?

                        J Online
                        J Online
                        JonB
                        wrote on 29 Oct 2021, 07:19 last edited by JonB
                        #25

                        @SPlatten
                        In @SGaist's "diagram" QFormLayout is descended (via a QWidget) from QScrollArea. In yours they are siblings, and then you say "Where only the QFormLayout is scrollable, but it isn't." If you think your diagram is clear and what you mean that's up to you.

                        S 1 Reply Last reply 29 Oct 2021, 07:37
                        0
                        • J JonB
                          29 Oct 2021, 07:19

                          @SPlatten
                          In @SGaist's "diagram" QFormLayout is descended (via a QWidget) from QScrollArea. In yours they are siblings, and then you say "Where only the QFormLayout is scrollable, but it isn't." If you think your diagram is clear and what you mean that's up to you.

                          S Offline
                          S Offline
                          SPlatten
                          wrote on 29 Oct 2021, 07:37 last edited by SPlatten
                          #26

                          @JonB

                          //Create container for errors panel
                                  mpobjErrors = new QWidget(this);
                                  mpvbloErrors = new QVBoxLayout;
                                  mpobjErrors->setLayout(mpvbloErrors);
                                  mpsaErrors = new QScrollArea;
                                  int intFixedHeight(fontMetrics().height() * MainWindow::mscuintErrorRows);
                                  mpsaErrors->setFixedHeight(intFixedHeight);
                              //Create a widget that contains the scrollable errors form
                                  QWidget* pobjFormErrorsContr(new QWidget);
                                  mpsaErrors->setWidget(pobjFormErrorsContr);
                                  mpfrmloErrors = new QFormLayout;
                                  pobjFormErrorsContr->setLayout(mpfrmloErrors);
                                  mpvbloErrors->addWidget(pobjFormErrorsContr, 1, Qt::AlignHCenter);
                              //Create a push button for acknowledging and clearing errors panel
                                  QPushButton* pbtnAck(new QPushButton(tr("&Clear Errors")));
                                  mpvbloErrors->addWidget(pbtnAck, 1, Qt::AlignHCenter);
                                  QObject::connect(pbtnAck, &QPushButton::clicked,
                                      [this, pbtnAck]
                                      {
                                          mpobjErrors->setVisible(false);
                                      }
                                  );
                              //Default to hidden until errors are available to display
                                  mpobjErrors->setVisible(false);
                          

                          From the above the structure should be:

                          QWidget
                          -- QVBoxLayout
                          ----- QScrollArea, with fixed height to show 3 rows
                          ------- QWidget
                          ---------- QFormLayout
                          ------ QPushButton

                          The image of this output:
                          988009f8-981b-407c-bb8e-2b71fe94befd-image.png
                          The form layout needs to be the full width of the QVBoxLayout and should be just 3 rows as the image shows it isn't.

                          Kind Regards,
                          Sy

                          J 1 Reply Last reply 29 Oct 2021, 09:12
                          0
                          • S SPlatten
                            29 Oct 2021, 07:37

                            @JonB

                            //Create container for errors panel
                                    mpobjErrors = new QWidget(this);
                                    mpvbloErrors = new QVBoxLayout;
                                    mpobjErrors->setLayout(mpvbloErrors);
                                    mpsaErrors = new QScrollArea;
                                    int intFixedHeight(fontMetrics().height() * MainWindow::mscuintErrorRows);
                                    mpsaErrors->setFixedHeight(intFixedHeight);
                                //Create a widget that contains the scrollable errors form
                                    QWidget* pobjFormErrorsContr(new QWidget);
                                    mpsaErrors->setWidget(pobjFormErrorsContr);
                                    mpfrmloErrors = new QFormLayout;
                                    pobjFormErrorsContr->setLayout(mpfrmloErrors);
                                    mpvbloErrors->addWidget(pobjFormErrorsContr, 1, Qt::AlignHCenter);
                                //Create a push button for acknowledging and clearing errors panel
                                    QPushButton* pbtnAck(new QPushButton(tr("&Clear Errors")));
                                    mpvbloErrors->addWidget(pbtnAck, 1, Qt::AlignHCenter);
                                    QObject::connect(pbtnAck, &QPushButton::clicked,
                                        [this, pbtnAck]
                                        {
                                            mpobjErrors->setVisible(false);
                                        }
                                    );
                                //Default to hidden until errors are available to display
                                    mpobjErrors->setVisible(false);
                            

                            From the above the structure should be:

                            QWidget
                            -- QVBoxLayout
                            ----- QScrollArea, with fixed height to show 3 rows
                            ------- QWidget
                            ---------- QFormLayout
                            ------ QPushButton

                            The image of this output:
                            988009f8-981b-407c-bb8e-2b71fe94befd-image.png
                            The form layout needs to be the full width of the QVBoxLayout and should be just 3 rows as the image shows it isn't.

                            J Online
                            J Online
                            JonB
                            wrote on 29 Oct 2021, 09:12 last edited by
                            #27

                            @SPlatten
                            Your "diagram" now looks like @SGaist's, so I am happy.

                            I have played with scroll areas all morning for you, and I have had enough! Play with this:

                            #include "widget.h"
                            
                            #include <QApplication>
                            #include <QScrollArea>
                            #include <QBoxLayout>
                            #include <QPushButton>
                            
                            int main(int argc, char *argv[])
                            {
                                QApplication a(argc, argv);
                                Widget w;
                            
                                QVBoxLayout layout0;
                                w.setLayout(&layout0);
                            
                                QScrollArea scrollArea(&w);
                                layout0.addWidget(&scrollArea);
                                QWidget container(&scrollArea);
                                scrollArea.setWidget(&container);
                                QVBoxLayout layout;
                                container.setLayout(&layout);
                                for (int i = 0; i < 10; i++)
                                    layout.addWidget(new QPushButton);
                            
                                scrollArea.setVerticalScrollBarPolicy(Qt::ScrollBarPolicy::ScrollBarAsNeeded);
                                scrollArea.setFixedHeight(100);
                                scrollArea.setWidgetResizable(true);
                            
                                w.show();
                                return a.exec();
                            }
                            

                            Note the scrollArea.setWidgetResizable(true);. Without that I don't actually see any buttons. Is that what you are missing?

                            Don't ask me about Qt layouts/size policies etc., I have always found them about as intuitive as a duck-billed platypus, I play till I get what I want.

                            S 1 Reply Last reply 29 Oct 2021, 09:22
                            0
                            • J JonB
                              29 Oct 2021, 09:12

                              @SPlatten
                              Your "diagram" now looks like @SGaist's, so I am happy.

                              I have played with scroll areas all morning for you, and I have had enough! Play with this:

                              #include "widget.h"
                              
                              #include <QApplication>
                              #include <QScrollArea>
                              #include <QBoxLayout>
                              #include <QPushButton>
                              
                              int main(int argc, char *argv[])
                              {
                                  QApplication a(argc, argv);
                                  Widget w;
                              
                                  QVBoxLayout layout0;
                                  w.setLayout(&layout0);
                              
                                  QScrollArea scrollArea(&w);
                                  layout0.addWidget(&scrollArea);
                                  QWidget container(&scrollArea);
                                  scrollArea.setWidget(&container);
                                  QVBoxLayout layout;
                                  container.setLayout(&layout);
                                  for (int i = 0; i < 10; i++)
                                      layout.addWidget(new QPushButton);
                              
                                  scrollArea.setVerticalScrollBarPolicy(Qt::ScrollBarPolicy::ScrollBarAsNeeded);
                                  scrollArea.setFixedHeight(100);
                                  scrollArea.setWidgetResizable(true);
                              
                                  w.show();
                                  return a.exec();
                              }
                              

                              Note the scrollArea.setWidgetResizable(true);. Without that I don't actually see any buttons. Is that what you are missing?

                              Don't ask me about Qt layouts/size policies etc., I have always found them about as intuitive as a duck-billed platypus, I play till I get what I want.

                              S Offline
                              S Offline
                              SPlatten
                              wrote on 29 Oct 2021, 09:22 last edited by SPlatten
                              #28

                              @JonB , thank you, however its missing the QFormLayout which is required, see my post before you post.

                              Edited code, still doesn't work.

                                  //Create container for errors panel
                                      mpobjErrors = new QWidget(this);
                                      mpvbloErrors = new QVBoxLayout;
                                      mpobjErrors->setLayout(mpvbloErrors);
                                  //Create scroll area
                                      mpsaErrors = new QScrollArea(mpobjErrors);
                                  //Create a widget that contains the scrollable errors form
                                      QWidget* pobjFormErrorsContr(new QWidget(mpsaErrors));
                                  //Set-up scroll area
                                      mpsaErrors->setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded);
                                      int intFixedHeight(fontMetrics().height() * MainWindow::mscuintErrorRows);
                                      mpsaErrors->setWidget(pobjFormErrorsContr);
                                      mpsaErrors->setFixedHeight(intFixedHeight);
                                      mpsaErrors->setWidgetResizable(true);
                                      mpfrmloErrors = new QFormLayout(pobjFormErrorsContr);
                                      mpvbloErrors->addWidget(pobjFormErrorsContr, 1, Qt::AlignHCenter);//addLayout(mpfrmloErrors, 1);//
                                  //Create a push button for acknowledging and clearing errors panel
                                      QPushButton* pbtnAck(new QPushButton(tr("&Clear Errors")));
                                      mpvbloErrors->addWidget(pbtnAck, 1, Qt::AlignHCenter);
                                      QObject::connect(pbtnAck, &QPushButton::clicked,
                                          [this, pbtnAck]
                                          {
                                              mpobjErrors->setVisible(false);
                                          }
                                      );
                              

                              Kind Regards,
                              Sy

                              J 1 Reply Last reply 29 Oct 2021, 11:04
                              0
                              • S SPlatten
                                29 Oct 2021, 09:22

                                @JonB , thank you, however its missing the QFormLayout which is required, see my post before you post.

                                Edited code, still doesn't work.

                                    //Create container for errors panel
                                        mpobjErrors = new QWidget(this);
                                        mpvbloErrors = new QVBoxLayout;
                                        mpobjErrors->setLayout(mpvbloErrors);
                                    //Create scroll area
                                        mpsaErrors = new QScrollArea(mpobjErrors);
                                    //Create a widget that contains the scrollable errors form
                                        QWidget* pobjFormErrorsContr(new QWidget(mpsaErrors));
                                    //Set-up scroll area
                                        mpsaErrors->setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded);
                                        int intFixedHeight(fontMetrics().height() * MainWindow::mscuintErrorRows);
                                        mpsaErrors->setWidget(pobjFormErrorsContr);
                                        mpsaErrors->setFixedHeight(intFixedHeight);
                                        mpsaErrors->setWidgetResizable(true);
                                        mpfrmloErrors = new QFormLayout(pobjFormErrorsContr);
                                        mpvbloErrors->addWidget(pobjFormErrorsContr, 1, Qt::AlignHCenter);//addLayout(mpfrmloErrors, 1);//
                                    //Create a push button for acknowledging and clearing errors panel
                                        QPushButton* pbtnAck(new QPushButton(tr("&Clear Errors")));
                                        mpvbloErrors->addWidget(pbtnAck, 1, Qt::AlignHCenter);
                                        QObject::connect(pbtnAck, &QPushButton::clicked,
                                            [this, pbtnAck]
                                            {
                                                mpobjErrors->setVisible(false);
                                            }
                                        );
                                
                                J Online
                                J Online
                                JonB
                                wrote on 29 Oct 2021, 11:04 last edited by
                                #29

                                @SPlatten
                                I found I had to put in scrollArea.setWidgetResizable(true); to make it respect the height. You are saying it still does not. I assume you have verified the value for fontMetrics().height() * MainWindow::mscuintErrorRows. You can put a QFormLayout into my sample code in place of QVBoxLayout layout;, I can't see why that would behave any differently vertically.

                                S 1 Reply Last reply 29 Oct 2021, 11:09
                                0
                                • J JonB
                                  29 Oct 2021, 11:04

                                  @SPlatten
                                  I found I had to put in scrollArea.setWidgetResizable(true); to make it respect the height. You are saying it still does not. I assume you have verified the value for fontMetrics().height() * MainWindow::mscuintErrorRows. You can put a QFormLayout into my sample code in place of QVBoxLayout layout;, I can't see why that would behave any differently vertically.

                                  S Offline
                                  S Offline
                                  SPlatten
                                  wrote on 29 Oct 2021, 11:09 last edited by SPlatten
                                  #30

                                  @JonB , just to verify:

                                  int intFontHeight(fontMetrics().height()),
                                      intFixedHeight(intFontHeight * MainWindow::mscuintErrorRows);
                                  

                                  intFontHeight is 13
                                  MainWindow::mscuintErrorRows is 3
                                  intFixedHeight is 39

                                  Clearly setFixedHeight on mpsaErrors is not having the desired effect.

                                  Kind Regards,
                                  Sy

                                  1 Reply Last reply
                                  0
                                  • M Offline
                                    M Offline
                                    mpergand
                                    wrote on 29 Oct 2021, 13:45 last edited by mpergand
                                    #31

                                    Assuming margins at set to zero:

                                    pFormLayout=new QFormLayout;
                                    pFormLayout->setContentsMargins(0,0,0,0);
                                    pFormLayout->setSpacing(0);
                                    pFormLayout->setHorizontalSpacing(10);
                                    

                                    In the show event you can calculate 3 rows height like that:

                                    void Widget::showEvent(QShowEvent* event)
                                    {
                                        QWidget::showEvent(event);
                                        QLayoutItem * item=pFormLayout-> itemAt(0);
                                        int h=item->geometry().height();
                                    
                                        pScrollArea->setFixedHeight(h*3+2);
                                    
                                    }
                                    

                                    S 1 Reply Last reply 29 Oct 2021, 15:12
                                    2
                                    • M mpergand
                                      29 Oct 2021, 13:45

                                      Assuming margins at set to zero:

                                      pFormLayout=new QFormLayout;
                                      pFormLayout->setContentsMargins(0,0,0,0);
                                      pFormLayout->setSpacing(0);
                                      pFormLayout->setHorizontalSpacing(10);
                                      

                                      In the show event you can calculate 3 rows height like that:

                                      void Widget::showEvent(QShowEvent* event)
                                      {
                                          QWidget::showEvent(event);
                                          QLayoutItem * item=pFormLayout-> itemAt(0);
                                          int h=item->geometry().height();
                                      
                                          pScrollArea->setFixedHeight(h*3+2);
                                      
                                      }
                                      

                                      S Offline
                                      S Offline
                                      SPlatten
                                      wrote on 29 Oct 2021, 15:12 last edited by
                                      #32

                                      @mpergand , thank you

                                      Kind Regards,
                                      Sy

                                      M 1 Reply Last reply 29 Oct 2021, 15:22
                                      0
                                      • S SPlatten
                                        29 Oct 2021, 15:12

                                        @mpergand , thank you

                                        M Offline
                                        M Offline
                                        mpergand
                                        wrote on 29 Oct 2021, 15:22 last edited by mpergand
                                        #33

                                        @SPlatten
                                        I assumed that the formLayout is not empty, unless the app will crash.

                                        If you create one row in the constructor and want an empty form on show up, you can do:

                                        QWidget::showEvent(event);
                                        QLayoutItem* item=pFormLayout-> itemAt(0);
                                        int h=item->geometry().height();
                                        pScrollArea->setFixedHeight(h3+3);
                                        pFormLayout->removeRow(0);

                                        [WARNING]
                                        Multi showEvent can occur, you need to add some flag to prevent that:

                                        void Widget::showEvent(QShowEvent* event)
                                        {
                                            static bool firstShow=true;
                                        
                                            QWidget::showEvent(event);
                                             
                                            if(firstShow)
                                                {
                                                QLayoutItem * item=pFormLayout-> itemAt(0);
                                                int h=item->geometry().height();
                                                pScrollArea->setFixedHeight(h*3+3);
                                                pFormLayout->removeRow(0);
                                                firstShow=false;
                                                }
                                        }
                                        
                                        S 2 Replies Last reply 29 Oct 2021, 15:33
                                        0
                                        • M mpergand
                                          29 Oct 2021, 15:22

                                          @SPlatten
                                          I assumed that the formLayout is not empty, unless the app will crash.

                                          If you create one row in the constructor and want an empty form on show up, you can do:

                                          QWidget::showEvent(event);
                                          QLayoutItem* item=pFormLayout-> itemAt(0);
                                          int h=item->geometry().height();
                                          pScrollArea->setFixedHeight(h3+3);
                                          pFormLayout->removeRow(0);

                                          [WARNING]
                                          Multi showEvent can occur, you need to add some flag to prevent that:

                                          void Widget::showEvent(QShowEvent* event)
                                          {
                                              static bool firstShow=true;
                                          
                                              QWidget::showEvent(event);
                                               
                                              if(firstShow)
                                                  {
                                                  QLayoutItem * item=pFormLayout-> itemAt(0);
                                                  int h=item->geometry().height();
                                                  pScrollArea->setFixedHeight(h*3+3);
                                                  pFormLayout->removeRow(0);
                                                  firstShow=false;
                                                  }
                                          }
                                          
                                          S Offline
                                          S Offline
                                          SPlatten
                                          wrote on 29 Oct 2021, 15:33 last edited by
                                          #34

                                          @mpergand , this layout will only be displayed if there is content to display.

                                          Kind Regards,
                                          Sy

                                          JoeCFDJ 1 Reply Last reply 29 Oct 2021, 16:31
                                          0

                                          24/41

                                          29 Oct 2021, 07:16

                                          • Login

                                          • Login or register to search.
                                          24 out of 41
                                          • First post
                                            24/41
                                            Last post
                                          0
                                          • Categories
                                          • Recent
                                          • Tags
                                          • Popular
                                          • Users
                                          • Groups
                                          • Search
                                          • Get Qt Extensions
                                          • Unsolved