PDA

View Full Version : How to use the ui_*.h,inherit it or as a member?



75543255
30th August 2009, 09:33
If you use QtCreator the UI file will be generate a UI_*.h file.
In the QtCreator they use it as a member . I think which I make is a window,not has a window.
For example:


//Use it as a member
class MainWin : public QMainWindow
{
Q_OBJECT

public:
MainWin(QWidget *parent = 0);
~MainWin();

private:
Ui::MainWin *ui;//a member value
};




//Inherit as a base class
class MainWin : public QMainWindow,public UI::MainWin//Multiple inheritance
{
Q_OBJECT

public:
MainWin(QWidget *parent = 0);
~MainWin();

};


"is a" means to inherit it as base class,
"has a"means use it as member value,
Athough two ways all can work,but I want to know which is better,what's difference between them.
Why the QtCreator use it as a member?is the way better?

jpn
30th August 2009, 10:45
Quoting docs:


The Single Inheritance Approach
...
The advantages of this approach are its simple use of inheritance to provide a QWidget-based interface, and its encapsulation of the user interface widget variables within the ui data member. We can use this method to define a number of user interfaces within the same widget, each of which is contained within its own namespace, and overlay (or compose) them. This approach can be used to create individual tabs from existing forms, for example.

The Multiple Inheritance Approach
...
In this case, the widgets used in the user interface can be accessed in the same say as a widget created in code by hand. We no longer require the ui prefix to access them.

Subclassing using multiple inheritance gives us more direct access to the contents of the form, is slightly cleaner than the single inheritance approach, but does not conveniently support composition of multiple user interfaces.


Some people prefer the multiple inheritance approach because they say it's cleaner. Other people prefer the single inheritance approach because it makes the code more readable. I'd say it's a matter of taste. Feel free to use the approach that makes you feel more comfortable. :)