How do I copy object in Qt?

后端 未结 3 676
梦毁少年i
梦毁少年i 2020-11-27 21:40

I\'m using Qt and have some real basic problems. I have created my own widget MyTest that have a variable obj. I need to set this variable obj from

3条回答
  •  感动是毒
    2020-11-27 22:35

    It seems the copy constructor and assignment operator are disabled. From this.

    No copy constructor or assignment operator

    QObject has neither a copy constructor nor an assignment operator. This is by design. Actually, they are declared, but in a private section with the macro Q_DISABLE_COPY(). In fact, all Qt classes derived from QObject (direct or indirect) use this macro to declare their copy constructor and assignment operator to be private. The reasoning is found in the discussion on Identity vs Value on the Qt Object Model page.

    The main consequence is that you should use pointers to QObject (or to your QObject subclass) where you might otherwise be tempted to use your QObject subclass as a value. For example, without a copy constructor, you can't use a subclass of QObject as the value to be stored in one of the container classes. You must store pointers.

提交回复
热议问题