eMko
eMko

Reputation: 1143

Qt Creator - custom namespace for UI classes

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.

Upvotes: 15

Views: 6894

Answers (1)

Pavel Strakhov
Pavel Strakhov

Reputation: 40502

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.

Upvotes: 24

Related Questions