0

I have a issue removing widgets/layouts.

The function tries to update the panel according to what is in the dbo, when a script is inserted or deleted, I want the panel to show the changes, but it doesn't work quite right, the interface becomes a complete mess, this is my function:

enter image description here

After deleting/adding an element, this is how the interface looks like:

enter image description here

Here my code:

github/compliance_operative_control

if self.scripts_list_table is not None:
    while self.scripts_list_table.count():
        child = self.scripts_list_table.takeAt(0)
        if child.widget() is not None:
            child.widget().deleteLater()
            child.widget().setParent(None)
        elif child.layout() is not None:
            child.layout().deleteLater()
            child.layout().setParent(None)
0

1 Answer 1

0

I already found the solution, when you arrive to a layout, what you should do is to iterate that layout before deleting it, because widgets are not deleted together with the layout.

I copy the solution in case someone finds it useful:

def scripts_panel(self):
    if self.scripts_list_table is not None:
        while self.scripts_list_table.count():
            child = self.scripts_list_table.takeAt(0)
            if child.widget() is not None:
                child.widget().deleteLater()
            elif child.layout() is not None:
                while child.count():
                    subchild = child.takeAt(0)
                    subchild.widget().deleteLater()
                child.layout().deleteLater()
Sign up to request clarification or add additional context in comments.

1 Comment

This just adds a further layer to what originally done, but doesn't consider layouts nested in multiple levels. A proper implementation should use recursion, with a basic function that accepts a target layout, delete its direct child widgets, and whenever it finds a child layout will call itself with that. Also, you're not considering spacer items.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.