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:
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;
};
}
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;
};
}
To copy to clipboard, switch view to plain text mode
And I register the class using the following call, at the bottom of the header file:
Q_DECLARE_METATYPE(MyNamespace::ObserverProperty);
Q_DECLARE_METATYPE(MyNamespace::ObserverProperty);
To copy to clipboard, switch view to plain text mode
I want to set this class as a property on a QObject, and the following piece of code works:
prop = obj->property(property_name.toStdString().data());
if (prop.isValid() && prop.canConvert<ObserverProperty>()) {
// Ok it exists.
}
QVariant prop;
prop = obj->property(property_name.toStdString().data());
if (prop.isValid() && prop.canConvert<ObserverProperty>()) {
// Ok it exists.
}
To copy to clipboard, switch view to plain text mode
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");
qRegisterMetaType<MyNamespace::ObserverProperty>("ObserverProperty");
To copy to clipboard, switch view to plain text mode
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().toStdString().data(),property)
ObserverProperty observer_property("My Property");
QVariant property= qVariantFromValue(observer_property);
obj->setProperty(observer_property.objectName().toStdString().data(),property)
To copy to clipboard, switch view to plain text mode
Now the problem is, I get the following error when compiling the qRegisterMetaType() call.
error: expected constructor, destructor, or type conversion before '(' token
error: expected constructor, destructor, or type conversion before '(' token
To copy to clipboard, switch view to plain text mode
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
Bookmarks