PDA

View Full Version : Ui namespace



ashboi
25th May 2012, 11:58
normally when creating an instance of the UI
QMainWindow *w = new QMainWindow();
Ui::MainWindow *ui = new Ui::MainWindow(w);

but is is possible to write it as such?

QMainWindow *w = new QMainWindow();
MainWindow *ui = new MainWindow(w);

is there some method we could use to remove the namespace qualifier?


Also is Ui::MainWindow and Ui_MainWindow equivalent?
I've read that its equivalent but can't seem to know why.
Could someone please let me know.

Thank you:)

AlekseyOk
25th May 2012, 14:33
you can add next line in cpp-file header

using namespace Ui;

elcuco
25th May 2012, 14:37
you can add next line in cpp-file header

using namespace Ui;

Please don't do this in the header. Leave the headers without any namespace inclusion - let the developer choose which namespace to "use". Otherwise you might get collisions in symbols - which is exactly what namespaces try to fix.

ChrisW67
26th May 2012, 01:00
Also is Ui::MainWindow and Ui_MainWindow equivalent?
I've read that its equivalent but can't seem to know why.
Could someone please let me know.


Read the output of the uic tool.

When uic generates code from a .ui it creates a class called Ui_Form that implements the setupUi() function you call. It also creates a derived class Form that it places in the Ui namespace.

I am not sure of the history of this split arrangement but is suspect it is that way to not break legacy code using Ui_Form directly, while ensuring new code uses the Ui namespace so that (eventually) uic can put everything into namespace Ui. Perhaps some very old compilers did not understand "namespace" and this split arrangement allows them to still function.

All the documentation uses the class in namespace Ui so it would be unwise to rely on the presence of Ui_Form in new code.