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

  • Home
  • SEARCH
  • 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 6093965
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T12:37:21+00:00 2026-05-23T12:37:21+00:00

While going through Qt code I had this basic question on the pimpl implementation.

  • 0

While going through Qt code I had this basic question on the pimpl implementation.
As an example taking QWidget implementation.

QWidget        ---inherits---> QObject
   |                              |
 contains                      contains
   |                              |
  \ /                            \ /
QWidgetPrivate ---inherits---> QObjectPrivate

Now QWidget has two instances of QObjectPrivate (through inheritance and through containment).
Why was the implementation done in this way? Isn’t it an overhead to have two instances of the same object?

  • 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-23T12:37:21+00:00Added an answer on May 23, 2026 at 12:37 pm

    There aren’t two instances of QObjectPrivate when instantiating a QWidget. If you look closely at the header file for QObject, you’ll notice a protected constructor:

    protected:
        QObject(QObjectPrivate &dd, QObject *parent = 0);
    

    Which sets the instance of QObjectPrivate to that passed in via the protected constructor:

    QObject::QObject(QObjectPrivate &dd, QObject *parent)
        : d_ptr(&dd)
    

    This is the constructor called by all of the different QWidget constructors. The QWidget constructor passes in an instace of QWidgetPrivate which, as you noted, is a subclass of QObjectPrivate. Thus, only one instance of QObjectPrivate exists in QWidget.

    Here’s the default QWidget constructor that illustrates this:

    QWidget::QWidget(QWidget *parent, Qt::WindowFlags f)
        : QObject(*new QWidgetPrivate, 0), QPaintDevice()
    {
        QT_TRY {
            d_func()->init(parent, f);
        } QT_CATCH(...) {
            QWidgetExceptionCleaner::cleanup(this, d_func());
            QT_RETHROW;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I noticed this while using Qt, going through the code examples. When they try
I was going through someone's code where I came across a thread: while(TRUE) {
While going through university and from following the development of SO, I've heard a
It's been a while since I've had to do any HTML-like code in Vim
While reading "C# in Depth" I was going through the section titled "Reference types
I am very new to iPhone dev and while going through Sams Teach Yourself
I am trying to learn about Objective-C for iPhone development, and while going through
I have been using C# for a while now, and going back to C++
While the C# spec does include a pre-processor and basic directives (#define, #if, etc),
I am going through some exam questions for the 70-536 exam and an actual

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.