2013-01-08 47 views
7

我有一個類,我想在Qvariant中使用它,因此我需要聲明和註冊Meta類型。這是我做了什麼:在Qt中註冊一個元類型

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

但這段代碼是給我的錯誤:

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 

我想我需要註冊我遇到了型,但我不知道哪裏有qRegisterMetaType<MyClass>("MyClass"); 。我嘗試過在元類型聲明宏之後使用它,但導致錯誤。通過任何評論或暗示指引我走向正確的道路。

+0

我相信我在C++源文件中註冊了我正在註冊的類的元類型。和cmannett85一樣,我使用指向類而不是對象的指針。 – drescherjm

回答

19

將對象複製到QVariant時被複制,但QObject派生類不能被複制,因此解決方案是使用指向您的類的指針。

Q_DECLARE_METATYPE(blabla*) 

另外qRegisterMetaType<T>()僅需要通過排隊信號/槽連接發送的對象。

+1

注意:它實際上是Q_DECLARE_METATYPE(blabla *) –

+0

@MatthiasKuhn Woops,是的,你是對的。 – cmannett85

+0

Np。我很高興找到你的答案後,我不得不遇到這個問題。 –