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.1k 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.
  • 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