szisziszilvi
15th August 2011, 14:32
Hi,
I would like to add a custom namespace to my Qt GUI application. How should I do that?
Here are the details and what I have tried:
If I create a new Qt console application and I change the main.cpp to this:
// ... includes
namespace
{
int x = 1;
}
int main(int argc, char *argv[])
{
// whatever I wish
}
it works fine.
But if I create a new Qt GUI project I don't know then where to add the namespace code. If I just try to add it to the mainwindow.h like below then it fails:
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
namespace Ui {
class MainWindow;
}
//only this short code is added:
namespace myn
{
int x = 1;
}
class MainWindow : public QMainWindow
{
// ... (nothing is changed here compared to the default new project opening)
};
#endif // MAINWINDOW_H
The error message is that multiple definition for myn::x is given.
I also tried to add a c++ header as if i wanted to create a new class and include it but it did not work. I tried then to add a class with a proper header and and cpp, and defined the namespace befeore the declaration of the class and the strange result is that it can be compiled and it runs as long as I don't use anything from the namespace. But if I put say a spinbox on the gui and I write into the constructor:
ui->spinBox->setValue(myn::x);
it complains again for multiple definition.
Why is this whole? How can I use than custome namespaces for my Qt Application?
Szilvi
I would like to add a custom namespace to my Qt GUI application. How should I do that?
Here are the details and what I have tried:
If I create a new Qt console application and I change the main.cpp to this:
// ... includes
namespace
{
int x = 1;
}
int main(int argc, char *argv[])
{
// whatever I wish
}
it works fine.
But if I create a new Qt GUI project I don't know then where to add the namespace code. If I just try to add it to the mainwindow.h like below then it fails:
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
namespace Ui {
class MainWindow;
}
//only this short code is added:
namespace myn
{
int x = 1;
}
class MainWindow : public QMainWindow
{
// ... (nothing is changed here compared to the default new project opening)
};
#endif // MAINWINDOW_H
The error message is that multiple definition for myn::x is given.
I also tried to add a c++ header as if i wanted to create a new class and include it but it did not work. I tried then to add a class with a proper header and and cpp, and defined the namespace befeore the declaration of the class and the strange result is that it can be compiled and it runs as long as I don't use anything from the namespace. But if I put say a spinbox on the gui and I write into the constructor:
ui->spinBox->setValue(myn::x);
it complains again for multiple definition.
Why is this whole? How can I use than custome namespaces for my Qt Application?
Szilvi