Unsolved Q_DECLARE_METATYPE error
-
Hi
I am having the following class:
class DataHandler : public QObject { Q_OBJECT public: explicit DataHandler(QObject *parent = 0); ~DataHandler(); ------------------------ };
When I try to use
Q_DECLARE_METATYPE(DataHandler)
It gives the following error:
error: use of deleted function 'DataHandler::DataHandler(const DataHandler&)'
return new (where) T(static_cast<const T>(t));note: in definition of macro 'Q_DISABLE_COPY'
Class(const Class &) Q_DECL_EQ_DELETE;I tried adding Q_DISABLE_COPY(DataHandler) under a private section in DataHandler class but it did not solve the error. Any suggestions to fix this error plz.
-
When I try to use
Q_DECLARE_METATYPE(DataHandler)
You shouldn't use that with a
QObject
subclass. -
This post is deleted! -
Sorry! I will get back after I try with out QObject subclass.
-
I need to use Q_PROPERTY in the DataHandler class. I cannot use the same with out subclassing QObject.
-
You can with
Q_GADGET
, but this is beside the point. Why do you think you needQ_DECLARE_METATYPE
for yourQObject
subclass? -
I need it as a QVariant type for handling between QML & C++
-
Then you don't need a
QObject
subclass. You need a copyable datatype. UseQ_GADGET
instead ofQ_OBJECT
and don't derive fromQObject
. -
I tried using Q_GADGET already. I was getting these errors when I add
DataHandler *dhandler= new DataHandler();
QVariant::fromValue(dhandler)-
C:\Qt\5.7\mingw53_32\include\QtCore\qglobal.h:746: error: static assertion failed: Type is not registered, please use the Q_DECLARE_METATYPE macro to make it known to Qt's meta-object system
#define Q_STATIC_ASSERT_X(Condition, Message) static_assert(bool(Condition), Message)
^ -
C:\Qt\5.7\mingw53_32\include\QtCore\qmetatype.h:1604: error: 'qt_metatype_id' is not a member of 'QMetaTypeId<DataHandler*>'
static inline Q_DECL_CONSTEXPR int qt_metatype_id() { return QMetaTypeId<T>::qt_metatype_id(); }
^ -
C:\Qt\5.7\mingw53_32\include\QtCore\qmetatype.h:1604: error: body of constexpr function 'static constexpr int QMetaTypeId2<T>::qt_metatype_id() [with T = DataHandler*]' not a return-statement
static inline Q_DECL_CONSTEXPR int qt_metatype_id() { return QMetaTypeId<T>::qt_metatype_id(); }
^
-
-
Rebuild the project and make sure
moc
's being run on your file. -
I have rebuild it but m getting the same error. I m adding more detailed code.
DataHandler.cpp
#include "DataHandler.h" DataHandler::DataHandler(/*QObject *parent*/) /*: QObject(parent)*/ { m_id = 0; m_dispColor = "black"; } DataHandler::~DataHandler(){ delete this; } int DataHandler::id() const{ return m_id; } QString DataHandler::dispColor() const{ return m_dispColor; } QList<QVector3D> DataHandler::flightPath() const{ return m_flightPath; } //set fns void DataHandler::setId(int id){ if(m_id == id){ return; } m_id = id; // emit idChanged(id); } void DataHandler::setDispColor(QString dispColor){ if(m_dispColor == dispColor) return; m_dispColor = dispColor; // emit dispColorChanged(dispColor); } void DataHandler::setFlightPath(QList<QVector3D> flightPath){ if(m_flightPath == flightPath) return; m_flightPath = flightPath; // emit flightPathChanged(flightPath); }
qmlhelper.cpp
void QMLHelper::createList(int numList){ std::random_device seeder; std::ranlux48 gen(seeder()); std::uniform_int_distribution<int> uniform_0_255(0, 255); for(int i=0;i<numList;i++){ DataHandler *dat = new DataHandler(); dat ->setId(i); QColor col(uniform_0_255(gen),uniform_0_255(gen),uniform_0_255(gen)); dat ->setDispColor(col.name()); m_droneList.push_back(QVariant::fromValue(dat )); // m getting my error here } }
-
You're trying to save a pointer to the object not the object itself and Qt doesn't know how to handle this. You should work with the object directly as your class has to be copyable and assignable to be a considered a meta-type. You should try like this:
DataHandler dat; //... m_droneList.push_back(QVariant::fromValue(dat));
-
Thanks! it solved my issue.