2

I have created a DialogBox in QtDesigner

//---------- *.H
namespace Ui {
   class MyDialog;
}

class MyDialog : public QDialog {
   Q_OBJECT
public:
   explicit MyDialog(QWidget* parent = 0);
   ~MyDialog();

 private:
    Ui::MyDialog* ui;  
};

and its source

// --------- *.CPP
MyDialog::MyDialog(QWidget* parent = 0) :
    QDialog(parent),
    ui(new Ui::MyDialog)
{
    ui->setupUi(this);
    //..
}

I would like to structure that in a certain namespace encapsulation, so that I have com::example::mydialogs::MyDialog like:

//---------- *.H
namespace com {
namespace example {
namespace mydialogs {

namespace Ui {
   class MyDialog;
}

class MyDialog : public QDialog {
   Q_OBJECT
public:
   explicit MyDialog(QWidget* parent = 0);
   ~MyDialog();

 private:
    Ui::MyDialog* ui;  
};
}}} //namespace closing

and its source

// --------- *.CPP
namespace com {
namespace example {
namespace mydialogs {

MyDialog::MyDialog(QWidget* parent = 0) :
    QDialog(parent),
    ui(new Ui::MyDialog)
{
    ui->setupUi(this);
    //..
}
}}} //namespace closing

but my compiler complains about that with a message:

C:\MyProjects\Test\com\example\mydialogs\MyDialog.h:29: Error: forward declaration of 'class com::example::mydialogs::Ui::MyDialog' class MyDialog;

The namespace Ui was added by Qt automatically. How can I use my namespace structure properly?

Thomas Dickey
  • 51,086
  • 7
  • 70
  • 105
Ralf Wickum
  • 2,850
  • 9
  • 55
  • 103

2 Answers2

2

In order to forward declare a namespace, you need to use the following canonical syntax:

namespace ns1
{
   namespace ns2
   {
   //....
     namespace nsN
     {
     class a;
     }
  //....    
  }
}

The above code snippet was taken from here. So in your case, you need to close the namespaces where the forward declaration is.

namespace com {
namespace example {
namespace mydialogs {

namespace Ui {
   class MyDialog;
}

} // mydialogs
} // example
} // com

namespace com {
namespace example {
namespace mydialogs {

class MyDialog : public QDialog {
   Q_OBJECT
public:
   explicit MyDialog(QWidget* parent = 0);
   ~MyDialog();

private:
    Ui::MyDialog* ui;  
};
}}} //namespace closing

Your implementation code looks correct. Make those slight changes to the header file and it should compile.

I have not tested this code. Use at your own risk.

Community
  • 1
  • 1
Tyler Jandreau
  • 4,245
  • 1
  • 22
  • 47
0

Just leave outside Ui::MyDialog

namespace Ui {
   class MyDialog;
}

namespace com {
namespace example {
namespace mydialogs {
...

Because the forward declaration Ui::MyDialog is created by uic tool when processing your ui file, it cannot being included in your declaration.

AlexandreP
  • 410
  • 1
  • 5
  • 17
  • `// .. HPP #include namespace Ui { class MyDialog; } using namespace other::namespaces; namespace com { namespace example { namespace mydialogs { class MyDialog : public QDialog { Q_OBJECT public: explicit MyDialog(QWidget* parent = 0); ~MyDialog(); //.. }` and the cource `// .. CPP #include "mydialog.h" namespace com { namespace example { namespace mydialogs { MyDialog::MyDialog(QWidget* parent) : QDialog(parent), ui(new Ui::MyDialog) { ui->setupUi(this); //.. } Same invalid use of type 'class Ui::Mydialog' and forward declaration class Ui:MyDialog – Ralf Wickum Jul 15 '15 at 06:01