Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor removeAllLayoutWidgets #89

Open
rodolakis opened this issue Feb 24, 2024 · 1 comment
Open

refactor removeAllLayoutWidgets #89

rodolakis opened this issue Feb 24, 2024 · 1 comment
Assignees
Labels
enhancement New feature or request
Milestone

Comments

@rodolakis
Copy link
Collaborator

def removeAllLayoutWidgets(layout):
    """Remove all existing widgets from QLayout."""
    for i in reversed(range(layout.count())):
        layout.itemAt(i).widget().setParent(None)

ChatGPT's suggestion:

The function removeAllLayoutWidgets that you've provided sets the parent of each widget in the layout to None, which effectively removes the widget from the layout. However, this does not necessarily delete the widget from memory. The widget still exists and takes up memory, but it is no longer visible and does not have a parent widget.

Using setParent(None) may be sufficient if you plan to reuse the widgets later or if you are managing their deletion elsewhere in your code. However, if you want to ensure that the widgets are deleted and their memory is freed, you should use deleteLater(), as it schedules the widget for deletion in a safe manner.

Here's how you could modify your removeAllLayoutWidgets function to use deleteLater():

 def removeAllLayoutWidgets(layout):
     """Remove all existing widgets from QLayout and schedule them for deletion."""
     for i in reversed(range(layout.count())):
         widget = layout.itemAt(i).widget()
         if widget is not None:  # Check if the layout item is a widget and not None
             widget.deleteLater()

This version of the function will schedule all widgets for deletion, and Qt will delete them the next time it processes events. This is generally the preferred way to remove and delete widgets in Qt when you no longer need them and want to ensure that resources are freed appropriately.

@rodolakis rodolakis added this to mdaviz Feb 24, 2024
@rodolakis rodolakis moved this to In Progress in mdaviz Feb 24, 2024
@rodolakis rodolakis self-assigned this Feb 24, 2024
@rodolakis rodolakis added the enhancement New feature or request label Feb 24, 2024
@rodolakis rodolakis added this to the v1.0 milestone Feb 24, 2024
@rodolakis
Copy link
Collaborator Author

I am only using removeAllLayoutWidgets in mda_file_viz. setPlot, which is called by mda_folder.py: self.mda_file_visualization.setPlot(widgetMpl)
Not sure it is really worth it.

@rodolakis rodolakis removed the status in mdaviz Feb 24, 2024
@rodolakis rodolakis moved this to Could be 1.5 in mdaviz Feb 26, 2024
@rodolakis rodolakis modified the milestones: v1.0, v1.1 Feb 28, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
Status: Upgrades
Development

No branches or pull requests

1 participant