Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Delete a row from gridLayout



  • Hi,

    Does anyone know how to delete a row from gridlayout. The row int the gridlayout contains widgets.

    Deepak


  • Moderators

    This should do what you want:
    @
    QGridLayout* gridLayout;
    for(int c = 0; c < gridLayout->columnCount(); ++c)
    {
    QLayoutItem* item = gridLayout->itemAtPosition( row, c );
    QWidget* itemWIdget = item->widget();
    if( itemWidget )
    {
    gridLayout->removeWidget(itemWidget);
    delete itemWidget; //not sure if you want that?
    }
    }
    @



  • Hi,

    Thanks .I could do deletion.
    But the row count of gridLayout is not decreasing. When i print the row count of gridLayout before deletion and after deletion they are same. May i know why is it?

    Deepak


  • Moderators

    because that's the concept/implementation of QGridLayout class.
    You must explicitly specify the row and column when you add a item. And it will remain at it's position no matter if a preceding widget was removed, the grid itself stays the same.



  • Ok. Thanks.

    Deepak



  • hi,

    Is there any method to clear all the elements of gridlayout. A single line command as we clear a Qlist with the clear command.


  • Moderators

    no i am aware of... but removing all widgets form a QGridLayout is even simplier:
    @
    while( gridLayout->count() ) {
    QWidget* widget = gridLayout->itemAt(0)->widget();
    if( widget ) {
    layout->removeWidget(widget);
    delete widget;
    }
    }
    @



  • But if we delete like the above, the row count of grid layout doesnot decrease. Is there any where when i delete the elements from a row of the gridlayout and the row count of the gridlayout also decrease.


Log in to reply