[Qt-interest] save QObject based classes in vector with auto delete functionality

Dan Milburn danmilburn at clara.co.uk
Tue Oct 20 14:16:51 CEST 2009


Malyushytsky, Alex wrote:
>>> I have a class derived from QThread (Classes which do Curve Fitting).
> Now I want to save these Classes (which contain the fit results) in a
> vector.
> 
> Don't mix classes and objects.
> 
> As far as I can recall QT3 had QPtrVector class, which does not exist in QT4.
> So I believe there is no class which handles array of pointers.
> 
> But you can:
>  - derive your class from  QValueVector< MyClass* > and add code to destructor to delete all pointers there.
>     ( 1 loop 3 lines of the code )
>  - instead of the raw pointer use guarded pointer like QPointer  http://doc.trolltech.com/4.5/qpointer.html
>    (extra memory allocation, which might be undesired if you have large array)

QPointer does not delete the pointed-to object when it goes out of 
scope.  The only Qt pointer class which would achieve the desired 
behaviour here is QSharedPointer.  Subclassing QVector would probably be 
better as long as you take account of the fact that the base class 
destructor is not virtual.

However I would suggest that the fit results be moved into a separate 
class from the QThread.  Having auto-deletion of QThread objects seems 
like a very bad idea.

Dan



More information about the Qt-interest-old mailing list