I have a class that is a subclass of QObject that I would like to register as a meta-type. The QObject documentation states that the copy-constructor should be private, but the QMetaType documentation states that a type should have a public default constructor, a public copy constructor, and a public destructor.
I can override QObject's private copy constructor and declare a public copy constructor, but is this safe/ok/right?
class MyClass : public QObject {
Q_OBJECT
public:
MyClass();
MyClass(const MyClass &other);
~MyClass();
}
Q_DECLARE_METATYPE(MyClass);
I use a separate
copyValue(const MyClass & other)
function to copy the data members that define the "values" of theMyClass
instance. That ensures that I don't break the assumption ofQObject
unique identity, while still being able to duplicate the parts of the class that are defined at compile time.It is not safe to make a QObject's copy constructor public. You can register a class pointer as the metatype, though. i.e.:
Q_DECLARE_METATYPE(MyClass*);
That's how Qt handles it with QObject and QWidget.
What you're asking for is perfectly ok. You can't use
QObject
s copy constructor (it's private) in the implementation of your copy constructor, but then again, no-one forces you to:Depending on what services you need from
QObject
, you need to copy some properties over fromother
, in both the copy ctor and the copy assignment operator. E.g., if you use QObject for it's dynamic properties feature, you'd need to copy those, too:Likewise, if you want to maintain signal/slot connections.
And the cpp: