PDA

View Full Version : qRegisterMetaType compilation error: expected constructor, destructor...



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

yogeshgokul
9th September 2009, 08:02
To register, your class should have a public default constructor. Your class doesn't have this. Means there are default parameters but please create an empty also.

JPNaude
9th September 2009, 08:23
Hi

Thanks for the reply.

Isn't the current constructor a public default constructor? I would think that Q_DECLARE_METATYPE would also not work if the class's constructors were incorrect?

I followed your guidelines and changed the class's constructors and destructors to the following:


ObserverProperty() : QObject(0) {
observer_map = new QMap<QString,QVariant>;
setObjectName("Test");
}

ObserverProperty(const ObserverProperty& observer_property) {
observer_map = new QMap<QString,QVariant>(observer_property.observerMap());
setObjectName(observer_property.objectName());
setParent(observer_property.parent());
}
~ObserverProperty() { delete observer_map; }
~SharedObserverProperty() {}


However the problem did not go away.

wysota
9th September 2009, 08:38
But If I understand correctly, I need to call the following to be able to create a QVariant with my class in it:
No, that's not true. If you take a look at setProperty(), it takes a QVariant, so your class already goes through QVariant. qRegisterMetaType is useful if you want to use your class as an argument of a signal with queued connections.

Anyway, if you want to be able to handle QVariant, your object can't inherit QObject as QObjects can't be copied (its copy constructor is private). From what I see you don't need your object to inherit QObject - just remove the inheritance and it should work.

@yogeshgokul: He already has a default constructor, if he creates another one the compiler will complain.

JPNaude
9th September 2009, 09:02
Hi Wysota

Thank you for the help. It now works if I remove the qRegisterMetaType call and remove the QObject base class.

I still get the original error message when I call qRegisterMetaType, but since it is not needed I don't need to call it anymore.

Thanks again,
Regards,
Jaco