Qt Ui Namespace

What is the meaning of the Ui namespace that Qt automatically generates?

Are these two namespaces the same?

In the first, which contains the forward declaration of the MainWindow class from ui_MainWindow.h , why is it not declared as the class Ui::MainWindow ? How does the compiler know that MainWindow comes from an auto-generated Ui header?

In MainWindow.h

 namespace Ui { class MainWindow; //MainWindow from ui_MainWindow.h } 

In ui_MainWindow.h

 namespace Ui { class MainWindow: public Ui_MainWindow {}; } // namespace Ui 
+6
source share
2 answers

It is used to group automatically generated windows into a single namespace. This helps to differentiate the ui class that is generated from the ui design file and the class that implements the functionality.

+5
source

What is the meaning of the Ui namespace that Qt automatically generates?

So, it is in its own β€œbubble” and will not conflict with any other similar class, like the one you create explicitly. Of course, they could make it be called differently and so on, but even then namespaces are good practice in C ++ to deal with them in order to avoid collisions.

Are these two namespaces the same?

Yes, of course, namespaces with the same name inside the same parent namespace are always the same in a project.

I guess I meant why there is a Ui namespace created in Ui_MainWindow.h.

Well, wherever it was created, really? There should be some file where this code is generated, and not pollute any existing source file with the generated code on the fly. As it is called, the way it is, it is again personal taste.

And as the MainWindow class in the Ui namespace in MainWindow.h knows that this is a direct declaration of the MainWindow class in the Ui file, and not created by me.

Well, this has nothing to do with Qt, it's just the basics of C ++. It seems to me that you do not understand what namespaces are and how they are used. It will be "tagged" with a namespace, so the compiler will of course find out which one it belongs to. Now imagine this without a namespace. This would be completely ambiguous for the compiler.

+4
source

Source: https://habr.com/ru/post/972540/


All Articles