[Qt-interest] Dereferencing QPointers

Антон Сердюков uksus70 at gmail.com
Sat Apr 18 06:32:16 CEST 2009


It's because QObject descendats cannot be copied as you want due to
several Meta-object reasons (see docs).
You have two choices: either not to inherit QObject or to store
pointers to the objects. If you really need QObject inheritance,
decide for yourself who is an owner of your objects and establish
correct parent-child relationships to ensure the children are
destroyed once the parent is destroyed.

2009/4/18 Ellen Kestrel <dragonflykes at gmail.com>:
> I have a class we'll call ClassA, which has as a data member a QList of
> objects of ClassB, which inherits QObject.  This list is initially set by
> calling an external function that returns a QList< QPointer<ClassB> >, which
> is converted thusly:
>
> QList< QPointer<ClassB> > tempList = func ();
>
> while (tempList.size () > 0)  {
>   ClassB *classB = tempList[0];
>   classBList.append (*classB);
>   tempList.removeAt (0);
>   delete classB;
> }
>
> When compiling, it errors on the line that dereferences classB and adds it
> to classBList.  The error says:
>
> error: ‘QObject::QObject(const QObject&)’ is private
>
> The external function has to create the objects as pointers, because they
> have to be passed to other functions which have to be able to modify them
> globally, so even if I changed it so that the function returned a
> QList<ClassB> instead of a list of pointers, they would still have to be
> dereferenced at some point.  I don't want to store them in ClassA as a list
> of pointers, because then I would have to write a destructor to avoid memory
> leaks, and there is no need for them to be pointers.  Does anyone know
> what's going wrong, and how I can make this work?
>
> - EK
>
> _______________________________________________
> Qt-interest mailing list
> Qt-interest at trolltech.com
> http://lists.trolltech.com/mailman/listinfo/qt-interest
>
>



-- 
Regards,
Anthony




More information about the Qt-interest-old mailing list