This is actually a very good point ... there is no explicit inverse function for addRow() .
To delete a line, you can do the following:
QLineEdit *myEdit; int row; ItemRole role; //find the row myQFormLayout->getWidgetPosition( myEdit, &row, &role); //stop if not found if(row == -1) return; ItemRole otheritemrole; if( role == QFormLayout::FieldRole){ otheritemrole = QFormLayout::LabelRole; } else if( role == QFormLayout::LabelRole){ otheritemrole = QFormLayout::FieldRole; } //get the item corresponding to the widget. this need to be freed QLayoutItem* editItem = myQFormLayout->itemAt ( int row, role ); QLayoutItem* otherItem = 0; //get the item corresponding to the other item. this need to be freed too //only valid if the widget doesn't span the whole row if( role != QFormLayout::SpanningRole){ otherItem = myQFormLayout->itemAt( int row, role ); } //remove the item from the layout myQFormLayout->removeItem(editItem); delete editItem; //eventually remove the other item if( role != QFormLayout::SpanningRole){ myQFormLayout->removeItem(otherItem); delete otherItem }
Please note that I retrieve all the elements before deleting them. This is because I do not know if their role will change when the item is deleted. This behavior is not specified, so I'm playing safe. In qt design, when you remove an element from a form, another element per line takes up all the space (which means its role is changing ...).
Maybe there is a function somewhere and not only did I invent the wheel, but I made a broken one ...
Umnyobe
source share