[Qt-interest] Dereferencing QPointers
Ellen Kestrel
dragonflykes at gmail.com
Sun Apr 19 12:27:15 CEST 2009
Thanks for the info. I had ClassB inherit QObject because it is apparently
needed if you want to use QPointers, which are safer; however, it will
probably work just find with regular old ClassB* non-Qt pointers, so I will
try that instead.
- EK
On Sat, Apr 18, 2009 at 12:00 AM, Thiago Macieira <
thiago.macieira at trolltech.com> wrote:
> Ellen Kestrel wrote:
> >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?
>
> You have to store them as pointers because you can't store them as values.
>
> QObject's copy constructor is private, as the error message you received
> (and pasted in your email) explained. That means you can't create a copy,
> which QList needs you to do.
>
> You can try Anton's suggestion of not deriving from QObject. If you don't
> need signals and slots, that may be the way because it's simpler.
>
> However, there's another solution, which is to use QSharedPointer<ClassB>.
> The QSharedPointer class controls the lifetime of the object, which allows
> you to place it a QList.
>
> However, since it controls the lifetime, you have to make sure nothing
> deletes the object -- only QSharedPointer can delete it. That means you
> must ensure that all objects controlled by a QSharedPointer have no
> parents.
>
> Also note that QSharedPointer<QObject> has no direct operator QObject*(),
> so everywhere where functions expect QObject*, you'll have to add .data().
>
> --
> Thiago Macieira - thiago.macieira (AT) nokia.com
> Senior Product Manager - Nokia, Qt Software
> Sandakerveien 116, NO-0402 Oslo, Norway
>
> _______________________________________________
> Qt-interest mailing list
> Qt-interest at trolltech.com
> http://lists.trolltech.com/mailman/listinfo/qt-interest
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.qt-project.org/pipermail/qt-interest-old/attachments/20090419/f28e5e21/attachment.html
More information about the Qt-interest-old
mailing list