copy constructor of derived QT class

前端 未结 2 606
难免孤独
难免孤独 2020-12-06 02:44

I have a class which is publicly inherited from QWidget:

class MyWidget : public QWidget
{
    Q_OBJECT
public:
    MyWidget(const MyWidget&         


        
2条回答
  •  青春惊慌失措
    2020-12-06 03:33

    All Qt classes are noncopyable by deriving from QObject.

    It is common practice in C++ to forbid certain value-semantic operations like copying on polymorphic objects. Qt gives some examples of the problems that would arise if copy was allowed for QObject in its documentation:

    A Qt Object...

    • might have a unique QObject::objectName(). If we copy a Qt Object, what name should we give the copy?
    • has a location in an object hierarchy. If we copy a Qt Object, where should the copy be located?
    • can be connected to other Qt Objects to emit signals to them or to receive signals emitted by them. If we copy a Qt Object, how should we transfer these connections to the copy?
    • can have new properties added to it at runtime that are not declared in the C++ class. If we copy a Qt Object, should the copy include the properties that were added to the original?

提交回复
热议问题