Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to simply delete row in QFormLayout programatically

Tags:

layout

qt

pyqt

I have this code:

myEdit = QLineEdit()
myQFormLayout.addRow("myLabelText", myEdit)

Now I have to remove the row by reference to myEdit only:

myQformLayout.removeRow(myEdit)

But there is no API for that. I can use .takeAt(), but how can I get the argument? How do I find the label index, or the index of myEdit?

like image 422
borovsky Avatar asked Dec 12 '12 12:12

borovsky


2 Answers

You can just schedule the widget and its label (if it has one) for deletion, and let the form adjust itself accordingly. The label for the widget can be retrieved using labelForField.

Python Qt code:

    label = myQformLayout.labelForField(myEdit)
    if label is not None:
        label.deleteLater()
    myEdit.deleteLater()
like image 78
ekhumoro Avatar answered Oct 02 '22 15:10

ekhumoro


my solution...

in header file:

QPointer<QFormLayout> propertiesLayout; 

in cpp file:

// Remove existing info before re-populating.
while ( propertiesLayout->count() != 0) // Check this first as warning issued if no items when calling takeAt(0).
{
    QLayoutItem *forDeletion = propertiesLayout->takeAt(0);
    delete forDeletion->widget();
    delete forDeletion;
}
like image 38
Iain Avatar answered Oct 02 '22 14:10

Iain