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
Re: How to use Q_DECLARE_METATYPE
The codes look like:
Code:
{
Q_OBJECT
...
}
Q_DECLARE_METATYPE( ClassA*)
{
Q_OBJECT
Q_PROPERTY( ClassA* A READ getA WRITE setA)
public:
ClassA* getA() const { return A;}
void setA( ClassA* a ) {A = a; }
private:
ClassA* A;
}
The somewhere I try to read the property property.read( ClassB_Object )
When enter getA(), it starts to complain.
Any hint is higely appreciated.
Re: How to use Q_DECLARE_METATYPE
Can anyone help, please? Thanks
Re: How to use Q_DECLARE_METATYPE
Re: How to use Q_DECLARE_METATYPE
Code:
qRegisterMetaType<ClassA*>("ClassA*");
Re: How to use Q_DECLARE_METATYPE
Quote:
Originally Posted by
jpn
Code:
qRegisterMetaType<ClassA*>("ClassA*");
Thanks.
Then how can I construct the object as:
Code:
ClassA* a = static_cast<ClassA*>( obj );
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!
Code:
class MyStruct {
public:
MyStruct();
MyStruct( const MyStruct& );
MyStruct& operator=( const MyStruct& );
};
MyStruct::MyStruct()
{
std::cout << "In constructor" << std::endl;
}
MyStruct::MyStruct( const MyStruct& org )
{
std::cout << "In copy constructor" << std::endl;
}
MyStruct& MyStruct::operator=( const MyStruct& rhs )
{
std::cout << "operator=" << std::endl;
return *this;
}
int main()
{
qRegisterMetaType<MyStruct>( "MyStruct" );
}
Re: How to use Q_DECLARE_METATYPE
From the docs:
Quote:
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