Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Q_DECLARE_METATYPE error
Forum Updated to NodeBB v4.3 + New Features

Q_DECLARE_METATYPE error

Scheduled Pinned Locked Moved Unsolved General and Desktop
13 Posts 2 Posters 12.6k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • saitejS Offline
    saitejS Offline
    saitej
    wrote on last edited by
    #1

    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.

    kshegunovK 1 Reply Last reply
    0
    • saitejS saitej

      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.

      kshegunovK Offline
      kshegunovK Offline
      kshegunov
      Moderators
      wrote on last edited by kshegunov
      #2

      When I try to use Q_DECLARE_METATYPE(DataHandler)

      You shouldn't use that with a QObject subclass.

      Read and abide by the Qt Code of Conduct

      saitejS 3 Replies Last reply
      0
      • kshegunovK kshegunov

        When I try to use Q_DECLARE_METATYPE(DataHandler)

        You shouldn't use that with a QObject subclass.

        saitejS Offline
        saitejS Offline
        saitej
        wrote on last edited by
        #3
        This post is deleted!
        1 Reply Last reply
        0
        • kshegunovK kshegunov

          When I try to use Q_DECLARE_METATYPE(DataHandler)

          You shouldn't use that with a QObject subclass.

          saitejS Offline
          saitejS Offline
          saitej
          wrote on last edited by
          #4

          @kshegunov

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

          1 Reply Last reply
          0
          • kshegunovK kshegunov

            When I try to use Q_DECLARE_METATYPE(DataHandler)

            You shouldn't use that with a QObject subclass.

            saitejS Offline
            saitejS Offline
            saitej
            wrote on last edited by
            #5

            @kshegunov

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

            kshegunovK 1 Reply Last reply
            0
            • saitejS saitej

              @kshegunov

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

              kshegunovK Offline
              kshegunovK Offline
              kshegunov
              Moderators
              wrote on last edited by
              #6

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

              Read and abide by the Qt Code of Conduct

              saitejS 1 Reply Last reply
              0
              • kshegunovK kshegunov

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

                saitejS Offline
                saitejS Offline
                saitej
                wrote on last edited by
                #7

                @kshegunov

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

                kshegunovK 1 Reply Last reply
                0
                • saitejS saitej

                  @kshegunov

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

                  kshegunovK Offline
                  kshegunovK Offline
                  kshegunov
                  Moderators
                  wrote on last edited by
                  #8

                  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.

                  Read and abide by the Qt Code of Conduct

                  saitejS 1 Reply Last reply
                  0
                  • kshegunovK kshegunov

                    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.

                    saitejS Offline
                    saitejS Offline
                    saitej
                    wrote on last edited by saitej
                    #9

                    @kshegunov

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

                    kshegunovK 1 Reply Last reply
                    0
                    • saitejS saitej

                      @kshegunov

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

                      kshegunovK Offline
                      kshegunovK Offline
                      kshegunov
                      Moderators
                      wrote on last edited by
                      #10

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

                      Read and abide by the Qt Code of Conduct

                      saitejS 1 Reply Last reply
                      0
                      • kshegunovK kshegunov

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

                        saitejS Offline
                        saitejS Offline
                        saitej
                        wrote on last edited by
                        #11

                        @kshegunov

                        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
                            }
                        
                        
                        
                        }
                        
                        kshegunovK 1 Reply Last reply
                        0
                        • saitejS saitej

                          @kshegunov

                          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
                              }
                          
                          
                          
                          }
                          
                          kshegunovK Offline
                          kshegunovK Offline
                          kshegunov
                          Moderators
                          wrote on last edited by
                          #12

                          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));
                          

                          Read and abide by the Qt Code of Conduct

                          saitejS 1 Reply Last reply
                          3
                          • kshegunovK kshegunov

                            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));
                            
                            saitejS Offline
                            saitejS Offline
                            saitej
                            wrote on last edited by
                            #13

                            @kshegunov

                            Thanks! it solved my issue.

                            1 Reply Last reply
                            0

                            • Login

                            • Login or register to search.
                            • First post
                              Last post
                            0
                            • Categories
                            • Recent
                            • Tags
                            • Popular
                            • Users
                            • Groups
                            • Search
                            • Get Qt Extensions
                            • Unsolved