Important: Please read the Qt Code of Conduct -


  • Hi

    I am having the following class:

    class DataHandler : public QObject
        explicit DataHandler(QObject *parent = 0);

    When I try to use


    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.

  • Moderators

    When I try to use Q_DECLARE_METATYPE(DataHandler)

    You shouldn't use that with a QObject subclass.

  • This post is deleted!

  • @kshegunov

    Sorry! I will get back after I try with out QObject subclass.

  • @kshegunov

    I need to use Q_PROPERTY in the DataHandler class. I cannot use the same with out subclassing QObject.

  • Moderators

    You can with Q_GADGET, but this is beside the point. Why do you think you need Q_DECLARE_METATYPE for your QObject subclass?

  • @kshegunov

    I need it as a QVariant type for handling between QML & C++

  • Moderators

    Then you don't need a QObject subclass. You need a copyable datatype. Use Q_GADGET instead of Q_OBJECT and don't derive from QObject.

  • @kshegunov

    I tried using Q_GADGET already. I was getting these errors when I add
    DataHandler *dhandler= new DataHandler();

    • 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(); }

  • Moderators

    Rebuild the project and make sure moc's being run on your file.

  • @kshegunov

    I have rebuild it but m getting the same error. I m adding more detailed code.


    #include "DataHandler.h"
    DataHandler::DataHandler(/*QObject *parent*/) /*: QObject(parent)*/
        m_id = 0;
        m_dispColor = "black";
        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){
        m_id = id;
    //    emit idChanged(id);
    void DataHandler::setDispColor(QString dispColor){
        if(m_dispColor == dispColor)
        m_dispColor = dispColor;
    //    emit dispColorChanged(dispColor);
    void DataHandler::setFlightPath(QList<QVector3D> flightPath){
        if(m_flightPath == flightPath)
        m_flightPath = flightPath;
    //    emit flightPathChanged(flightPath);


    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(;
            m_droneList.push_back(QVariant::fromValue(dat )); // m getting my error here

  • Moderators

    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;

  • @kshegunov

    Thanks! it solved my issue.

Log in to reply