在Qt中注册元类型

mrz*_*mrz 7 c++ qt

我有一个类,我想在Qvariant中使用它,因此我需要声明并注册Meta类型.这就是我所做的:

class blabla: public QThread
{
   Q_OBJECT
 .
 .
 .
 };
 Q_DECLARE_METATYPE(blabla)
Run Code Online (Sandbox Code Playgroud)

但是这段代码给了我错误:

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
Run Code Online (Sandbox Code Playgroud)

我想我需要注册我的met-type,但我不确定在哪里qRegisterMetaType<MyClass>("MyClass");.我尝试在元类型声明宏之后使用它,但导致错误.赞美任何指引我走向正确道路的评论或提示.

cma*_*t85 19

放入a时会复制对象QVariant,但QObject无法复制派生类,因此解决方案是使用指向类的指针.

Q_DECLARE_METATYPE( blabla* )
Run Code Online (Sandbox Code Playgroud)

qRegisterMetaType<T>()只需要通过排队的信号/插槽连接发送对象.