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



  • 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


  • Lifetime Qt Champion

    Hi,

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

    [incomplete see @kshegunov's answer]


  • Qt Champions 2016

    @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).



  • @kshegunov Thanks for the interesting thread!


  • Qt Champions 2016

    @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).


  • Lifetime Qt Champion

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

    Thanks for the pointers.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.