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. qRegisterMetaType results in "call to implicitly-deleted copy constructor"
Qt 6.11 is out! See what's new in the release blog

qRegisterMetaType results in "call to implicitly-deleted copy constructor"

Scheduled Pinned Locked Moved Solved General and Desktop
6 Posts 3 Posters 4.7k Views 3 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.
  • M Offline
    M Offline
    Marc_Van_Daele
    wrote on last edited by
    #1

    I want to dynamically create some components using QMetaType::type and QMetaType::create
    This works fine with the following (dummy) component.

    class Test1
    {
    public:
        Test1(){};
    };
    

    but fails when I use a subclass of QObject

    class Test2 : public QObject
    {
        Q_OBJECT
    public:
        explicit Test2(QObject *parent = 0) : QObject(parent)  {}
    
    signals:
    
    public slots:
    };
    

    I get a compile error on the following code snippet in main.cpp

        qRegisterMetaType<Test2>("Test2");
        int id2 = QMetaType::type("Test2" );
        void * ptr2 = QMetaType::create( id2 );
    

    The error that I get is

    ../../TestCreate/main.cpp:23:12: warning: unused variable 'ptr1' [-Wunused-variable]
        void * ptr1 = QMetaType::create( id1 );
               ^
    ../../TestCreate/main.cpp:24:12: warning: unused variable 'ptr2' [-Wunused-variable]
        void * ptr2 = QMetaType::create( id2 );
               ^
    In file included from ../../TestCreate/main.cpp:1:
    In file included from /Users/marc/Qt5.6/5.6/clang_64/lib/QtGui.framework/Headers/QGuiApplication:1:
    In file included from /Users/marc/Qt5.6/5.6/clang_64/lib/QtGui.framework/Headers/qguiapplication.h:37:
    In file included from /Users/marc/Qt5.6/5.6/clang_64/lib/QtCore.framework/Headers/qcoreapplication.h:40:
    In file included from /Users/marc/Qt5.6/5.6/clang_64/lib/QtCore.framework/Headers/qobject.h:48:
    /Users/marc/Qt5.6/5.6/clang_64/lib/QtCore.framework/Headers/qmetatype.h:760:32: error: call to implicitly-deleted copy constructor of 'Test2'
                return new (where) T(*static_cast<const T*>(t));
                                   ^ ~~~~~~~~~~~~~~~~~~~~~~~~~
    /Users/marc/Qt5.6/5.6/clang_64/lib/QtCore.framework/Headers/qmetatype.h:1689:83: note: in instantiation of member function 'QtMetaTypePrivate::QMetaTypeFunctionHelper<Test2, true>::Construct' requested here
                                       QtMetaTypePrivate::QMetaTypeFunctionHelper<T>::Construct,
                                                                                      ^
    /Users/marc/Qt5.6/5.6/clang_64/lib/QtCore.framework/Headers/qmetatype.h:1717:12: note: in instantiation of function template specialization 'qRegisterNormalizedMetaType<Test2>' requested here
        return qRegisterNormalizedMetaType<T>(normalizedTypeName, dummy, defined);
               ^
    ../../TestCreate/main.cpp:17:5: note: in instantiation of function template specialization 'qRegisterMetaType<Test2>' requested here
        qRegisterMetaType<Test2>("Test2");
        ^
    ../../TestCreate/test2.h:6:15: note: copy constructor of 'Test2' is implicitly deleted because base class 'QObject' has a deleted copy constructor
    class Test2 : public QObject
                  ^
    /Users/marc/Qt5.6/5.6/clang_64/lib/QtCore.framework/Headers/qobject.h:461:20: note: 'QObject' has been explicitly marked deleted here
        Q_DISABLE_COPY(QObject)
                       ^
    /Users/marc/Qt5.6/5.6/clang_64/lib/QtCore.framework/Headers/qglobal.h:318:5: note: expanded from macro 'Q_DISABLE_COPY'
        Class(const Class &) Q_DECL_EQ_DELETE;\
        ^
    2 warnings and 1 error generated.
    

    If I understand this error correctly, qRegisterMetaTypeuses internally a copy constructor (which is deleted explicitly on QObject).
    How can I work around this?

    Thanks for your help,

    Marc

    kshegunovK 1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by SGaist
      #2

      Hi,

      Since Test2 is a QObject based class, you should register Test2 *.

      [incomplete see @kshegunov's answer]

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      1
      • M Marc_Van_Daele

        I want to dynamically create some components using QMetaType::type and QMetaType::create
        This works fine with the following (dummy) component.

        class Test1
        {
        public:
            Test1(){};
        };
        

        but fails when I use a subclass of QObject

        class Test2 : public QObject
        {
            Q_OBJECT
        public:
            explicit Test2(QObject *parent = 0) : QObject(parent)  {}
        
        signals:
        
        public slots:
        };
        

        I get a compile error on the following code snippet in main.cpp

            qRegisterMetaType<Test2>("Test2");
            int id2 = QMetaType::type("Test2" );
            void * ptr2 = QMetaType::create( id2 );
        

        The error that I get is

        ../../TestCreate/main.cpp:23:12: warning: unused variable 'ptr1' [-Wunused-variable]
            void * ptr1 = QMetaType::create( id1 );
                   ^
        ../../TestCreate/main.cpp:24:12: warning: unused variable 'ptr2' [-Wunused-variable]
            void * ptr2 = QMetaType::create( id2 );
                   ^
        In file included from ../../TestCreate/main.cpp:1:
        In file included from /Users/marc/Qt5.6/5.6/clang_64/lib/QtGui.framework/Headers/QGuiApplication:1:
        In file included from /Users/marc/Qt5.6/5.6/clang_64/lib/QtGui.framework/Headers/qguiapplication.h:37:
        In file included from /Users/marc/Qt5.6/5.6/clang_64/lib/QtCore.framework/Headers/qcoreapplication.h:40:
        In file included from /Users/marc/Qt5.6/5.6/clang_64/lib/QtCore.framework/Headers/qobject.h:48:
        /Users/marc/Qt5.6/5.6/clang_64/lib/QtCore.framework/Headers/qmetatype.h:760:32: error: call to implicitly-deleted copy constructor of 'Test2'
                    return new (where) T(*static_cast<const T*>(t));
                                       ^ ~~~~~~~~~~~~~~~~~~~~~~~~~
        /Users/marc/Qt5.6/5.6/clang_64/lib/QtCore.framework/Headers/qmetatype.h:1689:83: note: in instantiation of member function 'QtMetaTypePrivate::QMetaTypeFunctionHelper<Test2, true>::Construct' requested here
                                           QtMetaTypePrivate::QMetaTypeFunctionHelper<T>::Construct,
                                                                                          ^
        /Users/marc/Qt5.6/5.6/clang_64/lib/QtCore.framework/Headers/qmetatype.h:1717:12: note: in instantiation of function template specialization 'qRegisterNormalizedMetaType<Test2>' requested here
            return qRegisterNormalizedMetaType<T>(normalizedTypeName, dummy, defined);
                   ^
        ../../TestCreate/main.cpp:17:5: note: in instantiation of function template specialization 'qRegisterMetaType<Test2>' requested here
            qRegisterMetaType<Test2>("Test2");
            ^
        ../../TestCreate/test2.h:6:15: note: copy constructor of 'Test2' is implicitly deleted because base class 'QObject' has a deleted copy constructor
        class Test2 : public QObject
                      ^
        /Users/marc/Qt5.6/5.6/clang_64/lib/QtCore.framework/Headers/qobject.h:461:20: note: 'QObject' has been explicitly marked deleted here
            Q_DISABLE_COPY(QObject)
                           ^
        /Users/marc/Qt5.6/5.6/clang_64/lib/QtCore.framework/Headers/qglobal.h:318:5: note: expanded from macro 'Q_DISABLE_COPY'
            Class(const Class &) Q_DECL_EQ_DELETE;\
            ^
        2 warnings and 1 error generated.
        

        If I understand this error correctly, qRegisterMetaTypeuses internally a copy constructor (which is deleted explicitly on QObject).
        How can I work around this?

        Thanks for your help,

        Marc

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

        @Marc_Van_Daele
        This thread from the interest mailing list should be useful.

        @SGaist
        This will not help (if you wish, you can see the thread for more details as to why).

        Read and abide by the Qt Code of Conduct

        1 Reply Last reply
        2
        • M Offline
          M Offline
          Marc_Van_Daele
          wrote on last edited by
          #4

          @kshegunov Thanks for the interesting thread!

          kshegunovK 1 Reply Last reply
          0
          • M Marc_Van_Daele

            @kshegunov Thanks for the interesting thread!

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

            @Marc_Van_Daele
            No problem. My advice is to create a factory for your objects as Thiago suggested, because it's the cleanest solution and doesn't presume anything on the part of the QObject instances themselves (like an invokable constructor).

            Read and abide by the Qt Code of Conduct

            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

              Argle, I didn't saw I hit reply… I was looking for that email exchange.

              Thanks for the pointers.

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              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