The code to post the problem first:
1#include <QCoreApplication>2 Classmyobject:publicqobject3 {4 Public:5MyObject (qobject*parent =0):6 Qobject (parent)7 {8 }9 Private:Ten intm_id; One }; A intMainintargcChar*argv[]) - { - qcoreapplication A (argc, argv); theQlist<myobject>list; -List<<myobject (0); - returna.exec (); -}
After compiling the following error, very many: summed up the problem: MyObject is inherited from the Qobject subclass, when its instance into the QT container (such as qlist, Qhash, etc.), will report the above error. For reasons, the official documentation has the following instructions:
Qobject have neither a copy constructor nor an assignment operator. This was by design. Actually, they is declared, but is 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 Ignment operator to be private. The reasoning is found with the discussion on Identity vs Value on the Qt Object Model page.
The main consequence is so 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 Conta Iner classes. You must store pointers.
Simply put:
The copy constructor of the Qobject is private and cannot be constructed when the child class is put into the container.
The workaround is to use pointers. Save the custom subclass as a pointer in the container.
Qlist<myobject *>list;
' qobject& qobject::operator= (const qobject&) ' is private--unable to place a custom qobject subclass into the QT container (container)