[Development] On QML, ownership, QObject-trees and QSharedPointer

Rene Jensen rene at catatonic.dk
Tue May 29 10:55:32 CEST 2012

>> Question: How can we expose objects governed by QSharedPointer to QML
>> safely? I *can* guarantee the lifecycle beyond the life of my
>> QDeclarativeEngine.
> If you can guarantee that, then use mySharedPtr.data() as others have said.

Just a quick thought, would that suggesion be leading into trouble? In
only see one way to do that right:

Step 1: mySharedPtr.data() ==> MyShared* P;
Step 2: make sure the QML engine doesn't assume ownership:
Step 3: hand over P to QML: setProperty or setContextProperty

When I said the database layer should preferably remain QML agnostic,
I merely wanted to keep "patchup code" like "setObjectOwnership" out
of a clean set of data classes for purist reasons. (Really it is an
exaggeration imho.. I have no problem using stuff from QtDeclarative
in code. Will need it for QList properties anyway. It's just a design
Leaving out step 2 is bad, right?

Best regards,
Rene Jensen

More information about the Development mailing list