Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8979743
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T19:55:00+00:00 2026-06-15T19:55:00+00:00

What is the ‘correct’ or idiomatic way to cleanup/delete widgets when using PyQt4? Consider

  • 0

What is the ‘correct’ or idiomatic way to cleanup/delete widgets when using PyQt4?

Consider the following code:

choices = ['a', 'b', 'c']
checkboxes = []
layout = QtGui.QVBoxLayout()

dialog = MyDialog()

for c in choices:
    checkboxes.append(QtGui.QCheckBox(c)
    layout.addWidget(chkbox)

dialog.setLayout(layout)

for c in checkboxes:
    c.setParent(None)
    c.deleteLater()
    c = None

The above code uses setParent(), deleteLater(), and setting the object to None. Are all of these necessary?

Another possible scenario is I have a dialog with a bunch of widgets on it and want to remove these widgets and add new ones. I don’t want to ‘leak’ the old widgets, but I’m not sure what the correct way to do something like this would be.

It seems to me that deleteLater() might never be needed. Does it just decrement the reference count? If so, wouldn’t just setting the variable to None do the same thing?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T19:55:01+00:00Added an answer on June 15, 2026 at 7:55 pm

    First thing you should remember to do is to use the parent/child relationships for your widgets. When you do this, they will be owned by Qt and will automatically clean up all children when the parent is deleted.

    dialog = MyDialog()
    
    for c in choices:
        checkboxes.append(QtGui.QCheckBox(c, parent=dialog))
        layout.addWidget(chkbox)
    

    In this situation, all the checkboxes will be properly cleaned up when you delete dialog. This handles one part of your question. I realize that you are implicitly having the parent set when you add them to the layout. But you should not then clear that parent before deleting. It is the parent relationship that allows the child to be automatically removed. Not a reference count. The reference aspect would be a python-side thing where it will get garbage collected when there are no more references to it.

    deleteLater is very important, for use when you want the deletion to occur when control returns to the eventloop. It is also the safe way to delete widgets when you are removing some from a layout and adding new ones:

    # clear a layout and delete all widgets
    # aLayout is some QLayout for instance
    while aLayout.count():
        item = aLayout.takeAt(0)
        item.widget().deleteLater()
    

    These widgets will actually be deleted once this method has completed. deleteLater is also useful for deleting the widget under which the slot or event is currently occurring. Such as a QPushButton that can delete itself on click.

    There is also not much need to set c = None. Once a parent is deleted, and that triggers the deletion of all of its children, recursively, your python references to that object will be invalid. So all you need to do is to just not use them anymore. If they are in a list, clear the list. Accessing them would raise the RuntimeError: wrapped C/C++ object of %S has been deleted meaning they are deleted.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I was writing code for dragging mechanism which invokes to wait for small period
I am trying to create a RegEx expression that will successfully parse the following
I'm creating a web service to transfer json to an iPhone app. I'm using
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
We are using XSLT to translate a RIXML file to XML. Our RIXML contains
I have the following tasks in my deploy.rb namespace :unicorn do desc stop unicorn
I have just tried to save a simple *.rtf file with some websites and
I downloaded PostgreSQL from the official website and ran the .dmg installer. After that
Configuring TinyMCE to allow for tags, based on a customer requirement. My config is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.