View Full Version : 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
The codes look like:
class ClassA : public QObject
{
Q_OBJECT
...
}
Q_DECLARE_METATYPE( ClassA*)
class ClassB : public QObject
{
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.
Can anyone help, please? Thanks
qRegisterMetaType<ClassA*>("ClassA*");
qRegisterMetaType<ClassA*>("ClassA*");
Thanks.
Then how can I construct the object as:
int id = QMetaType::type("ClassA") or QMetaType::type("ClassA*");
void* obj = QMetaType::construct( id );
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!
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" );
int id = QMetaType::type("MyStruct");
void* obj = QMetaType::construct( id );
}
RThaden
21st July 2008, 18:13
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
Powered by vBulletin® Version 4.2.5 Copyright © 2024 vBulletin Solutions Inc. All rights reserved.