Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
430 views
in Technique[技术] by (71.8m points)

qt4 - Subclass of QObject, qRegisterMetaType, and the private copy constructor

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);
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...