Reputation: 861
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:
UIbasisclass
) .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:
Why isn't there used inheritance in the way that UIsubclass
inherits from UIbasisclass
? Instead one object of UIbasisclass
becomes member of UIsubclass
.
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
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
Reputation: 98485
Why isn't there used inheritance in the way that
UIsubclass
inherits fromUIbasisclass
?
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
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