What is the purpose of the Ui Namespace in Qt ?



  • The code auto-generated by qt creates 2 classes which are similar. For Example :-
    @class Ui_MainWindow
    {
    //...
    }

    namespace ui
    {
    class MainWindow : public Ui_MainWindow
    {};
    }
    @

    The class in the Ui name space i.e Ui::MainWIndow and Ui_MainWindow are exactly the same.
    Then what is the purpose of creating another class in the Ui namespace when we can directly use Ui_Mainwindow itself and what is the purpose of the Ui Namespace?



  • Read this wiki:
    "namespace explained":http://en.wikipedia.org/wiki/Namespace
    or this one:
    "namespace 2":http://www.cplusplus.com/doc/tutorial/namespaces/

    The namespace is used to group your mainwindow and ui_mainwindow together.
    It is C++ and not Qt specific.

    For example using the
    @using namespace std@
    will give you control to use the stdout and stderr etc in your class. When running in terminal the
    @cout << "Notification";@
    will be printed in your terminal box. Without the namespace Qt or C++ has no idea what cout is.
    Greetz



  • Thank you for the answer but, My question was not about what a namespace is.

    Let me rephrase the question in a different way :-
    [quote]We are making 2 separate classes which are the exact same thing.
    Why are we making 2 classes that serve the same purpose ?[/quote]

    One of those 2 similar classes is in the Ui Namespace. Instead of making a similar class in the Ui namespace, we can directly use Ui_MainWindow class itself. What is the drawback in doing so ?

    Also the Auto generated code does not use the Ui Name space. The MainWindow class in the mainwindow.h is Not at all the same as Ui::MainWindow . They are very different classes. The MainWIndow class has a private object of the Ui::MainWIndow class i.e.
    In MainWIndow class deceleration :-
    @
    Class MainWIndow
    {
    //....
    private :
    Ui::MainWindow *ui;
    }@

    This is working perfectly fine if i directly use the Ui_MainWindow class itself. Like this :-
    @Class MainWIndow
    {
    //....
    private:
    Ui_MainWIndow *ui;
    }@

    So why create this other similar class in the Ui Namespace?



  • Yup, I guess your right when there is only 1 MainWindow class in your namespace Ui declaration. But what happens with your "redefinition" when a second window class in included in the Ui namespace declaration. No idea what will happen though, but in theory possible.
    Greetz


  • Moderators

    You could say that it's not strictly necessary. Similarly, many other namespaces out there are not strictly necessary either.

    Some people would say that its purpose is to group all your auto-generated windows in one namespace, and to make your auto-generated member variable (Ui::MainWindow *ui) look different from your other member variables.

    Perhaps the more elegant way to do the above is...
    @
    namespace ui
    {
    class MainWindow {
    // Auto-generated code here
    };
    }
    @
    ... instead of having Ui::MainWindow inherit Ui_MainWindow.

    But in the end, which way we choose to do it has absolutely no effect on the final compiled application.


  • Moderators

    I don't know if it's the actual rationale behind the decision of doing it this way, but here's why I like this solution.
    There are radicals out there that really really love/hate namespaces to the point of bashing any library that doesn't/does use them to encapsulate code that is not actually written by the user. Qt is sorta playing it safe and for lovers provides Ui::MainWindow and for haters there's Ui_MainWindow. For the prevalent rest namespaces are used as a "modern default".

    It's not strictly related but you can even put ALL of Qt into a namespace with a "compiler switch":http://qt-project.org/wiki/Qt-In-Namespace

    Could it be done in any of many other ways? Sure. But does it matter? For most it really comes to choosing :: over _ so it's more of an aesthetic choice of implementation detail.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.