I am working with Qt 5.7 (C ++).
Inside the cpp file of one class, I use an anonymous namespace to create a class (some utility), which I will use only in this file.
However, I got binding errors if the utility class is derived from the Qt class. I think the problem is in the Q_OBJECT macro, if I do not add it, I do not get errors. But in any derived Qt class, it is mandatory / recommended to have a Q_OBJECT macro.
How can i avoid this?
Is there any other approach to have a utility class with a file area?
A simple example for displaying errors: the CMyClass class uses a utility class (called CUtility) that derives from QWidget.
Thank.
CMyClass.h
class CMyClass
{
public:
CMyClass();
void someMethod();
};
CMyClass.cpp
#include <QtWidgets>
#include "CMyClass.h"
namespace
{
class CUtility : public QWidget
{
Q_OBJECT
public:
CUtility(QWidget *p_parent = 0) : QWidget(p_parent){qDebug() << "CUtility constructor";}
void utilityMethod() {qDebug() << "This is CUtility::utilityMethod()";}
};
}
CMyClass::CMyClass()
{
qDebug() << "CMyClass constructor.";
}
void CMyClass::someMethod()
{
qDebug() << "This is CMyClass::someMethod().";
CUtility p_myUtil;
p_myUtil.utilityMethod();
}
Errors:
LNK2001: "public: virtual struct QMetaObject const * __cdecl` ':: CUtility:: metaObject (void) const" (? metaObject @CUtility @? A0x27a8253c @@UEBAPEBUQMetaObject @@XZ)
LNK2001: "public: virtual void * __cdecl` ':: CUtility:: qt_metacast (char const *)" (? qt_metacast @CUtility @? A0x27a8253c @@UEAAPEAXPEBD @Z) sin resolver
LNK2001: "public: virtual int __cdecl` anonymous namespace ':: CUtility:: qt_metacall (enum QMetaObject:: Call, int, void * *)" (? qt_metacall @CUtility @? A0x27a8253c @@UEAAHW4Call @QMetaObject @@HPEAPEAX @Z) sin resolver