Results 1 to 5 of 5

Thread: qRegisterMetaType compilation error: expected constructor, destructor...

  1. #1
    Join Date
    Aug 2008
    Posts
    132
    Thanks
    23
    Thanked 3 Times in 3 Posts

    Default qRegisterMetaType compilation error: expected constructor, destructor...

    Hi

    I'm struggling to get my own class registered in the Meta-Object system. Here is my class:

    Qt Code:
    1. namespace MyNamespace{
    2. class ObserverProperty : public QObject
    3. {
    4. Q_OBJECT
    5.  
    6. public:
    7. ObserverProperty(const QString& property_name = QString(), QObject* parent = 0) : QObject(parent) {
    8. observer_map = new QMap<QString,QVariant>;
    9. setObjectName(property_name);
    10. }
    11.  
    12. ObserverProperty(const ObserverProperty& observer_property) {
    13. observer_map = new QMap<QString,QVariant>(observer_property.observerMap());
    14. setObjectName(observer_property.objectName());
    15. setParent(observer_property.parent());
    16. }
    17. ~ObserverProperty() {}
    18.  
    19. inline QMap<QString,QVariant> observerMap() const { return *observer_map; }
    20.  
    21. protected:
    22. QMap<QString,QVariant>* observer_map;
    23. };
    24.  
    25. }
    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:

    Qt Code:
    1. 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:

    Qt Code:
    1. QVariant prop;
    2. prop = obj->property(property_name.toStdString().data());
    3. if (prop.isValid() && prop.canConvert<ObserverProperty>()) {
    4. // Ok it exists.
    5. }
    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:

    Qt Code:
    1. 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:

    Qt Code:
    1. ObserverProperty observer_property("My Property");
    2. QVariant property= qVariantFromValue(observer_property);
    3. 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.

    Qt Code:
    1. 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

  2. #2
    Join Date
    Dec 2007
    Posts
    628
    Thanks
    3
    Thanked 89 Times in 87 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: qRegisterMetaType compilation error: expected constructor, destructor...

    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.

  3. #3
    Join Date
    Aug 2008
    Posts
    132
    Thanks
    23
    Thanked 3 Times in 3 Posts

    Default Re: qRegisterMetaType compilation error: expected constructor, destructor...

    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:
    Qt Code:
    1. ObserverProperty() : QObject(0) {
    2. observer_map = new QMap<QString,QVariant>;
    3. setObjectName("Test");
    4. }
    5.  
    6. ObserverProperty(const ObserverProperty& observer_property) {
    7. observer_map = new QMap<QString,QVariant>(observer_property.observerMap());
    8. setObjectName(observer_property.objectName());
    9. setParent(observer_property.parent());
    10. }
    11. ~ObserverProperty() { delete observer_map; }
    12. ~SharedObserverProperty() {}
    To copy to clipboard, switch view to plain text mode 

    However the problem did not go away.

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: qRegisterMetaType compilation error: expected constructor, destructor...

    Quote Originally Posted by JPNaude View Post
    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.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. The following user says thank you to wysota for this useful post:

    JPNaude (9th September 2009)

  6. #5
    Join Date
    Aug 2008
    Posts
    132
    Thanks
    23
    Thanked 3 Times in 3 Posts

    Default Re: qRegisterMetaType compilation error: expected constructor, destructor...

    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

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.