Streight
Streight

Reputation: 861

QT: Private member instead of inheritance? What is the reason? Is this a specific concept?

Some time ago I programmed a GUI with QT Designer / QT Creator. For this question I will first state a schematic of how the general process of creating a GUI with the mentioned IDE works:

  1. Creating the design with the QT Designer -> Get .ui files
  2. The .ui files are translated into header files and you especially get something like "UIbasisclass.h" (with class UIbasisclass) .
  3. You create something like an "UIsubclass.h" (with class UIsubclass) yourself making one private member UIbasisclass ui.

Code within class UIsubclass:

...
        private:

       Ui::UIbasisclass ui;
...

Finally you will create an object of UIsubclass in the main method -> Code:

...
    UIsubclass *MyGUI = new UIsubclass();
...

where the constructor of UIsubclass consists among other code of:

...
    ui.setupUi(this); 
...

In short: We have a UIsubclass that is mostly responsible for applicational methods, but also has a private member of UIbasisclass named ui that consists mostly of design code. When we create an object of UIsubclass its private member UIbasisclass ui is initialized within the constructor of UIsubclass with the object of UIsubclass itself (?). [see: this pointer]

My questions are now:

  1. Why isn't there used inheritance in the way that UIsubclass inherits from UIbasisclass? Instead one object of UIbasisclass becomes member of UIsubclass.

  2. Is this some specific advantageous concept (if yes which advantages has it or how is it named?) or is it "just" a necessity of the QT code structure?

Let me know if I have to specify my questions or if there are any questions.

Upvotes: 1

Views: 887

Answers (3)

Tomaz Canabrava
Tomaz Canabrava

Reputation: 2408

Because with a private member you can forward declare the generated class:

namespace Ui {
    class MyForm;
}

class Foo {
private:
    Ui::MyForm *ui;    
};

and on the .cpp file you insert the include.

this way all of the possible includes of this file will not have to preprocess that file again.

Upvotes: 1

Why isn't there used inheritance in the way that UIsubclass inherits from UIbasisclass?

You're asking us about why you didn't do it in your own code? Just do it. It's up to you. It truly is your code. You are responsible for its design. If you're using a template, it's there to help you get started, but not to design your software for you. The responsibility is yours, and yours only.

it "just" a necessity of the QT code structure?

There is no such necessity. The Ui class is a POD class with a bunch of pointers and one method. Nothing much to it. If you want to privately inherit from it: go right ahead.

Upvotes: 1

Benjamin T
Benjamin T

Reputation: 8311

You can do with private inheritance, it is even documented in Qt documentation.

The use of a private member for ui is the default because of the templates used by Qt Creator, Qt itself does not care.

Upvotes: 2

Related Questions