Results 1 to 7 of 7

Thread: How to use Q_DECLARE_METATYPE

  1. #1
    Join Date
    Dec 2006
    Posts
    426
    Thanks
    8
    Thanked 18 Times in 17 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default How to use Q_DECLARE_METATYPE

    Hi,

    My object is derived from QObject, which have copy construction and assignment op disable.

    I try to let QVairant know it by using:
    Q_DECLARE_METATYPE(MyClass *)

    In another class that has a member field of the MyClass, and I define as:
    Q_PROPERTY( MyClass* tvd READ tvd WRITE setTVD )

    However, when reading the obj use QMetaProperty::read( obj ), it complains
    QMetaProperty::read: Unable to handle unregistered datatype "MyClass*' for property..

    How do I actually register a pointer?

    Thanks

  2. #2
    Join Date
    Dec 2006
    Posts
    426
    Thanks
    8
    Thanked 18 Times in 17 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: How to use Q_DECLARE_METATYPE

    The codes look like:
    Qt Code:
    1. class ClassA : public QObject
    2. {
    3. Q_OBJECT
    4. ...
    5. }
    6. Q_DECLARE_METATYPE( ClassA*)
    7.  
    8. class ClassB : public QObject
    9. {
    10. Q_OBJECT
    11. Q_PROPERTY( ClassA* A READ getA WRITE setA)
    12.  
    13. public:
    14.  
    15. ClassA* getA() const { return A;}
    16. void setA( ClassA* a ) {A = a; }
    17.  
    18. private:
    19.  
    20. ClassA* A;
    21.  
    22. }
    To copy to clipboard, switch view to plain text mode 

    The somewhere I try to read the property property.read( ClassB_Object )

    When enter getA(), it starts to complain.

    Any hint is higely appreciated.

  3. #3
    Join Date
    Dec 2006
    Posts
    426
    Thanks
    8
    Thanked 18 Times in 17 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: How to use Q_DECLARE_METATYPE

    Can anyone help, please? Thanks

  4. #4
    Join Date
    Dec 2006
    Posts
    426
    Thanks
    8
    Thanked 18 Times in 17 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: How to use Q_DECLARE_METATYPE

    bump bump bump

  5. #5
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: How to use Q_DECLARE_METATYPE

    Qt Code:
    1. qRegisterMetaType<ClassA*>("ClassA*");
    To copy to clipboard, switch view to plain text mode 
    J-P Nurmi

  6. #6
    Join Date
    Dec 2006
    Posts
    426
    Thanks
    8
    Thanked 18 Times in 17 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: How to use Q_DECLARE_METATYPE

    Quote Originally Posted by jpn View Post
    Qt Code:
    1. qRegisterMetaType<ClassA*>("ClassA*");
    To copy to clipboard, switch view to plain text mode 
    Thanks.

    Then how can I construct the object as:
    Qt Code:
    1. int id = QMetaType::type("ClassA") or QMetaType::type("ClassA*");
    2. void* obj = QMetaType::construct( id );
    3. ClassA* a = static_cast<ClassA*>( obj );
    To copy to clipboard, switch view to plain text mode 
    My ClassA doesn't have copy constructor (disabled).

    I have tried the follow codes, the copy constructor or operator= are never called, why copy constructor are required here? Thank you!
    Qt Code:
    1. class MyStruct {
    2. public:
    3. MyStruct();
    4. MyStruct( const MyStruct& );
    5. MyStruct& operator=( const MyStruct& );
    6. };
    7.  
    8. MyStruct::MyStruct()
    9. {
    10. std::cout << "In constructor" << std::endl;
    11. }
    12.  
    13. MyStruct::MyStruct( const MyStruct& org )
    14. {
    15. std::cout << "In copy constructor" << std::endl;
    16. }
    17.  
    18. MyStruct& MyStruct::operator=( const MyStruct& rhs )
    19. {
    20. std::cout << "operator=" << std::endl;
    21. return *this;
    22. }
    23. int main()
    24. {
    25. qRegisterMetaType<MyStruct>( "MyStruct" );
    26. int id = QMetaType::type("MyStruct");
    27. void* obj = QMetaType::construct( id );
    28. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by lni; 20th July 2008 at 23:03.

  7. #7
    Join Date
    Jan 2008
    Posts
    56
    Thanks
    7
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to use Q_DECLARE_METATYPE

    From the docs:
    int qRegisterMetaType ( const char * typeName )

    Registers the type name typeName to the type T. Returns the internal ID used by QMetaType. Any class or struct that has a public constructor, a public copy constructor, and a public destructor can be registered.
    If you use queued signal-slot connections, the parameters of the signals/slots have to be stored somewhere since the slot is delivered within the event loop at a later time.
    That's why the copy-constructor is needed.

    Regards,

    Rainer

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.