Qt Creator - UI类的自定义命名空间 [英] Qt Creator - custom namespace for UI classes
问题描述
我想在自己的命名空间中有一个UI类,如ProjectName :: MainWindow。有没有一些方便的方法如何在Qt Creator中实现这个?
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?
我可以打开mainwindow.ui文件,将MainWindow更改为ProjectName :: MainWindow,它编译和工作。但是当我改变UI设计器中的某些东西时,ui文件会再次生成...错误的类名。
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.
推荐答案
当您创建新的Designer窗体类时,请使用命名空间指定类名称,例如 ProjectName :: MainWindow
。 Qt Creator会自动生成以下代码。
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>
正如你看到的, MainWindow
code> Ui :: MainWindow 现在位于 ProjectName
命名空间。
As you see, both MainWindow
and Ui::MainWindow
are now in ProjectName
namespace.
这篇关于Qt Creator - UI类的自定义命名空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!