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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T00:49:41+00:00 2026-05-28T00:49:41+00:00

If I subclassed any widget the usual pattern is: ZTabWidget::ZTabWidget(QWidget *parent):QTabWidget(parent){ blah … blah

  • 0

If I subclassed any widget the usual pattern is:

ZTabWidget::ZTabWidget(QWidget *parent):QTabWidget(parent){
  blah ... blah ...
}

The usual pattern is:

WidgetB widgetb = new WidgetB(widgeta)
widgeta.addWidget(widgetb);

Is there any harm in having all my widgets assigned MainWindow as their respective parent – even though, following the trails of addWidget hierarchy, most of those widgets have another widget as the addWidget parent:

WidgetB widgetb = new WidgetB(mainWindow)
widgeta.addWidget(widgetb);

The reason I wish to do this is because mainWindow holds all the main items of the application and that I could have my any of my subclassed widgets reference those items.

In fact, I am thinking of a proxy widget that is merely used to hold some common references and use that proxy widget as the common parent of all my widgets. Any problem in that?

Exactly, what is the use of having registered a parent for a QWidget? Is the parent even used internally?

Does the addWidget parent have to be the same as the constructor parent?

  • 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-05-28T00:49:41+00:00Added an answer on May 28, 2026 at 12:49 am

    There are actually two aspects two your questions:

    Memory management

    The tree-like organization of QWidgets (and in fact of any QObjects) is part of the memory management strategy used within the Qt-Framework. Assigning a QObject to a parent means that ownership of the child object is transferred to the parent object. If the parent is deleted, all its children are also deleted. From the docs:

    QObjects organize themselves in object trees. When you create a
    QObject with another object as parent, the object will automatically
    add itself to the parent’s children() list. The parent takes ownership
    of the object; i.e., it will automatically delete its children in its
    destructor. You can look for an object by name and optionally type
    using findChild() or findChildren().

    This means you can organize your widgets in any way that seems sensible to you (as long as you don’t introduce memory leaks). I think, however, that it is a common practice to assign all the widgets to their respective container (either explicitly, or better, using methods like addWidget).

    If a widget is assigned to a QLayout using addWidget, then ownership of the widget is transferred to the layout (which in turn is probably owned by another surrounding layout or the main window). So yes, the relationship defined by this method includes the more general parent-child relationship described above.

    Now, as soon as the main window gets destroyed, basically the whole tree of QObjects is deleted as you’d expect.

    Consequently, if you leave an object “parentless”, it is your own responsibility to delete it.

    GUI semantics

    As Frank noted correctly, in some contexts the parent-child relationship between QWidgets also bears a semantically meaning to the GUI framework. An example of this are modal dialogs, who block their parent as long as they stay open.

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

Sidebar

Related Questions

I have subclassed UIViewController into a new class, PageViewController (I'm writing a simple book
As Error and Exception are subclass of throwable class, we can throw any error,
I subclassed UIScrollView Then created an object of it in the main class. Added
I have subclassed Form to include some extra functionality, which boils down to a
I have subclassed java.awt.Frame and have overridden the paint() method as I wish to
I have subclassed Process like so: class EdgeRenderer(Process): def __init__(self,starter,*args,**kwargs): Process.__init__(self,*args,**kwargs) self.starter=starter Then I
I've subclassed an UIImageView. After self = [super initWithImage:image] I try to access the
I have subclassed a control in C# WinForms, and am custom drawing text in
I've subclassed DropDownList to add functionality specific to my application: public class MyDropDownList :
I have a subclassed NSTextView that I am manipulating in a separate thread (using

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.