I would like to have a UI class in its own namespace, like ProjectName::MainWindow. Is there some convenient way how to achieve this in Qt Creator, please?
I can open the mainwindow.ui file and change the from "MainWindow" to "ProjectName::MainWindow", which compiles and works. But when I change something in the UI designer, the ui file gets generated again ... with the wrong class name.
It is used to group your auto-generated windows in one name-space. It helps to differentiate between the ui class that is generated from designer ui file and the class that implements the functionality.
To create a . ui file go to File -> New File or Project... In the window that appears select Qt under Files and Classes on the left, then select Qt Designer Form on the right. You'll notice the icon has "ui" on it, showing the type of file you're creating.
ui file is used to create a ui_calculatorform. h file that can be used by any file listed in the SOURCES declaration. Note: You can use Qt Creator to create the Calculator Form project. It automatically generates the main.
When you create new Designer Form Class, specify class name with namespace, e.g. ProjectName::MainWindow
. Qt Creator will automatically generate the following code.
MainWindow.h
:
namespace ProjectName {
namespace Ui {
class MainWindow;
}
class MainWindow : public QWidget {
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private:
Ui::MainWindow *ui;
};
} // namespace ProjectName
MainWindow.ui
:
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>ProjectName::MainWindow</class>
<widget class="QWidget" name="ProjectName::MainWindow">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>400</width>
<height>300</height>
</rect>
</property>
<property name="windowTitle">
<string>Form</string>
</property>
</widget>
<resources/>
<connections/>
</ui>
As you see, both MainWindow
and Ui::MainWindow
are now in ProjectName
namespace.
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