You are not logged in.

Dear visitor, welcome to QtForum.org. If this is your first visit here, please read the Help. It explains in detail how this page works. To use all features of this page, you should consider registering. Please use the registration form, to register here or read more information about the registration process. If you are already registered, please login here.

1

Monday, June 25th 2012, 7:58am

QSharedPointer with signals and slots

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:

Source code

1
2
3
4
MyObject* object;   // Subclass of QObject.
AnotherObject* something;
...
connect(object, SIGNAL(updated()), something, SLOT(update()));  // Notify when my object has changed


But now that i have changed MyObject* to QSharedObject<MyObject> (typedef'd as MyObjectPtr), the compiler complains:

Quoted


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()?

2

Monday, June 25th 2012, 8:13am

why dont you wait for a reply first?
http://www.qtcentre.org/threads/49617-QS…gnals-and-slots :rolleyes:
If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

3

Tuesday, June 26th 2012, 12:10am

Sorry, i didn't know which forum would get answered first, if at all.
I have replied to the other one.