' qobject& qobject::operator= (const qobject&) ' is private--unable to place a custom qobject subclass into the QT container (container)

Source: Internet
Author: User

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.