As the QObject documentation and many others explain, a QObject has an identity and thus hides its copy constructor and assignment operator.
However, I’m not deriving from QObject for its dynamic properties feature or the signals/slots feature. I only want reflection, or the ability to access Foo::staticMetaObject.
class Foo : public QObject {
Q_OBJECT
Q_ENUMS(Color)
public:
enum Color { Blue, Red, Pink };
private:
Color color;
};
Q_DECLARE_METATYPE(Foo::Color)
I then can’t copy Foo with:
Foo a;
Foo b;
a = b;
What’s the best way to allow copy and assignment in this case? Do I absolutely need to write a copy constructor and assignment operator? What would they look like? Will reflection still work?
If you are only interested in having reflection for
you can use
Q_GADGETinstead ofQ_OBJECT:which will declare and define
Foo::staticMetaObject.