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.
  • M Offline
    M Offline
    mpergand
    wrote on 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);
    
    }
    

    SPlattenS 1 Reply Last reply
    2
    • M mpergand

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

      SPlattenS Offline
      SPlattenS Offline
      SPlatten
      wrote on last edited by
      #32

      @mpergand , thank you

      Kind Regards,
      Sy

      M 1 Reply Last reply
      0
      • SPlattenS SPlatten

        @mpergand , thank you

        M Offline
        M Offline
        mpergand
        wrote on 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;
                }
        }
        
        SPlattenS 2 Replies Last reply
        0
        • M mpergand

          @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;
                  }
          }
          
          SPlattenS Offline
          SPlattenS Offline
          SPlatten
          wrote on 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
          0
          • SPlattenS SPlatten

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

            JoeCFDJ Offline
            JoeCFDJ Offline
            JoeCFD
            wrote on last edited by JoeCFD
            #35

            @SPlatten Q
            When you use font size to set item height, it is better to use some reference chars. That is what I do:
            Rect bounding_rect = QFontMetrics( font ).tightBoundingRect( QString( "MWLPGHXYZmwlpghxyz" ) );
            use bounding_rect.height();
            And item height has to be a bit bigger than this height.

            1 Reply Last reply
            1
            • M mpergand

              @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;
                      }
              }
              
              SPlattenS Offline
              SPlattenS Offline
              SPlatten
              wrote on last edited by SPlatten
              #36

              @mpergand , how is the showEvent connected and to which widget? I assume this is the MainWindow instance that this code is relevant to?

              Kind Regards,
              Sy

              M 1 Reply Last reply
              0
              • SPlattenS SPlatten

                @mpergand , how is the showEvent connected and to which widget? I assume this is the MainWindow instance that this code is relevant to?

                M Offline
                M Offline
                mpergand
                wrote on last edited by
                #37

                @SPlatten
                Here Widget is the top level object, but it does not have to be.

                how is the showEvent connected

                It's connected to nothing, it's a virtual method of QWidget.

                SPlattenS 1 Reply Last reply
                0
                • M mpergand

                  @SPlatten
                  Here Widget is the top level object, but it does not have to be.

                  how is the showEvent connected

                  It's connected to nothing, it's a virtual method of QWidget.

                  SPlattenS Offline
                  SPlattenS Offline
                  SPlatten
                  wrote on last edited by
                  #38

                  @mpergand, can you post your full example source, showing the relationship between pScrollArea and pFormLayout ?

                  Thank you,

                  Kind Regards,
                  Sy

                  1 Reply Last reply
                  0
                  • M Offline
                    M Offline
                    mpergand
                    wrote on last edited by mpergand
                    #39

                    Widget.h

                    #include <QWidget>
                    
                    class QScrollArea;
                    class QFormLayout;
                    
                    class Widget : public QWidget
                        {
                            Q_OBJECT
                            
                        public:
                            
                                    explicit Widget(QWidget *parent = nullptr);
                                    
                        private:
                                          void  showEvent(QShowEvent* event);
                                   QScrollArea* pScrollArea;
                                   QFormLayout* pFormLayout;
                    };
                    

                    Widget.cpp

                    Widget::Widget(QWidget *parent) : QWidget(parent)
                    {
                        auto vLayout=new QVBoxLayout;
                        setLayout(vLayout);
                        
                        pScrollArea=new QScrollArea;
                        vLayout->addWidget(pScrollArea);
                        
                        auto sWidget=new QWidget;
                        pScrollArea->setWidget(sWidget);
                        pScrollArea->setWidgetResizable(true);
                       
                        pFormLayout=new QFormLayout;
                        pFormLayout->setContentsMargins(0,0,0,1);
                        pFormLayout->setSpacing(0);
                        pFormLayout->setHorizontalSpacing(10);
                        sWidget->setLayout(pFormLayout);
                    
                        // add one row for calculation in showEvent
                        pFormLayout->addRow("", new QLabel());  
                    
                        vLayout->addWidget(new QPushButton("Clear"));
                    }
                    
                    SPlattenS 2 Replies Last reply
                    0
                    • M mpergand

                      Widget.h

                      #include <QWidget>
                      
                      class QScrollArea;
                      class QFormLayout;
                      
                      class Widget : public QWidget
                          {
                              Q_OBJECT
                              
                          public:
                              
                                      explicit Widget(QWidget *parent = nullptr);
                                      
                          private:
                                            void  showEvent(QShowEvent* event);
                                     QScrollArea* pScrollArea;
                                     QFormLayout* pFormLayout;
                      };
                      

                      Widget.cpp

                      Widget::Widget(QWidget *parent) : QWidget(parent)
                      {
                          auto vLayout=new QVBoxLayout;
                          setLayout(vLayout);
                          
                          pScrollArea=new QScrollArea;
                          vLayout->addWidget(pScrollArea);
                          
                          auto sWidget=new QWidget;
                          pScrollArea->setWidget(sWidget);
                          pScrollArea->setWidgetResizable(true);
                         
                          pFormLayout=new QFormLayout;
                          pFormLayout->setContentsMargins(0,0,0,1);
                          pFormLayout->setSpacing(0);
                          pFormLayout->setHorizontalSpacing(10);
                          sWidget->setLayout(pFormLayout);
                      
                          // add one row for calculation in showEvent
                          pFormLayout->addRow("", new QLabel());  
                      
                          vLayout->addWidget(new QPushButton("Clear"));
                      }
                      
                      SPlattenS Offline
                      SPlattenS Offline
                      SPlatten
                      wrote on last edited by
                      #40

                      @mpergand , thank you, I'm creating a simple sample application, if I get it working in that then I can see what I need to do in my other application.

                      Kind Regards,
                      Sy

                      1 Reply Last reply
                      0
                      • M mpergand

                        Widget.h

                        #include <QWidget>
                        
                        class QScrollArea;
                        class QFormLayout;
                        
                        class Widget : public QWidget
                            {
                                Q_OBJECT
                                
                            public:
                                
                                        explicit Widget(QWidget *parent = nullptr);
                                        
                            private:
                                              void  showEvent(QShowEvent* event);
                                       QScrollArea* pScrollArea;
                                       QFormLayout* pFormLayout;
                        };
                        

                        Widget.cpp

                        Widget::Widget(QWidget *parent) : QWidget(parent)
                        {
                            auto vLayout=new QVBoxLayout;
                            setLayout(vLayout);
                            
                            pScrollArea=new QScrollArea;
                            vLayout->addWidget(pScrollArea);
                            
                            auto sWidget=new QWidget;
                            pScrollArea->setWidget(sWidget);
                            pScrollArea->setWidgetResizable(true);
                           
                            pFormLayout=new QFormLayout;
                            pFormLayout->setContentsMargins(0,0,0,1);
                            pFormLayout->setSpacing(0);
                            pFormLayout->setHorizontalSpacing(10);
                            sWidget->setLayout(pFormLayout);
                        
                            // add one row for calculation in showEvent
                            pFormLayout->addRow("", new QLabel());  
                        
                            vLayout->addWidget(new QPushButton("Clear"));
                        }
                        
                        SPlattenS Offline
                        SPlattenS Offline
                        SPlatten
                        wrote on last edited by
                        #41

                        @mpergand , Thank you, works a treat...now to port to my main application:

                            ui->setupUi(this);
                        
                            QVBoxLayout* pvbxLayout(new QVBoxLayout);
                            mpsa = new QScrollArea;
                            pvbxLayout->addWidget(mpsa);
                        
                            QWidget* pContainer(new QWidget);
                            mpsa->setWidget(pContainer);
                            int intFixedHeight(fontMetrics().height() * 3);
                            mpsa->setFixedHeight(intFixedHeight);
                            mpsa->setWidgetResizable(true);
                        
                            mpFormLayout = new QFormLayout;
                            mpFormLayout->setContentsMargins(0,0,0,0);
                            mpFormLayout->setSpacing(0);
                            mpFormLayout->setHorizontalSpacing(10);
                            pContainer->setLayout(mpFormLayout);
                        
                            for( int i=1; i<=10; i++ ) {
                                mpFormLayout->addRow(QString::number(i),
                                                     new QLabel(QString("Hello World: %1").arg(i)));
                            }
                            pvbxLayout->addWidget(new QPushButton("HELLO"));
                            ui->centralwidget->setLayout(pvbxLayout);
                        

                        Kind Regards,
                        Sy

                        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