c++qtcopy-constructormoccopy-assignment

Could I have copy constructor for subclass of QObject?


Here we can read that no copy construct and copy assignment operator evaluable. But here we can read that qRegisterMetaType and Q_DECLARE_METATYPE have to have public default constructor, public copy constructor and public destructor. The question is: who is telling a lie? Or I did not understand it correctly?


Solution

  • Everything is true:
    1. QObject can't be copied and all its descendants can't be copied also.
    2. Q_DECLARE_METATYPE accepts objects with public constructor, copy constructor and destructor.

    There is no contradiction, because you can't register QObject descendants with Q_DECLARE_METATYPE.

    EDIT:

    When you convert your class to QVariant it uses a copy constructor to make a copy of your object:

     void *ptr = QMetaType::construct(x->type, copy);