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 7906083
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T10:46:11+00:00 2026-06-03T10:46:11+00:00

I have a widget which changes when an option is toggled. This invalidates all

  • 0

I have a widget which changes when an option is toggled. This invalidates all layouts and widgets. I keep list of all layouts, so I can delete them using something similar to this answer:

class MyWidget(QFrame):
   # ...
   def reLayout(self):
      def deleteLayoutChilds(l):
         while l.count():
            item=l.takeAt(0)
            widget=item.widget()
            if widget: widget.deleteLater()
            else: deleteLayoutChilds(item.layout())
      for l in self.allLayouts: deleteLayoutChilds(l)

      # now install the new layout
      ##
      ## how to delete the old layout first?
      l=self.layout(); del l # no effect
      #
      layout=QGridLayout(self)
      ## warning: QLayout: Attempting to add QLayout "" to MyWidget "", which already has a layout.

How can I get rid of the old layout and set the new one?


The documentation is quite terse and apparently not directly applicable to python:

QWidget.setLayout (self, QLayout)

The QLayout argument has it’s ownership transferred to Qt.

Sets the layout manager for this widget to layout.

If there already is a layout manager installed on this widget, QWidget
won’t let you install another. You must first delete the existing
layout manager (returned by layout()) before you can call setLayout()
with the new layout.

If layout is the layout manger on a different widget, setLayout() will
reparent the layout and make it the layout manager for this widget.

Example:

 QVBoxLayout *layout = new QVBoxLayout;
 layout->addWidget(formWidget);
 setLayout(layout);

An alternative to calling this function is to pass this widget to the
layout’s constructor.

The QWidget will take ownership of layout.

See also layout() and Layout Management.

  • 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-03T10:46:17+00:00Added an answer on June 3, 2026 at 10:46 am

    You can simply reparent the layout to a temporary widget:

    def reLayout(self):
        QWidget().setLayout(self.layout())
        layout = QGridLayout(self)
        ...
    

    That will reparent all the child widgets to that temporary object, and that object is deleted immediately along with its new children because we don’t keep a reference to it.

    But the typical way to have multiple layouts for a single widget and to be able to switch between them is to use a QStackedWidget or QStackedLayout.


    And if you still need the answer to that secondary question:

    How to delete the old layout first?

    It seems you can’t delete directly a QObject which has a parent, because the parent is keeping a reference to that object. But you can add the object to a temporary QObjectCleanupHandler which, like the above solution, will be deleted with the object(s) it contains immediately:

    QObjectCleanupHandler().add(self.layout())
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I did mockups in balsamiq & they have this nice widget, which Allowed to
I have a JavaScript widget which provides standard extension points. One of them is
I have a JavaScript widget which communicates with my Rails app by creating tags
In a C# desktop application, I have a customer widget which contains a text
I have a JS script (widget) which is added to other websites. Technically its
I have a K* window, and within it, a widget which needs the events
Suppose I have a MyWidget which contains a MySubWidget , e.g. a custom widget
I have an application which must save client instances of com.smartgwt.client.widgets.Canvas to a disk
I have a wx.Frame, in which there is a main wx.Panel with several widgets
I am using wordpress twentyten theme. and in this i have set search widget.

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.