Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Qt Creator - custom namespace for UI classes

Tags:

c++

qt

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?

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.

like image 564
eMko Avatar asked Jun 18 '13 08:06

eMko


People also ask

What is namespace UI in Qt?

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.

How do I create a Qt UI file?

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.

What is .UI file in Qt?

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.


1 Answers

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.

like image 77
Pavel Strakhov Avatar answered Sep 20 '22 04:09

Pavel Strakhov