What are differences between:
namespace Ui
{
     class T;
}
class T
{
     //some content
}; 
and
namespace Ui
{
     class T 
     {
          //some content
     };
}
I use Qt Creator and the first construction is used in the default code which is generated for Qt Gui Application. In the example project I have two classes: class MyDialog : public QDialog and class MainWindow : public QMainWindow Each of them contains in the private section a pointer to the class:
class T: public Q
{
private:
          Ui::T *pointer;
}
What is purpose of such contruction ? When MainWindow class contains also a pointer to the MyDialog class then that pointer can't contain Ui:: qualifier:
private:
          Ui::MainWindow *ui;
          MyDialog *mDialog;
Why ?
Interesting question, I didn't noticed this fact before, maybe because usually prefer building Ui objects by code.
Anyway, the construct is used to avoid the generation of 'service' class names: for instance, from an actual .h of mine
namespace Ui {
class Dialog;
}
class Dialog : public QDialog
{
    Q_OBJECT
...
private:
    Ui::Dialog *ui;
}
Note the Ui::Dialog *ui;: that's the actual class that's forwarded by the top declaration (that inside the namespace Ui). So QtCreator avoid to build an arbitrary symbol.
A nice trick, I think.
That forwarded class hides the gory details required by runtime construction of the GUI: you'll find in your build directory a .h (in my case, a ui_dialog.h) containing, for instance
QT_BEGIN_NAMESPACE
class Ui_Dialog
{
public:
    QVBoxLayout *verticalLayout;
    QTabWidget *tabWidget;
...
};
namespace Ui {
    class Dialog: public Ui_Dialog {};
} // namespace Ui
QT_END_NAMESPACE
                        namespaces are there to protect your code from aliasing classes, if you wrap your class in a namespace you can only access it through the use of that namespace, thus "class t" is not the same as "namespace::class t"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With