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

Dynamically create multiple layouts

Scheduled Pinned Locked Moved Unsolved General and Desktop
20 Posts 5 Posters 1.3k 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 Offline
    S Offline
    SamuelAdesola
    wrote on 18 Feb 2023, 04:00 last edited by
    #1

    I am setting dynamically the contents of multiple layouts. I have 15 horizontal layout set out in my ui file, i want to set their values dynamically in code but i am only able to set one layout per time.
    The code is below

        ui->horizontalLayout_6->addWidget(CourseCodeLabel);
        ui->horizontalLayout_6->addWidget(CourseCodeLineEdit);
        ui->horizontalLayout_6->addWidget(CourseGradeLabel);
        ui->horizontalLayout_6->addWidget(CourseGradeComboBox);
    
        ui->horizontalLayout_7->addWidget(CourseCodeLabel);
        ui->horizontalLayout_7->addWidget(CourseCodeLineEdit);
        ui->horizontalLayout_7->addWidget(CourseGradeLabel);
        ui->horizontalLayout_7->addWidget(CourseGradeComboBox);
    

    After running this, only the second layout content shows, how can i get the two to show at same time?

    J 1 Reply Last reply 18 Feb 2023, 08:08
    0
    • S SamuelAdesola
      18 Feb 2023, 04:00

      I am setting dynamically the contents of multiple layouts. I have 15 horizontal layout set out in my ui file, i want to set their values dynamically in code but i am only able to set one layout per time.
      The code is below

          ui->horizontalLayout_6->addWidget(CourseCodeLabel);
          ui->horizontalLayout_6->addWidget(CourseCodeLineEdit);
          ui->horizontalLayout_6->addWidget(CourseGradeLabel);
          ui->horizontalLayout_6->addWidget(CourseGradeComboBox);
      
          ui->horizontalLayout_7->addWidget(CourseCodeLabel);
          ui->horizontalLayout_7->addWidget(CourseCodeLineEdit);
          ui->horizontalLayout_7->addWidget(CourseGradeLabel);
          ui->horizontalLayout_7->addWidget(CourseGradeComboBox);
      

      After running this, only the second layout content shows, how can i get the two to show at same time?

      J Offline
      J Offline
      JonB
      wrote on 18 Feb 2023, 08:08 last edited by
      #2

      @SamuelAdesola
      You are trying to add the same widget instances to multiple layouts. Widgets are actual objects, they cannot "be in two places at one time" since they are not quantum objects! Your second set of statements move the widgets from horizontalLayout_6 to horizontalLayout_7.

      You have to create separate, additional widgets if you want to place them on other layouts.

      S 1 Reply Last reply 18 Feb 2023, 17:18
      3
      • J JonB
        18 Feb 2023, 08:08

        @SamuelAdesola
        You are trying to add the same widget instances to multiple layouts. Widgets are actual objects, they cannot "be in two places at one time" since they are not quantum objects! Your second set of statements move the widgets from horizontalLayout_6 to horizontalLayout_7.

        You have to create separate, additional widgets if you want to place them on other layouts.

        S Offline
        S Offline
        SamuelAdesola
        wrote on 18 Feb 2023, 17:18 last edited by
        #3

        @JonB How can i go about creating multiple objects. I tried using vectors of object but not working.

        M 1 Reply Last reply 18 Feb 2023, 17:27
        0
        • S SamuelAdesola
          18 Feb 2023, 17:18

          @JonB How can i go about creating multiple objects. I tried using vectors of object but not working.

          M Offline
          M Offline
          mpergand
          wrote on 18 Feb 2023, 17:27 last edited by
          #4

          @SamuelAdesola
          Do all your layouts contain the same bunch of widgets ?

          S 1 Reply Last reply 18 Feb 2023, 18:00
          0
          • M mpergand
            18 Feb 2023, 17:27

            @SamuelAdesola
            Do all your layouts contain the same bunch of widgets ?

            S Offline
            S Offline
            SamuelAdesola
            wrote on 18 Feb 2023, 18:00 last edited by SamuelAdesola
            #5

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

            M 1 Reply Last reply 18 Feb 2023, 18:26
            0
            • S SamuelAdesola
              18 Feb 2023, 18:00

              @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 18 Feb 2023, 18:26 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 18 Feb 2023, 18:31
              0
              • M mpergand
                18 Feb 2023, 18:26

                @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 18 Feb 2023, 18:31 last edited by
                #7

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

                M 1 Reply Last reply 18 Feb 2023, 18:36
                0
                • S SamuelAdesola
                  18 Feb 2023, 18:31

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

                  M Offline
                  M Offline
                  mpergand
                  wrote on 18 Feb 2023, 18:36 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 18 Feb 2023, 19:57
                  0
                  • S Offline
                    S Offline
                    SamuelAdesola
                    wrote on 18 Feb 2023, 18:45 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
                      18 Feb 2023, 18:36

                      @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 18 Feb 2023, 19:57 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 18 Feb 2023, 20:19
                      0
                      • SGaistS Offline
                        SGaistS Offline
                        SGaist
                        Lifetime Qt Champion
                        wrote on 18 Feb 2023, 20:02 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
                          18 Feb 2023, 19:57

                          @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 18 Feb 2023, 20:19 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
                            18 Feb 2023, 18:26

                            @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 22 Feb 2023, 16:03 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 J 2 Replies Last reply 22 Feb 2023, 16:12
                            0
                            • S SamuelAdesola
                              22 Feb 2023, 16:03

                              @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 22 Feb 2023, 16:12 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 23 Feb 2023, 09:06 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 23 Feb 2023, 12:15
                                0
                                • S SamuelAdesola
                                  22 Feb 2023, 16:03

                                  @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();
                                  
                                  J Offline
                                  J Offline
                                  JonB
                                  wrote on 23 Feb 2023, 10:10 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 23 Feb 2023, 14:00
                                  0
                                  • J jonezzpeterr
                                    23 Feb 2023, 09:06

                                    I will implement this and get back to you.

                                    S Offline
                                    S Offline
                                    SamuelAdesola
                                    wrote on 23 Feb 2023, 12:15 last edited by
                                    #17

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

                                    1 Reply Last reply
                                    0
                                    • J JonB
                                      23 Feb 2023, 10:10

                                      @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 23 Feb 2023, 14:00 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.

                                      J 1 Reply Last reply 23 Feb 2023, 14:10
                                      0
                                      • S SamuelAdesola
                                        23 Feb 2023, 14:00

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

                                        J Offline
                                        J Offline
                                        JonB
                                        wrote on 23 Feb 2023, 14:10 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 23 Feb 2023, 20:49
                                        1
                                        • J JonB
                                          23 Feb 2023, 14:10

                                          @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 23 Feb 2023, 20:49 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

                                          1/20

                                          18 Feb 2023, 04:00

                                          • Login

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