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



  • 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 ?



  • 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--;
        }
    }
    

    }@



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


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.