Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. What is the purpose of the Ui Namespace in Qt ?
Forum Updated to NodeBB v4.3 + New Features

What is the purpose of the Ui Namespace in Qt ?

Scheduled Pinned Locked Moved General and Desktop
6 Posts 4 Posters 14.4k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    andamedanda
    wrote on 15 May 2013, 16:35 last edited by Chris Kawa
    #1

    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?

    1 Reply Last reply
    0
    • J Offline
      J Offline
      Jeroentjehome
      wrote on 16 May 2013, 07:46 last edited by Chris Kawa
      #2

      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

      Greetz, Jeroen

      1 Reply Last reply
      0
      • A Offline
        A Offline
        andamedanda
        wrote on 16 May 2013, 08:40 last edited by Chris Kawa
        #3

        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?

        1 Reply Last reply
        0
        • J Offline
          J Offline
          Jeroentjehome
          wrote on 16 May 2013, 13:52 last edited by
          #4

          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

          Greetz, Jeroen

          1 Reply Last reply
          0
          • J Offline
            J Offline
            JKSH
            Moderators
            wrote on 16 May 2013, 15:17 last edited by Chris Kawa
            #5

            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.

            Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

            1 Reply Last reply
            0
            • C Offline
              C Offline
              Chris Kawa
              Lifetime Qt Champion
              wrote on 16 May 2013, 17:31 last edited by
              #6

              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.

              1 Reply Last reply
              1

              1/6

              15 May 2013, 16:35

              • Login

              • Login or register to search.
              1 out of 6
              • First post
                1/6
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved