[Qt-interest] What is the use of QWeakPointer

Thiago Macieira thiago at kde.org
Fri Apr 9 15:03:38 CEST 2010


Em Sexta-feira 09 Abril 2010, às 14:55:03, Matthias Pospiech escreveu:
> Dan Milburn schrieb:
> > Thiago Macieira wrote:
> >> Em Sexta-feira 09 Abril 2010, às 12:02:02, Dan Milburn escreveu:
> >>> QWeakPointer<MyObject> weakPointer = ...
> >>> MyObject *o = weakPointer.data();
> >>> if( o )
> >>> {
> >>> 
> >>>     // Do something with o
> >>> 
> >>> }
> >>> 
> >>> Obviously this would cause problems in a multi-threaded environment,
> >>> but then I don't think QWeakPointer is suitable for that.
> >> 
> >> The "proper" way is:
> >> 
> >> QSharedPointer<MyObject> sp = weakPointer;
> >> if ( sp ) {
> >> 
> >>     // do something with the shared pointer
> >>     // and it won't get deleted
> >> 
> >> }
> > 
> > However this won't work if the weak pointer was initialized from a
> > QObject, which seemed to be what the OP was trying to do.
> 
> Indeed, and as far as I understood from the docs - a weakpointer can
> only be constructed with a QObject based class.
> Thus I am little confused.

No. It can also be constructed from a QSharedPointer.

This new method of constructing from QObject is to replace QPointer.
-- 
Thiago Macieira - thiago (AT) macieira.info - thiago (AT) kde.org
  Senior Product Manager - Nokia, Qt Development Frameworks
      PGP/GPG: 0x6EF45358; fingerprint:
      E067 918B B660 DBD1 105C  966C 33F5 F005 6EF4 5358
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 190 bytes
Desc: This is a digitally signed message part.
Url : http://lists.qt-project.org/pipermail/qt-interest-old/attachments/20100409/aa8dabe6/attachment.bin 


More information about the Qt-interest-old mailing list