[Interest] How can I use QPointer as an argument?

André Somers andre at familiesomers.nl
Tue Jan 13 14:06:53 CET 2015


Guenther Boelter schreef op 13-1-2015 om 04:13:
> Sorrry,
>
> I have a 'small' problem and my timeline is running so fast ...
>
> How can I use QPointer as an argument? A simple
>
>
> void myFunction( QPointer *myPointer )
> {
>      // do something
> }
>
> myFunction( myPointer );
>
>
> returns 'error: ‘QPointer’ is not a type'.
>
>
> Thanks in advance and best regards

As Constatin already stated: QPointer on its own does not exist. What I 
am wondering though, is why you feel the need to pass a _pointer_to_ a 
QPointer as an argument to myFunction? QPointer is a smart-pointer: it 
acts as a normal pointer, and then provides some other cleverness. In 
this case, the cleverness provided is that it automatically resets to 0 
if the QObject you point to is deleted, so it can't become a dangling 
pointer.

Passing a QPointer* is possible of course, but probably not what you 
wanted to do. Smart pointers are small enough to just copy as values. A 
valid use case for passing a QPointer<MyClass>* is if you are really 
passing an array of QPointers, though I'd probably go for another 
solution like a QVector<QPointer<MyClass>> then.

André




More information about the Interest mailing list