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. Dynamically create multiple layouts

Dynamically create multiple layouts

Scheduled Pinned Locked Moved Unsolved General and Desktop
20 Posts 5 Posters 1.7k Views 3 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 SamuelAdesola

    @mpergand Yes
    The same set of widgets repeated in a certain number of times based on the user's input

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

    @SamuelAdesola
    One possibility:

    const int LayoutCount=7;
    struct LayoutWidgets
    {
            QHBoxLayout* layout; 
            QLabel*     CourseCodeLabel;
            QLineEdit*  CourseCodeLineEdit;
            QLabel*     CourseGradeLabel;
            QComboBox*  CourseGradeComboBox;
    };
    
    LayoutWidgets layoutArray[LayoutCount];
    
    ...
    // populate layoutArray
    for(int i=0; i<LayoutCount; i++)
        {
        QHBoxLayout* layout=this->findChild<QHBoxLayout*>(QString("horizontalLayout_%1").arg(i+1));
        if(layout==nullptr)
            {
            // not found
            continue;
            }
        layoutArray[i].layout=layout;
        layoutArray[i].CourseCodeLabel=new QLabel;
        layoutArray[i].CourseCodeLineEdit=new QLineEdit;
        layoutArray[i].CourseGradeLabel=new QLabel;
        layoutArray[i].CourseGradeComboBox=new QComboBox;
        
        // populate QLayout
        layout->addWidget(layoutArray[i].CourseCodeLabel);
        layout->addWidget(layoutArray[i].CourseCodeLineEdit);
        layout->addWidget(layoutArray[i].CourseGradeLabel);
        layout->addWidget(layoutArray[i].CourseGradeComboBox);
        }
    

    Of course not tested !

    S 2 Replies Last reply
    0
    • M mpergand

      @SamuelAdesola
      One possibility:

      const int LayoutCount=7;
      struct LayoutWidgets
      {
              QHBoxLayout* layout; 
              QLabel*     CourseCodeLabel;
              QLineEdit*  CourseCodeLineEdit;
              QLabel*     CourseGradeLabel;
              QComboBox*  CourseGradeComboBox;
      };
      
      LayoutWidgets layoutArray[LayoutCount];
      
      ...
      // populate layoutArray
      for(int i=0; i<LayoutCount; i++)
          {
          QHBoxLayout* layout=this->findChild<QHBoxLayout*>(QString("horizontalLayout_%1").arg(i+1));
          if(layout==nullptr)
              {
              // not found
              continue;
              }
          layoutArray[i].layout=layout;
          layoutArray[i].CourseCodeLabel=new QLabel;
          layoutArray[i].CourseCodeLineEdit=new QLineEdit;
          layoutArray[i].CourseGradeLabel=new QLabel;
          layoutArray[i].CourseGradeComboBox=new QComboBox;
          
          // populate QLayout
          layout->addWidget(layoutArray[i].CourseCodeLabel);
          layout->addWidget(layoutArray[i].CourseCodeLineEdit);
          layout->addWidget(layoutArray[i].CourseGradeLabel);
          layout->addWidget(layoutArray[i].CourseGradeComboBox);
          }
      

      Of course not tested !

      S Offline
      S Offline
      SamuelAdesola
      wrote on last edited by
      #7

      @mpergand
      Thanks
      I will implement this and get back to you.

      M 1 Reply Last reply
      0
      • S SamuelAdesola

        @mpergand
        Thanks
        I will implement this and get back to you.

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

        @SamuelAdesola

        repeated in a certain number of times based on the user's input

        the number of layouts is not fixed ?

        S 1 Reply Last reply
        0
        • S Offline
          S Offline
          SamuelAdesola
          wrote on last edited by
          #9

          @mpergand said in Dynamically create multiple layouts:

          const int LayoutCount=7;
          struct LayoutWidgets
          {
          QHBoxLayout* layout;
          QLabel* CourseCodeLabel;
          QLineEdit* CourseCodeLineEdit;
          QLabel* CourseGradeLabel;
          QComboBox* CourseGradeComboBox;
          };

          LayoutWidgets layoutArray[LayoutCount];

          ...
          // populate layoutArray
          for(int i=0; i<LayoutCount; i++)
          {
          QHBoxLayout* layout=this->findChild<QHBoxLayout*>(QString("horizontalLayout_%1").arg(i+1));
          if(layout==nullptr)
          {
          // not found
          continue;
          }
          layoutArray[i].layout=layout;
          layoutArray[i].CourseCodeLabel=new QLabel;
          layoutArray[i].CourseCodeLineEdit=new QLineEdit;
          layoutArray[i].CourseGradeLabel=new QLabel;
          layoutArray[i].CourseGradeComboBox=new QComboBox;

          // populate QLayout
          layout->addWidget(layoutArray[i].CourseCodeLabel);
          layout->addWidget(layoutArray[i].CourseCodeLineEdit);
          layout->addWidget(layoutArray[i].CourseGradeLabel);
          layout->addWidget(layoutArray[i].CourseGradeComboBox);
          }
          

          It worked Perfectly. Thanks very much

          1 Reply Last reply
          0
          • M mpergand

            @SamuelAdesola

            repeated in a certain number of times based on the user's input

            the number of layouts is not fixed ?

            S Offline
            S Offline
            SamuelAdesola
            wrote on last edited by
            #10

            @mpergand Max is 15, I created the 15 spaces in the from ui. The number created on at runtime depends on the user input

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

              Hi,

              Since you have a dynamic number of objects, you should use a QVector to hold the exact number of them.

              That will simplify your code and memory handling.

              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
              • S SamuelAdesola

                @mpergand Max is 15, I created the 15 spaces in the from ui. The number created on at runtime depends on the user input

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

                @SamuelAdesola
                As @SGaist said use QVector:

                LayoutWidgets layoutArray[LayoutCount];
                QVector<LayoutWidgets> layoutArray;

                then first create a LayoutWidgets instance in the loop:
                for(int i=0; i<LayoutCount; i++)
                {
                layoutArray<<LayoutWidgets;
                ....

                Max is 15, I created the 15 spaces in the from ui

                Why not create this layouts in code as well?

                1 Reply Last reply
                0
                • M mpergand

                  @SamuelAdesola
                  One possibility:

                  const int LayoutCount=7;
                  struct LayoutWidgets
                  {
                          QHBoxLayout* layout; 
                          QLabel*     CourseCodeLabel;
                          QLineEdit*  CourseCodeLineEdit;
                          QLabel*     CourseGradeLabel;
                          QComboBox*  CourseGradeComboBox;
                  };
                  
                  LayoutWidgets layoutArray[LayoutCount];
                  
                  ...
                  // populate layoutArray
                  for(int i=0; i<LayoutCount; i++)
                      {
                      QHBoxLayout* layout=this->findChild<QHBoxLayout*>(QString("horizontalLayout_%1").arg(i+1));
                      if(layout==nullptr)
                          {
                          // not found
                          continue;
                          }
                      layoutArray[i].layout=layout;
                      layoutArray[i].CourseCodeLabel=new QLabel;
                      layoutArray[i].CourseCodeLineEdit=new QLineEdit;
                      layoutArray[i].CourseGradeLabel=new QLabel;
                      layoutArray[i].CourseGradeComboBox=new QComboBox;
                      
                      // populate QLayout
                      layout->addWidget(layoutArray[i].CourseCodeLabel);
                      layout->addWidget(layoutArray[i].CourseCodeLineEdit);
                      layout->addWidget(layoutArray[i].CourseGradeLabel);
                      layout->addWidget(layoutArray[i].CourseGradeComboBox);
                      }
                  

                  Of course not tested !

                  S Offline
                  S Offline
                  SamuelAdesola
                  wrote on last edited by
                  #13

                  @mpergand
                  I successfully implemented this but came across a problem along the way, i have tried solving it but i am still not getting it.

                  After dynamically creating the objects using QVector, I wanted to access the element of the object using another funtion when a button is clicked but the program keeps crashing.

                  How can i access these elements in another function?

                  I want to be able to do something like

                  qDebug() << layoutArray[1].CourseCodeLabel->text();
                  
                  M JonBJ 2 Replies Last reply
                  0
                  • S SamuelAdesola

                    @mpergand
                    I successfully implemented this but came across a problem along the way, i have tried solving it but i am still not getting it.

                    After dynamically creating the objects using QVector, I wanted to access the element of the object using another funtion when a button is clicked but the program keeps crashing.

                    How can i access these elements in another function?

                    I want to be able to do something like

                    qDebug() << layoutArray[1].CourseCodeLabel->text();
                    
                    M Offline
                    M Offline
                    mpergand
                    wrote on last edited by
                    #14

                    @SamuelAdesola
                    Use the debugger and show us the stack trace.

                    1 Reply Last reply
                    0
                    • J Offline
                      J Offline
                      jonezzpeterr
                      wrote on last edited by
                      #15

                      I will implement this and get back to you.

                      https://get-vidmateapp.com/vidmate-2014-download-dl/
                      https://get-mobdroapk.com

                      S 1 Reply Last reply
                      0
                      • S SamuelAdesola

                        @mpergand
                        I successfully implemented this but came across a problem along the way, i have tried solving it but i am still not getting it.

                        After dynamically creating the objects using QVector, I wanted to access the element of the object using another funtion when a button is clicked but the program keeps crashing.

                        How can i access these elements in another function?

                        I want to be able to do something like

                        qDebug() << layoutArray[1].CourseCodeLabel->text();
                        
                        JonBJ Offline
                        JonBJ Offline
                        JonB
                        wrote on last edited by
                        #16

                        @SamuelAdesola
                        If you have code like layoutArray[1].CourseCodeLabel->text() which crashes you will doubtless find that layoutArray[1] does not exist or layoutArray[1]->CourseCodeLabel is null/not valid. Which a stack trace should indeed reveal.

                        S 1 Reply Last reply
                        0
                        • J jonezzpeterr

                          I will implement this and get back to you.

                          S Offline
                          S Offline
                          SamuelAdesola
                          wrote on last edited by
                          #17

                          @jonezzpeterr
                          Ok, I'll be looking forward to it

                          1 Reply Last reply
                          0
                          • JonBJ JonB

                            @SamuelAdesola
                            If you have code like layoutArray[1].CourseCodeLabel->text() which crashes you will doubtless find that layoutArray[1] does not exist or layoutArray[1]->CourseCodeLabel is null/not valid. Which a stack trace should indeed reveal.

                            S Offline
                            S Offline
                            SamuelAdesola
                            wrote on last edited by
                            #18

                            @JonB
                            Can I make layoutArray a global variable so if I assign it in a function, I can access it in another function. One function is to show the user the forms layouts and another is to use the inputed data to do some calculations. I should have put them in same function but the calculation aspect automatically use the empty fields when a button is press. There are 2 buttons, one to show the user the fields and the other to make use of the inputed data for some calculations.

                            JonBJ 1 Reply Last reply
                            0
                            • S SamuelAdesola

                              @JonB
                              Can I make layoutArray a global variable so if I assign it in a function, I can access it in another function. One function is to show the user the forms layouts and another is to use the inputed data to do some calculations. I should have put them in same function but the calculation aspect automatically use the empty fields when a button is press. There are 2 buttons, one to show the user the fields and the other to make use of the inputed data for some calculations.

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

                              @SamuelAdesola
                              Make it a class member variable, rather than global, and you can access it in any class method. The fact that it is array is not relevant. And btw if it's a plain C array better use a QVector, say.

                              S 1 Reply Last reply
                              1
                              • JonBJ JonB

                                @SamuelAdesola
                                Make it a class member variable, rather than global, and you can access it in any class method. The fact that it is array is not relevant. And btw if it's a plain C array better use a QVector, say.

                                S Offline
                                S Offline
                                SamuelAdesola
                                wrote on last edited by
                                #20

                                @JonB @mpergand @jonezzpeterr @SGaist

                                Thanks very much for the support, I think i have achieve quite a lot of those giving tough time.

                                As @SGaist said I am using QVector and as @JonB said i declare it as a class member variable

                                QVector<LayoutWidgets> layoutArray;
                                

                                Because the size will be set at run time base on user input, i resize the vector in a function

                                layoutArray.resize(LayoutCount);
                                
                                

                                and i am able to make calls to my class setters to set the values in layoutArray and use another getter function to access the set values

                                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