Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Subclass of QObject, qRegisterMetaType, and the private copy constructor

Tags:

qt4

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);
like image 946
darkadept Avatar asked Jun 05 '09 14:06

darkadept


People also ask

Should the qmetatype constructor be public or private?

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.

Is it possible to use a QObject as a copy constructor?

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: class MyClass : public QObject { Q_OBJECT public: // ...

Can QObject subclasses be copied or assigned?

Instances of subclasses of QObject should not be thought of as values that can be copied or assigned, but as unique identities. This means that when you create your own subclass of QObject (director or indirect), you should not give it a copy constructor or an assignment operator.

Why do Qt classes derived from QObject have a private section?

The curious user will have seen that the Qt classes derived from QObject typically include this macro in a private section: It declares a copy constructor and an assignment operator in the private section, so that if you use them by mistake, the compiler will report an error. But even this might not catch absolutely every case.


2 Answers

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.

like image 162
Gerald Avatar answered Oct 03 '22 03:10

Gerald


What you're asking for is perfectly ok. You can't use QObjects copy constructor (it's private) in the implementation of your copy constructor, but then again, no-one forces you to:

class MyClass : public QObject {
    Q_OBJECT
public:
    // ...
    MyClass( const MyClass & other )
        : QObject(), i( other.i ) {} // NOTE: calling QObject default ctor
    // ...
private:
    int i;
};

Depending on what services you need from QObject, you need to copy some properties over from other, 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:

    MyClass( const MyClass & other )
        : QObject(), i( other.i )
    {
        Q_FOREACH( const QByteArray & prop, other.dynamicPropertyNames() )
            setProperty( prop.constData(), other.property( prop.constData() ) );
    }

Likewise, if you want to maintain signal/slot connections.

like image 20
Marc Mutz - mmutz Avatar answered Oct 03 '22 03:10

Marc Mutz - mmutz