JPNaude
9th September 2009, 07:41
Hi
I'm struggling to get my own class registered in the Meta-Object system. Here is my class:
namespace MyNamespace{
class ObserverProperty : public QObject
{
Q_OBJECT
public:
ObserverProperty(const QString& property_name = QString(), QObject* parent = 0) : QObject(parent) {
observer_map = new QMap<QString,QVariant>;
setObjectName(property_name);
}
ObserverProperty(const ObserverProperty& observer_property) {
observer_map = new QMap<QString,QVariant>(observer_property.observerMap());
setObjectName(observer_property.objectName());
setParent(observer_property.parent());
}
~ObserverProperty() {}
inline QMap<QString,QVariant> observerMap() const { return *observer_map; }
protected:
QMap<QString,QVariant>* observer_map;
};
}
And I register the class using the following call, at the bottom of the header file:
Q_DECLARE_METATYPE(MyNamespace::ObserverProperty);
I want to set this class as a property on a QObject, and the following piece of code works:
QVariant prop;
prop = obj->property(property_name.toStdString().data());
if (prop.isValid() && prop.canConvert<ObserverProperty>()) {
// Ok it exists.
}
But If I understand correctly, I need to call the following to be able to create a QVariant with my class in it:
qRegisterMetaType<MyNamespace::ObserverProperty>("ObserverProperty");
And then create a QVariant with my class in it in the following way:
ObserverProperty observer_property("My Property");
QVariant property= qVariantFromValue(observer_property);
obj->setProperty(observer_property.objectName().toStdSt ring().data(),property)
Now the problem is, I get the following error when compiling the qRegisterMetaType() call.
error: expected constructor, destructor, or type conversion before '(' token
I can't figure out why this is. Is is perhaps because of something wrong in my class? Or perhaps something to do with namespaces.
Any help will be much appreciated.
Thanks,
Jaco
I'm struggling to get my own class registered in the Meta-Object system. Here is my class:
namespace MyNamespace{
class ObserverProperty : public QObject
{
Q_OBJECT
public:
ObserverProperty(const QString& property_name = QString(), QObject* parent = 0) : QObject(parent) {
observer_map = new QMap<QString,QVariant>;
setObjectName(property_name);
}
ObserverProperty(const ObserverProperty& observer_property) {
observer_map = new QMap<QString,QVariant>(observer_property.observerMap());
setObjectName(observer_property.objectName());
setParent(observer_property.parent());
}
~ObserverProperty() {}
inline QMap<QString,QVariant> observerMap() const { return *observer_map; }
protected:
QMap<QString,QVariant>* observer_map;
};
}
And I register the class using the following call, at the bottom of the header file:
Q_DECLARE_METATYPE(MyNamespace::ObserverProperty);
I want to set this class as a property on a QObject, and the following piece of code works:
QVariant prop;
prop = obj->property(property_name.toStdString().data());
if (prop.isValid() && prop.canConvert<ObserverProperty>()) {
// Ok it exists.
}
But If I understand correctly, I need to call the following to be able to create a QVariant with my class in it:
qRegisterMetaType<MyNamespace::ObserverProperty>("ObserverProperty");
And then create a QVariant with my class in it in the following way:
ObserverProperty observer_property("My Property");
QVariant property= qVariantFromValue(observer_property);
obj->setProperty(observer_property.objectName().toStdSt ring().data(),property)
Now the problem is, I get the following error when compiling the qRegisterMetaType() call.
error: expected constructor, destructor, or type conversion before '(' token
I can't figure out why this is. Is is perhaps because of something wrong in my class? Or perhaps something to do with namespaces.
Any help will be much appreciated.
Thanks,
Jaco