Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Register a Meta Type in Qt

Tags:

c++

qt

I have a class and I want to use it in a Qvariant, therefore I need to Declare and Register the Meta type. This is what I've done:

class blabla: public QThread
{
   Q_OBJECT
 .
 .
 .
 };
 Q_DECLARE_METATYPE(blabla)

But this code is giving me Error:

In copy constructor ‘QThread::QThread(const QThread&)’:
instantiated from ‘void* qMetaTypeConstructHelper(const T*) [with T = blabla]’
instantiated from ‘int qRegisterMetaType(const char*, T*) [with T = blabla]’
instantiated from here
‘QObject::QObject(const QObject&)’ is private
 within this context
In file included from UnitTest.cpp:16:0:
blabla.h: In copy constructor ‘blabla::blabla(const blabla&)’:
note: synthesized method ‘QThread::QThread(const QThread&)’ first required here 
In file included from /usr/include/QtCore/qvariant.h:48:0,
             from /usr/include/QtCore/qabstractitemmodel.h:45,
             from /usr/include/QtCore/QtCore:7,
             from /usr/include/QtTest/QtTest:3,
             from UnitTest.h:16,
             from UnitTest.cpp:14:
In function ‘void* qMetaTypeConstructHelper(const T*) [with T = blabla]’:
note: synthesized method ‘blabla::blabla(const blabla&)’ first required here 
make[1]: *** [build/obj/UnitTest.o] Error 1

I guess I need to register my met-type, but I am not sure where to have qRegisterMetaType<MyClass>("MyClass");. I tried having it after the meta type declaration macro, but resulted in error. Appriciate any comment or hint which directs me to the right path.

like image 292
mrz Avatar asked Jan 08 '13 14:01

mrz


Video Answer


1 Answers

Objects are copied when put into a QVariant, but QObject derived classes cannot be copied, so the solution is to use a pointer to your class.

Q_DECLARE_METATYPE( blabla* )

Also qRegisterMetaType<T>() is only required for sending your object through queued signal/slot connections.

like image 62
cmannett85 Avatar answered Sep 19 '22 15:09

cmannett85