How to add MySubClass to Q_DECLARE_METATYPE.
This is sample code and please ignoring business of code.
#include <QMetaType>
#include <QVariant>
#include <QDebug>
namespace MyNS {
class MyClass : public QObject {
public:
MyClass() : value(0) { }
MyClass(int value) : value(value) { }
MyClass(const MyClass &other) { value = other.value; }
~MyClass() { }
int getValue() const {
MySubClass msc(value);
QVariant v = QVariant::fromValue(msc);
int tempV = v.value<MySubClass>().getSubValue();
return tempV;
}
private:
class MySubClass
{
public:
MySubClass() : subValue(0) {}
MySubClass(int v) : subValue(v)
{
}
int getSubValue()
{
return subValue;
}
private:
int subValue;
};
// Q_DECLARE_METATYPE(MySubClass); error : 'QMetaTypeId<MyNS::MyClass::MySubClass>': cannot specialize template in current scope
int value;
};
// Q_DECLARE_METATYPE(MySubClass); error : 'MySubClass': undeclared identifier
}
Q_DECLARE_METATYPE(MyNS::MyClass);
int main(int argc, char *argv[])
{
MyNS::MyClass m(15);
QVariant v = QVariant::fromValue(m);
qDebug() << v.canConvert<MyNS::MyClass>();
qDebug() << v.value<MyNS::MyClass>().getValue();
}
Consider MyClass works well without subcalss, but my problem with subclass. I want to use MySubClass with QVariant in my code, but MySubClass is private.