I have recently started using QSharedPointer for a particular type of object which is shared between many classes. Previously i had just used a raw pointer and managed the creation and deletion of the objects in one place. But now i realise it will be much easier (and actually necessary) to do reference counting.

For some reason, there are very few examples out there on how to use QSharedPointer, so i find myself posting here.

One problem i have ran into is using signals and slots with the objects that are shared-pointed-to. Previously i had done this:
Qt Code:
  1. MyObject* object; // Subclass of QObject.
  2. AnotherObject* something;
  3. ...
  4. connect(object, SIGNAL(updated()), something, SLOT(update())); // Notify when my object has changed
To copy to clipboard, switch view to plain text mode 

But now that i have changed MyObject* to QSharedObject<MyObject> (typedef'd as MyObjectPtr), the compiler complains:
error C2664: 'bool QObject::connect(const QObject *,const char *,const QObject *,const char *,Qt::ConnectionType)' : cannot convert parameter 1 from 'MyObjectPtr' to 'const QObject *'
No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
I read that i need to use Q_DECLARE_METATYPE and qRegisterMetaType, but i'm not sure about that.
QObject::connect takes a pointer, but here i am passing a QSharedPointer object. Would doing the above allow that type to be passed? Do i still need to do that if the MyObject class is already a subclass of QObject?
And is it safe to just pass the raw pointer obtained from object.data()?