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. Add/Delete Multiple Widgets to QGridLayout Programmatically
Forum Updated to NodeBB v4.3 + New Features

Add/Delete Multiple Widgets to QGridLayout Programmatically

Scheduled Pinned Locked Moved Unsolved General and Desktop
13 Posts 4 Posters 1.0k Views
  • 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.
  • R Offline
    R Offline
    Radio1985
    wrote on last edited by
    #1

    Hi All,

    I want to add multiple Widgets programmatically to a QGridLayout. I have already created the following layout using QT Creator.

    There is one Horizontal Layout Frame and another Frame below it without specifying the layout type.

    79eca0ab-07f2-45e5-a7e6-54bb03146c53-image.png

    0bc980e5-6c6c-46d2-8768-c1f48000753c-image.png

    I have a button call Add, when I click it, it is supposed to add some labels and combo box, and Line Edits horizontally. When I click the button again, it is supposed to add another row below, similar to the previous row.

    The on button click code does the following:

    QGridLayout* specGridLayout = qobject_cast<QGridLayout*>(ui->specFrameLayout->layout());
       if(!specGridLayout){
           qDebug() << "Failed to qobject_cast.";
           specGridLayout = new QGridLayout(ui->specFrameLayout);
       }
    
    // Create LineEdit, ComboBox, and two LineEdits 
           QLineEdit* specNameLine = new QLineEdit(this);
           specNameLine->setFixedWidth(ui->specNameL->width()+20);
           specNameLine->setAlignment(Qt::AlignCenter);
           // Apply a style sheet to hide the line edit border and leave only the bottom edge
           specNameLine->setStyleSheet("border: none; border-bottom: 1px solid black;");
    
           // Connect the combo box 
           QLabel *specTypeLine =  new QLabel("", ui->specFrameLayout);
           specTypeLine->setText(ui->distributionNum->currentText());
           specTypeLine->setFixedWidth(ui->specTypeL->width());
           specTypeLine->setAlignment(Qt::AlignCenter);
    
           QComboBox* rangeTypeCombo = new QComboBox(this);
           rangeTypeCombo->setFixedWidth(ui->rangeTypeL->width()+10);
    
    // Add items to the ComboBox
           rangeTypeCombo->addItem("Select Range Type");
           rangeTypeCombo->addItem("Min");
           rangeTypeCombo->addItem("Max");
           rangeTypeCombo->addItem("Range");
    

    To add the widgets, I am using addWidget overload function.

    specGridLayout->addWidget(specNameLine, 0, 0, 0, 0, Qt::AlignLeft);
    specGridLayout->addWidget(specTypeLine, 0, 1, 0, 5, Qt::AlignLeft);
    specGridLayout->addWidget(rangeTypeCombo, 0, 7, 0, 5, Qt::AlignLeft);
    

    I want to add But the results I am getting is not what expected. I am getting the following layout, when I click the button.

    96c1dc33-6e9b-4e97-9919-a0ece6d13a66-image.png

    As you can see above, I am not able to exactly control the positions to align with the top labels.

    Above code adds only one row. I tried adding incrementing rows to add the widgets, but that did not display my widgets in the second row.

    Any ideas or suggestions I can do the resolve the issue? Maybe there is a better approach to implement this design?

    Thanks a lot!!

    qwasder85Q 1 Reply Last reply
    0
    • R Radio1985

      Hi All,

      I want to add multiple Widgets programmatically to a QGridLayout. I have already created the following layout using QT Creator.

      There is one Horizontal Layout Frame and another Frame below it without specifying the layout type.

      79eca0ab-07f2-45e5-a7e6-54bb03146c53-image.png

      0bc980e5-6c6c-46d2-8768-c1f48000753c-image.png

      I have a button call Add, when I click it, it is supposed to add some labels and combo box, and Line Edits horizontally. When I click the button again, it is supposed to add another row below, similar to the previous row.

      The on button click code does the following:

      QGridLayout* specGridLayout = qobject_cast<QGridLayout*>(ui->specFrameLayout->layout());
         if(!specGridLayout){
             qDebug() << "Failed to qobject_cast.";
             specGridLayout = new QGridLayout(ui->specFrameLayout);
         }
      
      // Create LineEdit, ComboBox, and two LineEdits 
             QLineEdit* specNameLine = new QLineEdit(this);
             specNameLine->setFixedWidth(ui->specNameL->width()+20);
             specNameLine->setAlignment(Qt::AlignCenter);
             // Apply a style sheet to hide the line edit border and leave only the bottom edge
             specNameLine->setStyleSheet("border: none; border-bottom: 1px solid black;");
      
             // Connect the combo box 
             QLabel *specTypeLine =  new QLabel("", ui->specFrameLayout);
             specTypeLine->setText(ui->distributionNum->currentText());
             specTypeLine->setFixedWidth(ui->specTypeL->width());
             specTypeLine->setAlignment(Qt::AlignCenter);
      
             QComboBox* rangeTypeCombo = new QComboBox(this);
             rangeTypeCombo->setFixedWidth(ui->rangeTypeL->width()+10);
      
      // Add items to the ComboBox
             rangeTypeCombo->addItem("Select Range Type");
             rangeTypeCombo->addItem("Min");
             rangeTypeCombo->addItem("Max");
             rangeTypeCombo->addItem("Range");
      

      To add the widgets, I am using addWidget overload function.

      specGridLayout->addWidget(specNameLine, 0, 0, 0, 0, Qt::AlignLeft);
      specGridLayout->addWidget(specTypeLine, 0, 1, 0, 5, Qt::AlignLeft);
      specGridLayout->addWidget(rangeTypeCombo, 0, 7, 0, 5, Qt::AlignLeft);
      

      I want to add But the results I am getting is not what expected. I am getting the following layout, when I click the button.

      96c1dc33-6e9b-4e97-9919-a0ece6d13a66-image.png

      As you can see above, I am not able to exactly control the positions to align with the top labels.

      Above code adds only one row. I tried adding incrementing rows to add the widgets, but that did not display my widgets in the second row.

      Any ideas or suggestions I can do the resolve the issue? Maybe there is a better approach to implement this design?

      Thanks a lot!!

      qwasder85Q Offline
      qwasder85Q Offline
      qwasder85
      wrote on last edited by qwasder85
      #2

      @Radio1985 The code only adds one row and not multiple, because you don't increment the row number in each "addWidget"-call. In each loop, you have to retrieve the current row count and set it accordingly. Something like this:

      int row_count = specGridLayout->rowCount();
      specGridLayout->addWidget(specNameLine, row_count , 0, 0, 0, Qt::AlignLeft);
      specGridLayout->addWidget(specTypeLine, row_count , 1, 0, 5, Qt::AlignLeft);
      specGridLayout->addWidget(rangeTypeCombo, row_count , 7, 0, 5, Qt::AlignLeft);
      

      Edit: You write that you've tried this already. That should work, though...

      R 1 Reply Last reply
      0
      • qwasder85Q qwasder85

        @Radio1985 The code only adds one row and not multiple, because you don't increment the row number in each "addWidget"-call. In each loop, you have to retrieve the current row count and set it accordingly. Something like this:

        int row_count = specGridLayout->rowCount();
        specGridLayout->addWidget(specNameLine, row_count , 0, 0, 0, Qt::AlignLeft);
        specGridLayout->addWidget(specTypeLine, row_count , 1, 0, 5, Qt::AlignLeft);
        specGridLayout->addWidget(rangeTypeCombo, row_count , 7, 0, 5, Qt::AlignLeft);
        

        Edit: You write that you've tried this already. That should work, though...

        R Offline
        R Offline
        Radio1985
        wrote on last edited by
        #3

        @qwasder85
        Thanks for the reply. I added the row_count as above. But then I get this error below:
        20239ddb-12a4-4f20-b5be-11519a017a33-image.png

        Now I don't see anything when I click the button

        jsulmJ JonBJ 2 Replies Last reply
        0
        • R Radio1985

          @qwasder85
          Thanks for the reply. I added the row_count as above. But then I get this error below:
          20239ddb-12a4-4f20-b5be-11519a017a33-image.png

          Now I don't see anything when I click the button

          jsulmJ Offline
          jsulmJ Offline
          jsulm
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @Radio1985 said in Add/Delete Multiple Widgets to QGridLayout Programmatically:

          But then I get this error below

          Show the code...

          https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          1
          • R Radio1985

            @qwasder85
            Thanks for the reply. I added the row_count as above. But then I get this error below:
            20239ddb-12a4-4f20-b5be-11519a017a33-image.png

            Now I don't see anything when I click the button

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

            @Radio1985
            ...As @jsulm says, and show the values of the "fromRow" and "toRow" you are passing....

            R 1 Reply Last reply
            0
            • JonBJ JonB

              @Radio1985
              ...As @jsulm says, and show the values of the "fromRow" and "toRow" you are passing....

              R Offline
              R Offline
              Radio1985
              wrote on last edited by
              #6

              @JonB

              I am using the same code above. Added the additional lines below:

              int row_count = specGridLayout->rowCount();
              specGridLayout->addWidget(specNameLine, row_count, 0, 0, 0, Qt::AlignLeft);
              specGridLayout->addWidget(specTypeLine, row_count, 1, 0, 5, Qt::AlignLeft);
              specGridLayout->addWidget(rangeTypeCombo, row_count, 7, 0, 5, Qt::AlignLeft);
              
              JonBJ 1 Reply Last reply
              0
              • R Radio1985

                @JonB

                I am using the same code above. Added the additional lines below:

                int row_count = specGridLayout->rowCount();
                specGridLayout->addWidget(specNameLine, row_count, 0, 0, 0, Qt::AlignLeft);
                specGridLayout->addWidget(specTypeLine, row_count, 1, 0, 5, Qt::AlignLeft);
                specGridLayout->addWidget(rangeTypeCombo, row_count, 7, 0, 5, Qt::AlignLeft);
                
                JonBJ Offline
                JonBJ Offline
                JonB
                wrote on last edited by JonB
                #7

                @Radio1985
                I would start by changing all your row and columns spans which are 0 --- which does not make sense --- to 1?

                On a separate matter, are you aware that with the column starts+spans you specify column #6 will be empty?

                R 1 Reply Last reply
                0
                • JonBJ JonB

                  @Radio1985
                  I would start by changing all your row and columns spans which are 0 --- which does not make sense --- to 1?

                  On a separate matter, are you aware that with the column starts+spans you specify column #6 will be empty?

                  R Offline
                  R Offline
                  Radio1985
                  wrote on last edited by
                  #8

                  @JonB
                  Thanks, yes it works when I change the spans to 1. I get the following. The issue was the row_number starts with 1, even if I don't have added any rows.

                  0eb16266-4de2-4e95-afc4-490e397f77bc-image.png

                  Now I have the other issue on aligning them according to the the label positions I have added.
                  I am not that clear on how the span and positions working.

                  I modify the addWidget as below:

                  specGridLayout->addWidget(specNameLine, row_count, 0, 1, 1, Qt::AlignLeft);
                  
                   specGridLayout->addWidget(specTypeLine, row_count, 1, 2, 1, Qt::AlignLeft);
                  
                   specGridLayout->addWidget(rangeTypeCombo, row_count, 3, 1, 1, Qt::AlignLeft);
                  

                  Then I get the following results.

                  2eace89c-41ec-4f35-ac77-e902dbf260d0-image.png

                  I want to align them according to the label on top.

                  jsulmJ 1 Reply Last reply
                  0
                  • R Radio1985

                    @JonB
                    Thanks, yes it works when I change the spans to 1. I get the following. The issue was the row_number starts with 1, even if I don't have added any rows.

                    0eb16266-4de2-4e95-afc4-490e397f77bc-image.png

                    Now I have the other issue on aligning them according to the the label positions I have added.
                    I am not that clear on how the span and positions working.

                    I modify the addWidget as below:

                    specGridLayout->addWidget(specNameLine, row_count, 0, 1, 1, Qt::AlignLeft);
                    
                     specGridLayout->addWidget(specTypeLine, row_count, 1, 2, 1, Qt::AlignLeft);
                    
                     specGridLayout->addWidget(rangeTypeCombo, row_count, 3, 1, 1, Qt::AlignLeft);
                    

                    Then I get the following results.

                    2eace89c-41ec-4f35-ac77-e902dbf260d0-image.png

                    I want to align them according to the label on top.

                    jsulmJ Offline
                    jsulmJ Offline
                    jsulm
                    Lifetime Qt Champion
                    wrote on last edited by
                    #9

                    @Radio1985 said in Add/Delete Multiple Widgets to QGridLayout Programmatically:

                    I want to align them according to the label on top

                    You added spacers in the first row, but you do not do that for the other rows. That's why they do not align.

                    https://forum.qt.io/topic/113070/qt-code-of-conduct

                    R 1 Reply Last reply
                    0
                    • jsulmJ jsulm

                      @Radio1985 said in Add/Delete Multiple Widgets to QGridLayout Programmatically:

                      I want to align them according to the label on top

                      You added spacers in the first row, but you do not do that for the other rows. That's why they do not align.

                      R Offline
                      R Offline
                      Radio1985
                      wrote on last edited by
                      #10

                      @jsulm
                      Is there is away I can add them programmatically?
                      Thanks!

                      JonBJ jsulmJ 2 Replies Last reply
                      0
                      • R Radio1985

                        @jsulm
                        Is there is away I can add them programmatically?
                        Thanks!

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

                        @Radio1985
                        I don't understand what exactly is wrong/you want to do from your pictures. Perhaps @jsulm does. But, yes, anything can be added programmatically. In fact if you design to produce a .ui file that gets processed into a ui_....h file with C++ code to implement everything designed. So "everything is programmatic". You can even examine the ui_...h file if you want to see what code is generated, for you to maybe copy and use elsewhere.

                        1 Reply Last reply
                        0
                        • R Radio1985

                          @jsulm
                          Is there is away I can add them programmatically?
                          Thanks!

                          jsulmJ Offline
                          jsulmJ Offline
                          jsulm
                          Lifetime Qt Champion
                          wrote on last edited by
                          #12

                          @Radio1985 said in Add/Delete Multiple Widgets to QGridLayout Programmatically:

                          Is there is away I can add them programmatically?

                          Why do you need them? Why don't you simply left align the elements in first row?

                          https://forum.qt.io/topic/113070/qt-code-of-conduct

                          R 1 Reply Last reply
                          1
                          • jsulmJ jsulm

                            @Radio1985 said in Add/Delete Multiple Widgets to QGridLayout Programmatically:

                            Is there is away I can add them programmatically?

                            Why do you need them? Why don't you simply left align the elements in first row?

                            R Offline
                            R Offline
                            Radio1985
                            wrote on last edited by
                            #13

                            @jsulm @JonB ,

                            Thanks for the suggestions. Yeah I was able to learn from ui_...h file how things are implemented.
                            I removed all the alignments in the first row. Then I set my dynamic widget parameter dimensions and alignment according to the first row.

                            Thanks!

                            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