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. Is there any way to set maximum column or row count in QGridLayout ?
Forum Updated to NodeBB v4.3 + New Features

Is there any way to set maximum column or row count in QGridLayout ?

Scheduled Pinned Locked Moved General and Desktop
3 Posts 1 Posters 4.4k Views 1 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.
  • A Offline
    A Offline
    Anticross
    wrote on last edited by
    #1

    I have a trivial task to add one widget by another and delete them begining from last add in order that they are added. I'm using grid layout and have some troubles when removing or adding more then one widget. I need that my widgets will be in 5 in a row. Here what my code looks like:
    @void MainWidget::onCountChanged( int count ) {

    int curCount = m_ui.gridLayout->count();
    
    if (count > curCount) {
        while(curCount != count) {
            int row    = 0;
            int column = curCount;
    
            if (count > 5 && count <= 10) {
                row = 1;
                column = column-5;
            }
    
            if (count > 10 && count <= 15) {
                row = 2;
                column = column-10;
            }
    
            m_List.append(new MusWidget(curCount+1));
            m_ui.gridLayout->addWidmusget(m_List.last(),row,column);
            curCount++;
        }
    
    } else {
        while(curCount != count) {
    
            QWidget * wg = m_List.takeLast();
            int index = m_ui.gridLayout->indexOf(wg);
    
            if (index < 0)
                continue;
    
            m_ui.gridLayout->takeAt(index);
            delete wg;
            curCount--;
        }
    }
    

    }@
    And i have problem when my current count is 1 changes to 12 for example, I mean whe I adding more then one in time. So is there any way to set fixed count of columns in QGridLayout and add all widgets using addWidget method with one parameter ?

    1 Reply Last reply
    0
    • A Offline
      A Offline
      Anticross
      wrote on last edited by
      #2

      I modified code and now have another problem. My eleventh element isn't shown when I set count to 11 or more:
      @void MainWidget::onCountChanged( int count ) {

      int curCount = m_ui.gridLayout->count();
      
      if (count > curCount) {
          while(curCount != count) {
              int row    = 0;
              int column = curCount;
      
              if (curCount >= 5 && curCount <= 10) {
                  row = 1;
                  column = column-5;
              }
      
              if (curCount >= 10 && curCount <= 15) {
                  row = 2;
                  column = column-10;
              }
      
              m_List.append(new MusWidget(curCount+1));
              m_ui.gridLayout->addWidget(m_List.last(),row,column);
              curCount++;
          }
      
      } else {
          while(curCount != count) {
      
              QWidget * wg = m_List.takeLast();
              int index = m_ui.gridLayout->indexOf(wg);
      
              if (index < 0)
                  continue;
      
              m_ui.gridLayout->takeAt(index);
              delete wg;
              curCount--;
          }
      }
      

      }@

      1 Reply Last reply
      0
      • A Offline
        A Offline
        Anticross
        wrote on last edited by
        #3

        Oh, here it is:
        @if (curCount >= 5 && curCount <= 10) {
        row = 1;
        column = column-5;
        }

                if (curCount >= 10 && curCount <= 15) {
                    row = 2;
                    column = column-10;
                }@
        

        i need just to change if (curCount >= 5 && curCount <= 10) to if (curCount >= 5 && curCount < 10). So, now it's works. But it's not the best realization.

        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